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 ?

 

Link to comment
Share on other sites

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.

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.