Jump to content

Ulimited get variable from form to $_POST


RathanakBun

Recommended Posts

for ($i=1;$i<10;$i++)

{
         if (isset($_POST['btnsubmit']))

           {
              $fram=$_POST['fram_'.$i];
              $year=$_POST['year_'.$i];
              $ton=$_POST['ton_'.$i];
              $model=$_POST['model_'.$i];
              echo $fram."\t";
              echo $year."\t";
              echo $ton."\t";
              echo $model."\t";
              echo "<br>";

           }

}

 

This code above will show variable till 9 but i want to show only sometime 2 or sometime 3 or sometime 5 how can i close other variable

what code i can use or what function ?

 

First off, change your field names so the groups of fields will be in an array

 

 

Car 1:
<input type="text" name="cars[0][fram]" />
<input type="text" name="cars[0][year]" />
<input type="text" name="cars[0][ton]" />
<input type="text" name="cars[0][model]" />
 
Car 2:
<input type="text" name="cars[1][fram]" />
<input type="text" name="cars[1][year]" />
<input type="text" name="cars[1][ton]" />
<input type="text" name="cars[1][model]" />
. . . .etc.

 

Now, you can iterate over each group of fields in a more simple/logical manner

 

foreach($_POST['cars'] as $car)
{
    echo "{$car['fram']}\t{$car['year']}\t{$car['ton']}\t{$car['model']}\t<br>";
}

 

As for limiting the number of records you process - it all depends. If you limit the number of field sets that are created then the foreach() loop will work perfectly. Else, if you will always have 10 fieldsets but only want to process n, then you can use array_slice() to trim the array to the number of elements you want.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.