Jump to content


Photo

Ulimited get variable from form to $_POST


  • Please log in to reply
1 reply to this topic

#1 RathanakBun

RathanakBun

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 24 April 2013 - 09:15 PM

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 ?

 



#2 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,750 posts
  • LocationCanada

Posted 25 April 2013 - 12:49 AM

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.


The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com