firedealer Posted June 16, 2010 Share Posted June 16, 2010 Below the PHP code will show the first record only. If I replace $chunks in for() to the true number which is 5, it will show two records. I need $chunks to be there for customize from $_POST. Any solution? Thanks. <?php // Retrieve from $_POST which isn't included in this code for now. $word = "applesauce"; $number = 5; $chunks = ceil(strlen($word)/$number); echo "The $number-letter chunks of '$word' are:<br />\n"; for ($i=0; $i < $chunks; $i++) { $chunks = substr($word, $i*$number, $number); printf("%d: %s<br />\n", $i+1, $chunks); } ?> Link to comment https://forums.phpfreaks.com/topic/204995-for-problem-should-show-all-results/ Share on other sites More sharing options...
mrMarcus Posted June 17, 2010 Share Posted June 17, 2010 printf("%d: %s<br />\n", $i+1, $chunks); $i+1 is incrementing $i by 1 causing your for() loop condition of $i < $chunks to complete on the first pass. Link to comment https://forums.phpfreaks.com/topic/204995-for-problem-should-show-all-results/#findComment-1073195 Share on other sites More sharing options...
firedealer Posted June 17, 2010 Author Share Posted June 17, 2010 Your comment doesn't make a sense to me. Can you post a solution code to enlighten me? Link to comment https://forums.phpfreaks.com/topic/204995-for-problem-should-show-all-results/#findComment-1073202 Share on other sites More sharing options...
kenrbnsn Posted June 17, 2010 Share Posted June 17, 2010 You're reusing the variable $chunks inside the for loop. Use a different variable. Ken Link to comment https://forums.phpfreaks.com/topic/204995-for-problem-should-show-all-results/#findComment-1073204 Share on other sites More sharing options...
mrMarcus Posted June 17, 2010 Share Posted June 17, 2010 Yes, my head was somewhere else. You're reassigning $chunks within the loop. $word = "applesauce"; $number = 5; $chunks = ceil(strlen($word)/$number); echo "The {$number}-letter {$chunks} of '{$word}' are:<br />\n"; for ($i=0; $i < $chunks; $i++) { $chunks2 = substr($word, $i*$number, $number); printf("%d: %s<br />\n", $i+1, $chunks2); } Link to comment https://forums.phpfreaks.com/topic/204995-for-problem-should-show-all-results/#findComment-1073206 Share on other sites More sharing options...
firedealer Posted June 17, 2010 Author Share Posted June 17, 2010 Thanks for a solution code. Here mine is: <?php $word = "applesauce"; $number = 5; $calc = ceil(strlen($word)/$number); $howmany = $calc; $chunks = $calc; echo "The $number-letter chunks of '$word' are:<br />\n"; for ($i=0; $i < $howmany; $i++) { $chunks = substr($word, $i*$number, $number); printf("%d: %s<br />\n", $i+1, $chunks); } Link to comment https://forums.phpfreaks.com/topic/204995-for-problem-should-show-all-results/#findComment-1073208 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.