vynsane Posted July 7, 2007 Share Posted July 7, 2007 i have a script that sets up a second page with multiple select boxes. the first script has a hidden field for the ID number, and a text input field for quantity. the second part of the script has a foreach loop that gets the info that goes with the ID number and then creates the select box next to it. for the most part it's all working correctly, but i don't know how to get the submitted "quantity" to repeat that ID number (and everything else). here is the initial form: <table> <tr> <td class="textright">How many <strong>Editors:</strong><input type="hidden" name="catID[]" value="1" /></td> <td> <input type="text" style="width:30px;" name="qty[]" value="0" /> </td> </tr> <tr> <td class="textright">How many <strong>Plots:</strong><input type="hidden" name="catID[]" value="2" /></td> <td> <input type="text" style="width:30px;" name="qty[]" value="0" /> </td> </tr> <tr> <td class="textright">How many <strong>Scripts:</strong><input type="hidden" name="catID[]" value="3" /></td> <td> <input type="text" style="width:30px;" name="qty[]" value="0" /> </td> </tr> <tr> <td class="textright">How many <strong>Writers:</strong><input type="hidden" name="catID[]" value="4" /></td> <td> <input type="text" style="width:30px;" name="qty[]" value="0" /> </td> </tr> <tr> <td class="textright">How many <strong>Artists:</strong><input type="hidden" name="catID[]" value="5" /></td> <td> <input type="text" style="width:30px;" name="qty[]" value="0" /> </td> </tr> <tr> <td class="textright">How many <strong>Pencillers:</strong><input type="hidden" name="catID[]" value="6" /></td> <td> <input type="text" style="width:30px;" name="qty[]" value="0" /> </td> </tr> <tr> <td class="textright">How many <strong>Inkers:</strong><input type="hidden" name="catID[]" value="7" /></td> <td> <input type="text" style="width:30px;" name="qty[]" value="0" /> </td> </tr> <tr> <td class="textright">How many <strong>Cover Arts:</strong><input type="hidden" name="catID[]" value="8" /></td> <td> <input type="text" style="width:30px;" name="qty[]" value="0" /> </td> </tr> <tr> <td class="textright">How many <strong>Additional Arts:</strong><input type="hidden" name="catID[]" value="9" /></td> <td> <input type="text" style="width:30px;" name="qty[]" value="0" /> </td> </tr> </table> this gives me an array like this: Array ( ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) [qty] => Array ( [0] => 1 [1] => 0 [2] => 0 [3] => 2 [4] => 0 [5] => 2 [6] => 0 [7] => 1 [8] => 0 ) [next] => Next ) the php code on the second step of the form is the foreach loop to spit out the jobs and select boxes: foreach ($_POST['qty'] as $k => $qty) { $catID = $_POST['catID'][$k]; if($qty !=='0') { $jobsQuery = mysql_query("SELECT * FROM Category WHERE ID = '".$catID."'"); while ($jobs = mysql_fetch_array($jobsQuery)){ echo "<tr>\n"; echo "<td class=\"textright\"><strong>".$jobs['category'].":</strong><input type=\"hidden\" name=\"catID[]\" value=\"".$jobs['ID']."\" /></td>\n"; echo "<td>\n"; echo "<select name=\"creatorID[]\">\n"; echo "<option selected=\"selected\" value=\"0\">Choose</option>\n"; $creatorsQuery = mysql_query("SELECT * FROM names ORDER BY Lname"); while($creators = mysql_fetch_array($creatorsQuery)){ echo "<option value=\"".$creators['ID']."\">"; if($creators['Lname'] !=Null){ echo "".$creators['Lname'].", "; } if($creators['Fname'] !=Null){ echo "".$creators['Fname'].""; } if($creators['suffix'] !=Null){ echo ", ".$creators['suffix'].""; } echo "</option>\n"; } echo "</select>\n"; echo "</td>\n"; echo "</tr>\n"; } } } this outputs only the fields that were not "$qty=0" like below: <table> <tr> <td class="textright"><strong>Editor:</strong><input type="hidden" name="catID[]" value="1" /></td> <td> <select name="creatorID[]"> <option selected="selected" value="0">Choose</option> <option value="67">Paty</option> <option value="282">Studio F</option> <option value="296">Dream Engine</option> <option value="182">Abel, Jack</option> </select> </td> </tr> <tr> <td class="textright"><strong>Writer:</strong><input type="hidden" name="catID[]" value="4" /></td> <td> <select name="creatorID[]"> <option selected="selected" value="0">Choose</option> <option value="67">Paty</option> <option value="282">Studio F</option> <option value="296">Dream Engine</option> <option value="182">Abel, Jack</option> </select> </td> </tr> <tr> <td class="textright"><strong>Penciller:</strong><input type="hidden" name="catID[]" value="6" /></td> <td> <select name="creatorID[]"> <option selected="selected" value="0">Choose</option> <option value="67">Paty</option> <option value="282">Studio F</option> <option value="296">Dream Engine</option> <option value="182">Abel, Jack</option> </select> </td> </tr> <tr> <td class="textright"><strong>Cover Art:</strong><input type="hidden" name="catID[]" value="8" /></td> <td> <select name="creatorID[]"> <option selected="selected" value="0">Choose</option> <option value="67">Paty</option> <option value="282">Studio F</option> <option value="296">Dream Engine</option> <option value="182">Abel, Jack</option> </select> </td> </tr> </table> BUT it's not repeating each field based on the $qty number. that's what i need to do. is there a way i can edit the existing code to get this to work? Link to comment https://forums.phpfreaks.com/topic/58857-solved-repeat-form-field-based-on-_post-data/ Share on other sites More sharing options...
Barand Posted July 7, 2007 Share Posted July 7, 2007 I'd lose the hidden fields and put the cat in the index of the qty[] field IE qty[cat] example <?php $cats = array ( 1 => 'Editors', 'Plots', 'Scripts', 'Writers', 'Artists', 'Pencillers', 'Inkers', 'Cover Arts', 'Additional Arts', ); /** * PROCESS THE DATA FROM FORM */ if (isset($_POST['qty'])) { foreach ($_POST['qty'] as $cat => $qty) { if ($qty > 0) { echo "Job $cats[$cat] : $qty required<br>"; } } } /** * INPUT FORM */ echo '<form method="post">'; echo '<table>'; foreach ($cats as $cat => $job) { echo <<<TXT <tr> <td class="textright">How many <strong>$job:</strong></td> <td> <input type="text" style="width:30px;" name="qty[$cat]" value="0" /> </td> </tr> TXT; } echo '</table>'; echo '<input type="submit" name="submit" value="Submit">'; echo '</form>' ?> Link to comment https://forums.phpfreaks.com/topic/58857-solved-repeat-form-field-based-on-_post-data/#findComment-292079 Share on other sites More sharing options...
vynsane Posted July 7, 2007 Author Share Posted July 7, 2007 i need to keep the catID as a hidden input because there's a third step to the form that inserts it into the database. the method you posed does not answer my problem, maybe i'm not making it as clear as it could be... it's pretty complex, so it's hard to describe. the first step of the form is to setup the quantity of each category i need, the second step is to choose the name of the person for each category, and the third step is to the submit the category id and the person's id into the database along with other hidden fields like the user's id that's submitting it and the current date as submission date. i have the first and third step working, the second step is missing a vital ability, which is to display each category with a select box of names next to it based on the quantity posted by the first step - for instance, if "inker" is posted from the initial form as "2" i need to display the "inker" category and select box twice. that's what i need help figuring out, how to display those two items multiple times based on the $qty variable posted from the initial form. Link to comment https://forums.phpfreaks.com/topic/58857-solved-repeat-form-field-based-on-_post-data/#findComment-292100 Share on other sites More sharing options...
vynsane Posted July 7, 2007 Author Share Posted July 7, 2007 could i use the function str_repeat() to do this? that might work... Link to comment https://forums.phpfreaks.com/topic/58857-solved-repeat-form-field-based-on-_post-data/#findComment-292106 Share on other sites More sharing options...
Barand Posted July 7, 2007 Share Posted July 7, 2007 do you mean <?php mysql_connect('localhost'); mysql_select_db('test3'); $cats = array ( 1 => 'Editors', 'Plots', 'Scripts', 'Writers', 'Artists', 'Pencillers', 'Inkers', 'Cover Arts', 'Additional Arts', ); function creatorSelect($cat) { /** * CREATE CREATOR SELECTOR */ $sql = "SELECT id, lname, fname, suffix FROM names ORDER BY lname"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); $str = "<select name='creator[$cat][]'>"; $str .= "<option value=''>-select-</option>"; while ($row = mysql_fetch_row($res)) { $id = array_shift($row); $name = join(' ', $row); $str .= "<option value='$id'>$name</option>"; } $str .= '</select>'; return $str; } /** * PROCESS THE DATA FROM FORM */ if (isset($_POST['qty'])) { echo '<form method="post">'; echo '<table border="1">'; foreach ($_POST['qty'] as $cat => $qty) { if ($qty > 0) { echo "<tr valign='top'><th>$cats[$cat]</th><td> "; for ($i=0; $i<$qty; $i++) echo creatorSelect($cat).'<br>'; echo '</td></tr>'; } } echo '<table><input type="submit" name="submit" value="Submit"></form>'; exit; } /** * INPUT FORM */ echo '<form method="post">'; echo '<table>'; foreach ($cats as $cat => $job) { echo <<<TXT <tr> <td class="textright">How many <strong>$job:</strong></td> <td> <input type="text" style="width:30px;" name="qty[$cat]" value="0" /> </td> </tr> TXT; } echo '</table>'; echo '<input type="submit" name="submit" value="Submit">'; echo '</form>' ?> Link to comment https://forums.phpfreaks.com/topic/58857-solved-repeat-form-field-based-on-_post-data/#findComment-292110 Share on other sites More sharing options...
vynsane Posted July 8, 2007 Author Share Posted July 8, 2007 that was close enough to get me on the right track... awesomeness, thanks! what i ended up doing was use the "str_repeat()" function to display each "job" multiplied by the $_POSTed $qty, and modified your "creator selector" function to suit my needs (i needed to place the commas the correct way, so i had to do it with a couple of "if" statements instead of the way you did it... this is what i came up with: foreach ($_POST['qty'] as $k => $qty) { $catID = $_POST['catID'][$k]; if($qty !=='0') { $jobsQuery = mysql_query("SELECT * FROM Categories WHERE ID = '".$catID."'"); while ($jobs = mysql_fetch_array($jobsQuery)){ $str = "<tr>\n"; $str .= "<td class=\"textright\"><strong>".$jobs['catName'].":</strong><input type=\"hidden\" name=\"catID[]\" value=\"".$jobs['ID']."\" /></td>\n"; $str .= "<td>\n"; $str .= creatorSelect(); $str .= "</select>\n"; $str .= "</td>\n"; $str .= "</tr>\n"; echo "".str_repeat($str, $qty).""; } } } the echo of the str_repeat() function was able to repeat the $str multiplied by the $qty, ending up with my desired result. and the modified select function: // creator select routine function creatorSelect() { $creatorsQuery = mysql_query("SELECT ID, Lname, Fname, suffix FROM names ORDER BY Lname"); $str = "<select name=\"creatorID[]\">\n"; $str .= "<option value=\"0\">Choose</option>\n"; while($creators = mysql_fetch_array($creatorsQuery)) { $str .= "<option value=\"".$creators['ID']."\">"; if($creators['Lname'] !=Null){ $str .= "".$creators['Lname'].", "; } if($creators['Fname'] !=Null){ $str .= "".$creators['Fname'].""; } if($creators['suffix'] !=Null){ $str .= ", ".$creators['suffix'].""; } $str .= "</option>\n"; } $str .= "</select>"; return $str; } // Link to comment https://forums.phpfreaks.com/topic/58857-solved-repeat-form-field-based-on-_post-data/#findComment-292242 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.