0

Let's say I have this inputs :

<input type="hidden" name="block-1" value="001"/>
<input type="hidden" name="block-2" value="012"/>
<input type="hidden" name="block-3" value="002"/>
<input type="hidden" name="block-4" value="005"/>
<input type="hidden" name="block-5" value="008"/>

and I want to process those input using this PHP loop

$i = 1;

do {
    $x = 'block-'.$i;
    $webBlock = $_POST[$x];
    //some codes here
    $i++;
}

while (!empty($webBlock));

why I always have 6 outputs? and the last one is blank output. seems that loop always doing n+1. how to make correct loop based on number of inputs given? thanks!

1
  • khan : sure. I'm trying all possibilities now... Oct 19, 2012 at 11:27

6 Answers 6

1

Do you need while?

I'd go with:

$i=0;
foreach($_POST as $name => $value)
{
   if( strpos($name , 'block-') !== false ) echo $i . " - " . $name . ": " . $value;
   $i++;
}

Believe that should account for items named 'block-n'. The if statement basically says "if block- is anywhere in the name of the field, echo out such and such". Let me know if you get an error and will amend.

3
  • because I need $i. can 'foreach' provide that? Oct 19, 2012 at 11:19
  • bro, I just notice that I have 3 more <input type="hidden"> under the code. this will cause N+3 now... how to have foreach loop but specific to $_POST[$x] and $x = 'block-'.$i; Oct 19, 2012 at 11:40
  • 1
    @RobertHanson Give us a moment and I'll write it up
    – Gga
    Oct 19, 2012 at 13:17
1

Because you are using a repeat loop, you should use a while loop:

while (!empty($webBlock)){
    $x = 'block-'.$i;
    $webBlock = $_POST[$x];
    //some codes here
    $i++;
}
1

beacause do will get executed at least once what ever may be in while expression .do while is an exit control loop.

1

Try this:

$i = 0;

do {
    $i++;
    $x = 'block-'.$i;
    $webBlock = $_POST[$x];
    //some codes here
}
while (!empty($webBlock));

UPD: The best approach is this:

for ($i = 1; $i <= count($_POST); $i++) {
    $webBlock = $_POST['block-'.$i];
    //some code here
}
1
  • You are right, my bad. Updated with the most correct approach.
    – dfsq
    Oct 19, 2012 at 11:29
0

Because You are using Do While loop.I am not sure about your data but i must say that it will execute one more time as you expected.Let us check it by iterating
It will run first time because $webBlock is not empty and same as for 5 times.
Now here you want to close the loop but your 5th iteration's condition gets true.Now it will execute once more and show nothing (because there is nothing in $webBlock)and now condition will find that $webBlock is empty.
I suggest you to use while loop here.It will solve your issue

0

I am not a PHP developer so I will just give you general idea about what you are doing.

Actually you are using Do-while loop which is sometimes called Exit Control loop. So, in case of Do-While after executing code your condition will be checked. So, even you didn't get any value in 'webBlock' your code will be executed. So, this is your bug.

Instead of this you can use While loop, Entry control loop. You code is executed if and only if condition is true.

$i = 1;
while (i>0) {
     $x = 'block-'.$i;
     $webBlock = $_POST[$x];
    if(empty($webBlock))
    {
      break;
   }
   $i++;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.