cry of war Posted November 30, 2007 Share Posted November 30, 2007 i have a while function that increases $i by one each time ($i++ while this function is still going it populates areas inside of a <input> html tag like so <input type="text" value="" name="try'.$i.'" /> it does this for X amount of times. Now the user press submit after filling in all the information. the page then refreshes on its self and is suppost to input the X amount of items selected. X is determinded by what ever the user puts in to the field before this. What I can'T figure out is how to make it so that the users can have 100's of box yet i dont have to code the php 1 insert at a time I have been looking for the answer for the last week and this is the best example i have came up with so far but its not quite it. <?php $foobar = "blah blah"; $a = "foo"; $b = "bar"; $fooboo = "$a" . "$b"; $fooboo = $$fooboo; echo $fooboo; ?> It does partly what i want it to do make 2 vars into one but in stead i need to make it so i can have a set name such as HELLO and have the "$i" be put on the end then the whole word turned into a var...... I=4 Hello=>Hello$i=>$Hello4 The area with the big blanks is the part im stuck on....... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Add Alchemy</title> </head> <?php ini_set('display_errors', '1'); error_reportIng(E_ALL); include "databaseconnect.php"; include "functionmysqldebug.php"; include "table.php"; /*link to other tables makes it work better instead of EffECTS $result = mysql_query("SELECT * FROM $table6"); if (!is_resource($result)){ echo "\nThis page cannot be displayed because there are no Affinities to add bonuses to."; die; }**/ $result = mysql_query("SELECT * FROM $table6"); if (!is_resource($result)){ echo "\nThis page cannot be displayed because there are no Affinities to add bonuses to."; die; } if (isset($_POST["Update"])) { $Affinity=$_POST["Affinity"]; $NumberofBonuses=$_POST["BonusesNumbers"]; $i=0; $result = mysql_query("SELECT * FROM $table6"); while ($i<$NumberofBonuses) { $i++; $effect=$_POST["effect"."$i"]; $align=$_POST["align"."$i"]; $strength=$_POST["strength"."$i"]; sqlQuery("INSERT INTO $table2 VALUES ('$Affinity','$effect','$align','$strength')", $link, "1"); } } if (!isset($_POST["Load"])){ echo "\nSelect alchemy item you wish to edit:"; echo "\n<FORM action='".$_SERVER['PHP_SELF']."' method='post'>"; echo "\n<SELECT name='Affinity'>"; echo "\n<option label='' value=''>None</OPTION>"; echo "\n<optgroup label='Affinity'>"; $result = mysql_query("SELECT * FROM `$table2` ORDER BY `Name`"); while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { printf("\n<option value='$row[Name]'>$row[Name]</OPTION>"); } echo "\n</OPTGROUP>"; echo "\n</SELECT>"; echo "\n<br>Input number of bonuses you want to give this affinity:<input type='text' value='0' Name='BonusesNumbers' size='2'>"; echo "\n<br><input type='SUBMIT' value='Load Affinity' Name='Load'>"; echo "</form>"; Die; } if (isset($_POST["Load"])) { $Affinity=$_POST["Affinity"]; $NumberofBonuses=$_POST["BonusesNumbers"]; echo "\n<FORM action='".$_SERVER['PHP_SELF']."' method='post'>"; echo "<input type='hidden' value='".$Affinity."' name='Affinity'/>"; $i=0; $maxnumber=$NumberofBonuses; While($i<$maxnumber){ $i++; echo "<table>"; echo "<td>Effect:\n"; echo "<td><select name='effect" .$i. "'>\n"; $result2 = mysql_query("SELECT * FROM $table7"); echo "\n<optgroup label='$table7'>"; $result = mysql_query("SELECT * FROM `$table7` ORDER BY `Name`"); while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { printf("\n<option value='$row[Name]'>$row[Name]</OPTION>"); } echo "\n</OPTGROUP>"; $result2 = mysql_query("SELECT * FROM $table8"); echo "\n<optgroup label='$table8'>"; $result = mysql_query("SELECT * FROM `$table8` ORDER BY `Name`"); while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { printf("\n<option value='$row[Name]'>$row[Name]</OPTION>"); } echo "\n</OPTGROUP>"; $result2 = mysql_query("SELECT * FROM $tabl9"); echo "\n<optgroup label='$table9'>"; $result = mysql_query("SELECT * FROM `$table9` ORDER BY `Name`"); while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { printf("\n<option value='$row[Name]'>$row[Name]</OPTION>"); } echo "\n</OPTGROUP>"; echo "</select></td>"; echo "<td>Effect Strength:\n"; echo "<select name='align" .$i. "'>\n"; echo "<option value='1'>+</option>\n"; echo "<option value='2'>-</option>\n"; echo "</select></td>\n"; echo "<td><input type='text' name='strength" .$i. "' value='' maxlength='10' size='8'></td>\n"; echo "</table>"; echo "<input type='hidden' value='".$NumberofBonuses."' name='BonusesNumbers'/>"; } echo "<input type='submit' value='Update ".$Affinity."' name='Update'/>"; echo "<form>"; } ?> <body> </body> </html> Link to comment https://forums.phpfreaks.com/topic/79575-insert-help/ Share on other sites More sharing options...
GingerRobot Posted November 30, 2007 Share Posted November 30, 2007 Whilst you could carry on with your current method, i suggest you use arrays. Since your code is long, ill just give a quick example: <?php if(isset($_POST['submit'])){ foreach($_POST['qty'] as $k => $v){ if(ctype_digit($v) && $v > 0){ echo 'The customer ordered '.$v.' units of the product with ID: '.$k.'<br />'; } } } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <?php for($i=0;$i<5;$i++){ echo 'Quantity of product '.$i.' <input type="text" name="qty['.$i.']" /><br />'; } ?> <input type="submit" name="submit" value="Submit" /> </form> Take a look at how that works. Once you understand that, rewrite your current stuff to make use of arrays - it's much easier. Link to comment https://forums.phpfreaks.com/topic/79575-insert-help/#findComment-403076 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.