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); } ?> Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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); } Quote Link to comment 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); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.