jeff5656 Posted June 16, 2011 Share Posted June 16, 2011 I have a form that has variables with a number at the end. When user hits submit, I want to echo them all out in a while loop. The names of these variables are the same except for the number, i.e. variable1, variable2, etc. How do I do this? the number of variables is : $_POST['num_variables'] $i = 0; while ($i++ < $_POST['num_variables']) { echo $_POST['variable'] <---- where do I put the $i? is it $_POST['variable'.$i]?? Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/ Share on other sites More sharing options...
fugix Posted June 16, 2011 Share Posted June 16, 2011 for this you would want to use a for loop $i = 0; for ($i = 0;$i < $_POST['num_variables']; $i++) { echo {$_POST['variable']}{$i} Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230665 Share on other sites More sharing options...
redixx Posted June 16, 2011 Share Posted June 16, 2011 Use a for loop. for($i=0;$i<$_POST['num_variables'];$i++) { echo $_POST['variable'][$i]; } EDIT: Damn, fugix beat me Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230666 Share on other sites More sharing options...
freelance84 Posted June 16, 2011 Share Posted June 16, 2011 If there isn't much in the form you could also use a foreach on the $_POST foreach($_POST as $key=>$value) { echo "$key = $value"; } I found that useful when to know when I first learned about looping through the $_POST array Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230673 Share on other sites More sharing options...
redixx Posted June 16, 2011 Share Posted June 16, 2011 If there isn't much in the form you could also use a foreach on the $_POST foreach($_POST as $key=>$value) Then you get unnecessary things, such as the submit button or other form elements. Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230676 Share on other sites More sharing options...
fugix Posted June 16, 2011 Share Posted June 16, 2011 If there isn't much in the form you could also use a foreach on the $_POST foreach($_POST as $key=>$value) { echo "$key = $value"; } I found that useful when to know when I first learned about looping through the $_POST array As you stated, it depends on how much information is being sent to the $_POST array, you would most likely get unwanted values that would need to be filtered out, causing more work, however this could be a feasible solution as well Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230683 Share on other sites More sharing options...
freelance84 Posted June 16, 2011 Share Posted June 16, 2011 True. I had a selection of radio buttons 1>5 with tick boxes next to them on a form once. It was easier to add a 'flag' at the beginning of each radio button then when running through each post, if the flag was there I knew it was a radio, then i could check for a checkbox among other possible hidden values dependent on the users action on the page. Then process the data accordingly PS// It was on here that somebody pointed it out to me and it was a real eye opener at the time. Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230687 Share on other sites More sharing options...
redixx Posted June 16, 2011 Share Posted June 16, 2011 True. I had a selection of radio buttons 1>5 with tick boxes next to them on a form once. It was easier to add a 'flag' at the beginning of each radio button then when running through each post, if the flag was there I knew it was a radio, then i could check for a checkbox among other possible hidden values dependent on the users action on the page. Then process the data accordingly PS// It was on here that somebody pointed it out to me and it was a real eye opener at the time. Why not just do this: <?php $radios = $_POST['radio']; echo $radios[0]; ?> <form action="radio.php" method="post" /> <input type="radio" name="radio[]" value="1" /> <input type="radio" name="radio[]" value="2" /> <input type="radio" name="radio[]" value="3" /> <input type="radio" name="radio[]" value="4" /> <input type="radio" name="radio[]" value="5" /> <input type="submit" name="submit" value="submit" /> </form> Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230694 Share on other sites More sharing options...
jeff5656 Posted June 16, 2011 Author Share Posted June 16, 2011 This doesn't work: for($i=0;$i<$_POST['num_variables'];$i++) { echo $i.": ".$_POST['variable'][$i]."<br>"; } I get 1: 2: 3: But it's blank. I want to echo $_POST['variable1'] but instead of 1 I am using $i in the loop, but when i do this: echo $_POST['variable'] [$i] it is blank! Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230714 Share on other sites More sharing options...
freelance84 Posted June 16, 2011 Share Posted June 16, 2011 The page displayed several rows On each row started a checkbox then the title of the radio in question then the 5 radio buttons in a line. The user could also add, remove, re-order the radio button rows depending on their settings. So instead of using js to keep track of the order and quantity i simply used a foreach, then search the post array for matching check boxes and hidden fields. The time the php script took to complete the script was so small i didnt think it needed to complicate things further with more js. Saying that, it was a while ago and was an early project so there is do doubt a more efficient way Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230717 Share on other sites More sharing options...
redixx Posted June 16, 2011 Share Posted June 16, 2011 This doesn't work: for($i=0;$i<$_POST['num_variables'];$i++) { echo $i.": ".$_POST['variable'][$i]."<br>"; } I get 1: 2: 3: But it's blank. I want to echo $_POST['variable1'] but instead of 1 I am using $i in the loop, but when i do this: echo $_POST['variable'] [$i] it is blank! Okay, now I see what you're trying to do. Try this then: for($i=1;$i<=$_POST['num_variables'];$i++) { echo $_POST['variable' . $i] . '<br />'; } Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230720 Share on other sites More sharing options...
jeff5656 Posted June 16, 2011 Author Share Posted June 16, 2011 "Okay, now I see what you're trying to do. Try this then:" That did it. Thanks! Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230726 Share on other sites More sharing options...
fugix Posted June 16, 2011 Share Posted June 16, 2011 basically the same thing i had wrote for you, glad you got it working though Link to comment https://forums.phpfreaks.com/topic/239575-how-to-echo-out-posted-variables-that-increment-by-a-number/#findComment-1230733 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.