Forum Moderators: coopster
Thanks,
Christi
<?php
$table_name_people = "run1_people";
$table_name_orders = "run1_orders";
// process form
if ($submit) {
$sql = "INSERT INTO `$table_name_people`
(`id`, `name`, `screenname`, `phone`, `payment_method`)
VALUES
('', '$name','$screenname','$phone', '$payment_method')";
$result = mysql_query($sql,$connection) or die("Couldn't add customer record");
$id_people = mysql_insert_id($connection);
foreach ($size as $key => $value){
$sql = "INSERT INTO `$table_name_orders`
(`id`, `id_people`, `design`, `size`, `color`, `type`, `qty`)
VALUES
('', '$id_people','$design','$size', '$color', '$type', '$qty')";
$result = mysql_query($sql,$connection) or die("Couldn't add order record");
echo "<CENTER>Thank you! Your order has been entered. <BR><P><a href=\"/tshirt_order.php\">Order More</A><BR><P><a href=\"http://somesite.net\"><-- Back to Homepage</A>!\n";
}
mysql_close();
} else {
// display form
?>
<FORM METHOD=POST ACTION="<?php echo $PHP_SELF?>">
<TABLE cellpadding=0 cellspacing=0 border=0>
<TR>
<TD align=right>Your Name: </TD>
<TD><INPUT TYPE="text" NAME="name" size=30 maxlength=30></TD>
</TR>
<TR>
<TD align=right>Screen Name: </TD>
<TD><INPUT TYPE="text" NAME="screenname" size=30 maxlength=40></TD>
</TR>
<TR>
<TD align=right>Your Phone: </TD>
<TD><INPUT TYPE="text" NAME="phone" size=30 maxlength=15></TD>
</TR>
<TR>
<TD><IMG SRC="http://somesite.net/album_pic.php?pic_id=766" width=300></TD>
<TD><B>Logo</B><BR>
<!--1-->
<input type=hidden name="design[]" value="PowLogo">
<SELECT NAME="size[]">
<OPTION value=''>Size--</OPTION>
<OPTION value='Small'>Small</OPTION>
<OPTION value='Medium'>Medium</OPTION>
<OPTION value='Large'>Large</OPTION>
<OPTION value='XLarge'>XLarge</OPTION>
</SELECT>
<SELECT NAME="color[]">
<OPTION value=''>Color--</OPTION>
<OPTION value='White'>White</OPTION>
<OPTION value='Black'>Black</OPTION>
<OPTION value='Blue'>Dark Blue</OPTION>
</SELECT>
<SELECT NAME="type[]">
<OPTION value=''>Type--</OPTION>
<OPTION value='ShortSlv'>Short Slv</OPTION>
<OPTION value='LongSlv'>Long Slv</OPTION>
<OPTION value='BabyDoll'>Baby Doll</OPTION>
</SELECT>
<SELECT NAME="qty[]">
<OPTION value=''>Qty--</OPTION>
<OPTION value='1'>1</OPTION>
<OPTION value='2'>2</OPTION>
<OPTION value='3'>3</OPTION>
<OPTION value='4'>4</OPTION>
<OPTION value='5'>5</OPTION>
<OPTION value='6'>6</OPTION>
<OPTION value='7'>7</OPTION>
<OPTION value='8'>8</OPTION>
<OPTION value='9'>9</OPTION>
<OPTION value='10'>10</OPTION>
</SELECT><BR>
<!--/1-->
<!--2-->
<input type=hidden name="design[]" value="PowLogo2">
<SELECT NAME="size[]">
<OPTION value=''>Size--</OPTION>
<OPTION value='Small'>Small</OPTION>
<OPTION value='Medium'>Medium</OPTION>
<OPTION value='Large'>Large</OPTION>
<OPTION value='XLarge'>XLarge</OPTION>
</SELECT>
<SELECT NAME="color[]">
<OPTION value=''>Color--</OPTION>
<OPTION value='White'>White</OPTION>
<OPTION value='Black'>Black</OPTION>
<OPTION value='Blue'>Dark Blue</OPTION>
</SELECT>
<SELECT NAME="type[]">
<OPTION value=''>Type--</OPTION>
<OPTION value='ShortSlv'>Short Slv</OPTION>
<OPTION value='LongSlv'>Long Slv</OPTION>
<OPTION value='BabyDoll'>Baby Doll</OPTION>
</SELECT>
<SELECT NAME="qty[]">
<OPTION value=''>Qty--</OPTION>
<OPTION value='1'>1</OPTION>
<OPTION value='2'>2</OPTION>
<OPTION value='3'>3</OPTION>
<OPTION value='4'>4</OPTION>
<OPTION value='5'>5</OPTION>
<OPTION value='6'>6</OPTION>
<OPTION value='7'>7</OPTION>
<OPTION value='8'>8</OPTION>
<OPTION value='9'>9</OPTION>
<OPTION value='10'>10</OPTION>
</SELECT><BR>
<!--/2-->
</TD>
</TR>
<TR>
<TD valign=top align=right>Payment Method: </TD>
<TD><INPUT TYPE="radio" NAME="payment_method" value="Cash"> Cash <INPUT TYPE="radio" NAME="payment_method" value="Check"> Check <INPUT TYPE="radio" NAME="payment_method" value="PayPal"> PayPal</TD>
</TR>
<TR>
<TD valign=top> </TD>
<TD valign=top><INPUT TYPE="hidden" name="year" value="2003">
<INPUT TYPE="submit" name="submit" value="Submit Form"></TD>
</TR>
</TABLE>
</FORM>
<P>
<?php
} // end if
?>
[edited by: jatar_k at 5:42 pm (utc) on Oct. 31, 2003]
[edit reason] generalized [/edit]
Warning: Invalid argument supplied for foreach() in /home/httpd/vhosts/somesite.net/httpdocs/tshirt_order.php on line 42
LIne forty 2 is :
foreach ($size as $key => $value){
[edited by: jatar_k at 12:43 am (utc) on Nov. 1, 2003]
[edit reason] generalized sitename [/edit]
and saw that they used $cats
$size is simply one of the variables in each row - figureing as long as there was another instance of the size it should continue recording them.
Like I said - I am new and am trying to learn by studing and addapting other's code.
If there is a better way to approch this please let me know :)
-Christi
there are three shirt styles and for each they can select up to four combinations of size, color, type and qty so it's something like:
Design 1
hidden design, select size, select color, select type, select qty
hidden design, select size, select color, select type, select qty
hidden design, select size, select color, select type, select qty
hidden design, select size, select color, select type, select qty
Design 2
hidden design, select size, select color, select type, select qty
hidden design, select size, select color, select type, select qty
hidden design, select size, select color, select type, select qty
hidden design, select size, select color, select type, select qty
Design 3
hidden design, select size, select color, select type, select qty
hidden design, select size, select color, select type, select qty
hidden design, select size, select color, select type, select qty
hidden design, select size, select color, select type, select qty
SUBMIT>
I figure as long as there is a value for $size that "row" should be saved as an actual row in the db.
Am I off in my logic?
-Christi
<Am I off in my logic?>
Yes :-).
You're wanting to record the value that is selected, right? Then you want to remove the array brackets [] as jatar_k mentioned.
You said that you're receiving an "Invalid argument supplied" error when using foreach() if you remove the brackets.
Is the number of SELECT boxes variable or static?
If it's static, then call your variables $design1, $size1, $color1, $type1, $qty1, and then again with $design2, and so on for however many you have.
If it's variable, then you'll have to know how many in advance so you can create the HTML form correctly, then you can create $design[1], $size[1], etc, and use arrays.
<?php
for($x = 0; $x < $Total; ++$x)
{
print "<SELECT NAME=\"$Design[$x]\">\n";
etc...
}
?>
Then you can foreach() it on the processing side.
Or, without knowing how many in advance, there are other ways. You could have a single blank set of SELECT boxes, and when it's full, the user hits "Next", and they are presented with a second set, and on and on until they hit "Finish". You could do this with PHP on the server-side, or with JavaScript on the client-side (maybe a little more difficult to code, but faster for the user). But this goes beyond the scope of your question :-).
Hope this helps.
I changed [] to numbered sets ie design1 --- next set starts with design2
Now I get this error:
Warning: Invalid argument supplied for foreach() in /home/httpd/vhosts/site.net/httpdocs/tshirt_order.php on line 42
42 still being the foreach - I dont understand what is supposed to be specified in the () of the foreach - going to research this further now
Christi <- feeling dense
Since the number of sets of SELECT boxes is static, you don't need foreach() since you would have a static number of INSERT statements:
<?php
mysql_query("INSERT INTO TableOrders (id,id_people,design,size,color,type,qty) VALUES ('','$id_people','$design1','$size1','color1','type1','qty1')");
mysql_query("INSERT INTO TableOrders (id,id_people,design,size,color,type,qty) VALUES ('','$id_people','$design2','$size2','color2','type2','qty2')");
# etc...
?>
Obviously this isn't exactly right.. I don't even know if it would work for your table. But the idea is that if you have a static number of SELECT boxes, then you have one INSERT statement for each one, so no foreach() necessary.
On the other hand, you could use foreach() the way you intended.. though then you would name your SELECT boxes design[1] and design[2] instead of design1 and design2, and so on.
In the example we're looking at I would probably use "name_1", "name_2", "size_1", "size_2", etc. and an ereg something like:
foreach ($_POST as $key => $val) {
if (ereg ("^name_(.*)", $key, $regs);
$idx = $regs[1];
$name = $val;
$size = ${"size_$idx"};
doQuery ("INSERT INTO table (name, size) VALUES ('$name', '$size')");
}