Forum Moderators: coopster
All values of the list are chosen from an mySql query...
When your script generates the forms, do something like this:
<Form action="action" method="method">
<?
$all_ids = array();
for($x = 0; $x < sizeof($what_ever_array_you_are_getting_the_id_numbers_from); $x++) {
?>
$curid = $array_from_the_loop[$x];
?>
<input type="text" name="qty-$curid" value=""><br>
<?
array_push($all_ids, $curid);
}
$id_list = join("-", $all_ids);
echo "<input type=\"hidden\" name=\"id_list\" value=\"$id_list\">
?>
<input type="submit" value="submit">
</form>
-----
On the receiving script
-----
<?
$idlist = $HTTP_POST_VARS["id_list"];
$idarray = split("-", $idlist);
$l = mysql_connect(put connect info here);
mysql_select_db("put database here");
for($x = 0; $x < sizeof($idarray); $x++) {
$curindex = $idarray[$x];
$curqty = $HTTP_POST_VARS["qty-$curid"];
mysql_query("update table set quantity='$curqty' where id='$curindex'");
}
?>
Now this is a very broken verion i just wrote for you really quick you are going to have understand what I am doing with it so that you can modify it. I assume this is for a shopping cart, you gotta put a condition if its zero then to remove that item all together, and also, is each item a row? its not in my cart, the whole cart is in one variable divided by special cars which means you would need code to goin an update this string with the correct quantites and what not, then re insert the cookie to the browser or the the session or however you are saving the cart...
PS, if you are having a really hard time with this, it might be a little out of your leage at this point, but i have no idea what your skill level is so who knows.
that should do it... i had some code out side of the <? thingies ;)
Strangely, I was just about to post the exact same question. I was concerned however, that being a relative newbie, and this being my first shopping cart attempt, that i would not understand the answer, and...
I don't understand the answer :-((
Here is my code so far anyway (apologies if it looks daft to a more experienced person)
<?php
// cookie stuff goes here
// db connect stuff here
// is the user updating the cart or wanting to mail the form?
if ($csubmit=="update cart") {
// is someone adding their contact details?
$putname = mysql_query("UPDATE cart SET UserName='$contact_name' WHERE UserID='$userinfo'");
} else if ($csubmit=="place order"){
// heres where we will send the info to the mailer if they want to submit it
}
// get all the users info...
$getuserdata = mysql_query("SELECT * FROM cart WHERE UserID='$userinfo'");
// get the first line
$getuserdata_array = mysql_fetch_array($getuserdata);
?>
<html>
<body>
<?php
// if a username exists, welcome them
if (isset($getuserdata_array[UserName])) {
print('<p>Welcome, '.$getuserdata_array[UserName].'</p>');
} else {
print('<p>Welcome, Guest</p>');
}
print('<p>These are the items in your shopping cart...<br /><br />');
// get all the items out of the cart
if ($myproducts = mysql_fetch_array($getuserdata)) {
// start table and form here
print('<form action="cart.php" method="get"><table class="SpecContent" width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">');
print('<tr><td><b>Item</b></td><td><b>Description</b></td><td><b>Price</b></td><td><b>Qty</b></td><td><b>Remove Item</b></td></tr>');
do {
print('<tr>
<td>'.$myproducts[Manufacturer].' '.$myproducts[Model].'</td>
<td>'.$myproducts[prodesc].'</td><td>'.$formattedprice.'</td>
<td><input name="'.$myproducts[ID].'" type="text" id="qtybox'.$myproducts[ID].'" value="'.$myproducts[Qty].'" size="5"></td>
<td><input type="radio" name="radiobutton" value="radiobutton" id="radiobutton"></td>
</tr>');
} while ($myproducts = mysql_fetch_array($getuserdata));
// end table and form
print('</table><br /><p>
Your name: <input name="contact_name" type="text" id="contact_name" value="'.$getuserdata_array[UserName].'"><br /><br />
<input type="submit" name="csubmit" value="update cart"> <input type="submit" name="csubmit" value="place order">
</p>
</form>');
} else {
echo "Your shopping cart is currently empty";
}
</body>
</html>
As you can see I have a quantity box and a delete radio button (or perhaps this should be a checkbox) but I don't know how make these update the right record. Everything else works OK.
Have I gone about this completely the wrong way?
Helen.
what you are doing is to take the two values (from my initial 'sql query array' that corresponds to itemID and qty and bind them with - into one array? which is passed to the recieving script. So if my SQL array is
while($query_data= mysql_fetch_array($result))
{
$itemName=$query_data["ItemName"];
$itemPrice=$query_data["ItemPrice"];
$itemQuantity=$query_data["qty"];
$itemcolor=$query_data["color"];
$itemId=$query_data["itemId"];
here we need to 'build' the new array combined of qty and item#? in fact your code should be integrated here...
}
itemID:
$all_ids = array();
BECOMES:
$all_ids=$query_data["ItemName"]
qty to:
$curid = $array_from_the_loop[$x];
BECOMES:
$curid = $query_data["qty"]
I did not understand the following?: (but not needed I think cause I have my while loop?)
sizeof($what_ever_array_you_are_getting_the_id_numbers_from)
Hope I have got it...
Right, I have figured out how to let the user adjust the quantity, and it works! Here is the code - the only new bit added is after the comment //update the form quantities
<?php
// cookie stuff goes here
// db connect stuff here
// is the user updating the cart or wanting to mail the form?
if ($csubmit=="update cart") {
// is someone adding their contact details?
$putname = mysql_query("UPDATE cart SET UserName='$contact_name' WHERE UserID='$userinfo'");
//update the form quantities
$UpdateArray = $_POST; // get all the variables posted with the form
unset($UpdateArray[contact_name]); // This removes the element from the array
unset($UpdateArray[csubmit]); // This removes the element from the array
foreach ($UpdateArray as $key => $value) {
$putqty = mysql_query("UPDATE cart SET Qty='$value' WHERE ID='$key'");
}
} else if ($csubmit=="place order"){
// heres where we will send the info to the mailer if they want to submit it
}
// get all the users info...
$getuserdata = mysql_query("SELECT * FROM cart WHERE UserID='$userinfo'");
// get the first line
$getuserdata_array = mysql_fetch_array($getuserdata);
?>
<html>
<body>
<?php
// if a username exists, welcome them
if (isset($getuserdata_array[UserName])) {
print('<p>Welcome, '.$getuserdata_array[UserName].'</p>');
} else {
print('<p>Welcome, Guest</p>');
}
print('<p>These are the items in your shopping cart...<br /><br />');
// get all the items out of the cart
if ($myproducts = mysql_fetch_array($getuserdata)) {
// start table and form here
print('<form action="cart.php" method="get"><table class="SpecContent" width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">');
print('<tr><td><b>Item</b></td><td><b>Description</b></td><td><b>Price</b></td><td><b>Qty</b></td><td><b>Remove Item</b></td></tr>');
do {
print('<tr>
<td>'.$myproducts[Manufacturer].' '.$myproducts[Model].'</td>
<td>'.$myproducts[prodesc].'</td><td>'.$formattedprice.'</td>
<td><input name="'.$myproducts[ID].'" type="text" id="qtybox'.$myproducts[ID].'" value="'.$myproducts[Qty].'" size="5"></td>
<td><input type="radio" name="radiobutton" value="radiobutton" id="radiobutton"></td>
</tr>');
} while ($myproducts = mysql_fetch_array($getuserdata));
// end table and form
print('</table><br /><p>
Your name: <input name="contact_name" type="text" id="contact_name" value="'.$getuserdata_array[UserName].'"><br /><br />
<input type="submit" name="csubmit" value="update cart"> <input type="submit" name="csubmit" value="place order">
</p>
</form>');
} else {
echo "Your shopping cart is currently empty";
}
</body>
</html>
My only problem now is how I can enable the user to delete an item! I will get working on it...
Helen
There is 2 parts to this process, the code that generates the form for the user to input the quantities and the 2nd part wich is the script that takes the inputed values and saves them to the database.
im assuming the while loop you posted is regarding the 2nd of the two processes. If that is the case, you are going about it wrong. Look at the code i gave you... in the first 1 part (form generation) i genereate a variable that most likely will look something like this: 1-2-3-4-5-6 assuming there are 6 items in the cart.
On the other side, we call a split function the make that into an array that looks like this:
Array[0] = 1
Array[1] = 2
Array[2] = 3 and so on...
So we call a for loop the run through the length of the array, which is why $x < sizeof($Array);
durring this loop:
$curindex = $idarray[$x];
$curqty = $HTTP_POST_VARS["qty-$curindex"];
and then do what you need with that quantity number like save it to a DB or something.
then end the loop
}
Also, its more complex for me than just saving to to a database, you see in my shopping cart the orders table is layed out like this:
ID ¦ ORDERNUM ¦ CART ¦ BILLING NAME ¦ BADDR1 ¦ BADDR2 BLA BLA BLA BLA BAL
the cart column holds the shopping cart information for every order all in one cell... also, while the person is browsing the site, the cart is all saved in 1 variable called $cart in their cookies.
A cart variable would look like this:
SKUNUM~ITEM_NAME~ITEM OPTIONS FROM PULLDOWNS~2~20.99&^&SKUNUM~ITEM_NAME~ITEM OPTIONS FROM PULLDOWNS~2~20.99&^&SKUNUM~ITEM_NAME~ITEM OPTIONS FROM PULLDOWNS~2~20.99&^&SKUNUM~ITEM_NAME~ITEM OPTIONS FROM PULLDOWNS~2~20.99
My script has to split this up into arrays before i can work with it. First it splits by the &^& to get a item array then it splits each index of the item array by the ~ to get the itemdata array.
Once I have that iforation i can update the quantity which is the field right before price or i can delete that whole item, then rebuild the array then re save it to the cookie instead of saving to the database....
You dont really need to save it to the database until the customer has PLACED THE ORDER, otherwise you are just wasting space in the DB!
Hope fully you can understand, thats as in depth as i can go with it... if not, just keep beating on it, you'll get it eventually.