Jump to content

for() problem - should show all results


firedealer

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.