xyn Posted October 26, 2006 Share Posted October 26, 2006 [quote author=HuggieBear link=topic=112678.msg457438#msg457438 date=1161790534]ok, here you go, this is the code for page 1. If you look at the bottom of this page, there's an example of page one and page 2.[code]<?php// start a sessionsession_start();// clear the cart if the action is clearif ($_GET['action'] == "clear"){ unset($_SESSION['items']);}// if a link has been clickedif (isset($_GET['product'])){ if (!isset($_SESSION['items'][$_GET['product']])){ // if it the first time it's selected, quantity is 1 $_SESSION['items'][$_GET['product']] = 1; } else { // if it's been selected before, increase it by 1 $_SESSION['items'][$_GET['product']]++; }}?><a href="products.php?product=1">Product 1</a><br><a href="products.php?product=2">Product 2</a><br><a href="products.php?product=3">Product 3</a><br><a href="products.php?product=4">Product 4</a><br><a href="products.php?product=5">Product 5</a><br><br><a href="products.php?action=clear">Clear Cart</a><br><br><?php// This is just a var dump so you can see things incrementingecho "<pre>";var_dump($_SESSION['items']);echo "</pre>";?>[/code]You can try page 1 [url=http://dizzie.co.uk/php/products.php]here...[/url] and page 2 [url=http://dizzie.co.uk/php/products2.php]here...[/url]Once you have all of these id's in an array, you can use that array to get the descriptions from the database for on the order form page.RegardsHuggie[/quote]Although if you want the users "CART" to be stored on their computer for a certain timeor never expires then i would suggest using cookies.www.php.net/cookies Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114730 Share on other sites More sharing options...
stualk Posted October 26, 2006 Author Share Posted October 26, 2006 Huggie,Yes, Yes, Yes! Works perfectly now, thank you, thank you, thank you!!! Let me buy you a drink...!!!Now for the last bit - the contents of the form then get submitted to a specified email account. Before I was using arrays I just had a long list of input boxes, each numbered like below. DESCRIPTION CODE QTY$desc1 $code1 $qty1$desc2 $code2 $qty2$desc3 $code3 $qty3etcNow that i'm using an array how do I do this? Do I need to use the items field along with the id? Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114747 Share on other sites More sharing options...
HuggieBear Posted October 26, 2006 Share Posted October 26, 2006 There's no need for input boxes or a form at all.Your order page is dynamically generated from the database, based on what existed in the array, so generate the email dynamically too.Once you have your order ready, hit 'purchase' or whatever you want on there and process it like follows (Most of the code is similar to the page we've already seen):[code]<?php// start the sessionsession_start();// put the array of id's into a string for use in the sql query$sqlitems = implode(",", array_keys($_SESSION['items']));// connect to the databaseinclude_once('database.php');// get the results from the database (the same query as earlier)$sql = "SELECT id, name, FROM products WHERE id IN ($sqlitems) ORDER BY id";$result = mysql_query($sql) or die ("<b>Unable to run :</b> $sql<br>\n<br>\n<b>Because:</b> " . mysql_error());// start the email$to = "me@mydomain.com"; // may need to escape the @$subject = "Order from your domain";// generate the main message body of the email$body = "Item\t\tQuantity\n";// put a row for each order line into the body of the emailwhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $body =. "{$row['name']}\t\t{$_SESSION['items'][$row['id']]}\n";}// send the mailmail($to, $subject, $body);?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114758 Share on other sites More sharing options...
stualk Posted October 26, 2006 Author Share Posted October 26, 2006 Problem is my order form already contains the client address and contact details. The hyperlink that takes the product details to the order form also takes the client details, so I need these details to be part of the email at the top really. Here's the content of the email I was using before, do I just put this beside the $body tag?:An order has been received. The order details are as follows:NAME: $contact_nameCOMPANY: $company_nameADDRESS: $addressTOWN: $townCOUNTY: $countyPOST CODE: $post_codeTELEPHONE: $telephoneMOBILE: $mobileCOMMENTS: $comments Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114769 Share on other sites More sharing options...
HuggieBear Posted October 26, 2006 Share Posted October 26, 2006 Can I see the actual code.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114792 Share on other sites More sharing options...
stualk Posted October 26, 2006 Author Share Posted October 26, 2006 Ok, previously I was using the code below. My order form was submitting the details to a file called 'order_form_submit.php' which is here...[code]<?phpinclude "mime_mail.inc";# create object instance$mail = new mime_mail;# set all data slots$mail->from = "$email";$mail->to = "MY EMAIL ADDRESS HERE";$mail->subject = "Order from the website";$mail->body = "The following is an order from the website.The order details are as follows:NAME: $contact_nameCOMPANY: $company_nameADDRESS: $addressTOWN: $townCOUNTY: $countyPOST CODE: $post_codeTELEPHONE: $telephoneMOBILE: $mobileCOMMENTS: $commentsDESCRIPTION CODE QTY$brand_name $product_name $item_number $items";# send email$mail->send();print("<meta http-equiv=\"refresh\" content=\"1;URL=order_form_thank_you.php\">");?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114798 Share on other sites More sharing options...
HuggieBear Posted October 26, 2006 Share Posted October 26, 2006 ok, I'm not 100% certain how that's going to work, as I've not used classes before, but you could try adding more than one item to the body of the email and see how it goes, for example:[code]<?php// start the sessionsession_start();// put the array of id's into a string for use in the sql query$sqlitems = implode(",", array_keys($_SESSION['items']));// connect to the databaseinclude_once('database.php');// get the results from the database (the same query as earlier)$sql = "SELECT id, name, FROM products WHERE id IN ($sqlitems) ORDER BY id";$result = mysql_query($sql) or die ("<b>Unable to run :</b> $sql<br>\n<br>\n<b>Because:</b> " . mysql_error());include "mime_mail.inc";# create object instance$mail = new mime_mail;# set all data slots$mail->from = "$email";$mail->to = "MY EMAIL ADDRESS HERE";$mail->subject = "Order from the website";$mail->body = "The following is an order from the website.The order details are as follows:NAME: $contact_nameCOMPANY: $company_nameADDRESS: $addressTOWN: $townCOUNTY: $countyPOST CODE: $post_codeTELEPHONE: $telephoneMOBILE: $mobileCOMMENTS: $comments";$mail->body = . "\n\nItem\t\tQuantity\n";// put a row for each order line into the body of the emailwhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $mail->body =. "{$row['name']}\t\t{$_SESSION['items'][$row['id']]}\n";}# send email$mail->send();print("<meta http-equiv=\"refresh\" content=\"1;URL=order_form_thank_you.php\">");?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114807 Share on other sites More sharing options...
stualk Posted October 26, 2006 Author Share Posted October 26, 2006 It's not liking that. It is only emailing through the last reference to $mail->bodyWhen I took out the order details and just left in the client contact details they came through ok.Seems it's just a problem displaying all the info in one email.Could we not declare everything as variables before we get to the mail->body bit and then when we get to mail->body just call in the variables?:$mail->body = "$contact_details$order_details";I guess that's complicating matters a little. There must be another way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114820 Share on other sites More sharing options...
HuggieBear Posted October 26, 2006 Share Posted October 26, 2006 Better idea, based on what you've said...[code]<?php// start the sessionsession_start();// put the array of id's into a string for use in the sql query$sqlitems = implode(",", array_keys($_SESSION['items']));// connect to the databaseinclude_once('database.php');// get the results from the database (the same query as earlier)$sql = "SELECT id, name, FROM products WHERE id IN ($sqlitems) ORDER BY id";$result = mysql_query($sql) or die ("<b>Unable to run :</b> $sql<br>\n<br>\n<b>Because:</b> " . mysql_error());include "mime_mail.inc";# create object instance$mail = new mime_mail;# set all data slots$mail->from = "$email";$mail->to = "MY EMAIL ADDRESS HERE";$mail->subject = "Order from the website";$body = "The following is an order from the website.The order details are as follows:NAME: $contact_nameCOMPANY: $company_nameADDRESS: $addressTOWN: $townCOUNTY: $countyPOST CODE: $post_codeTELEPHONE: $telephoneMOBILE: $mobileCOMMENTS: $comments";$body =. "\n\nItem\t\tQuantity\n";// put a row for each order line into the body of the emailwhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $body =. "{$row['name']}\t\t{$_SESSION['items'][$row['id']]}\n";}$mail->body = $body;# send email$mail->send();print("<meta http-equiv=\"refresh\" content=\"1;URL=order_form_thank_you.php\">");?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114827 Share on other sites More sharing options...
stualk Posted October 26, 2006 Author Share Posted October 26, 2006 No that does the same, it just returns the last reference to $body instead of all three.I've just reverted to my original code (which is below) and it works other than where the product details should appear it displays the word "Array"Maybe we can put the array details in here and that will fix the problem? It's definitely carrying all the values through it's just not displaying them.[code]<?phpinclude "mime_mail.inc";# create object instance$mail = new mime_mail;# set all data slots$mail->from = "$email";$mail->to = "MY EMAIL ADDRESS HERE";$mail->subject = "Order from the website";$mail->body = "The following is an order from the website.The order details are as follows:NAME: $contact_nameCOMPANY: $company_nameADDRESS: $addressTOWN: $townCOUNTY: $countyPOST CODE: $post_codeTELEPHONE: $telephoneMOBILE: $mobileCOMMENTS: $commentsDESCRIPTION CODE QTY$brand_name $product_name $item_number $items";# send email$mail->send();print("<meta http-equiv=\"refresh\" content=\"1;URL=order_form_thank_you.php\">");?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114840 Share on other sites More sharing options...
HuggieBear Posted October 26, 2006 Share Posted October 26, 2006 My bad, made a major typo! I had [code=php:0]$body =. "value";[/code] I should have had [code=php:0]$body .= "value";[/code] I had the period and the equals the wrong waya around, I wasn't actually concatenating anything... Idiot :-[Try again with this code:[code]<?php// start the sessionsession_start();// put the array of id's into a string for use in the sql query$sqlitems = implode(",", array_keys($_SESSION['items']));// connect to the databaseinclude_once('database.php');// get the results from the database (the same query as earlier)$sql = "SELECT id, name, FROM products WHERE id IN ($sqlitems) ORDER BY id";$result = mysql_query($sql) or die ("<b>Unable to run :</b> $sql<br>\n<br>\n<b>Because:</b> " . mysql_error());include "mime_mail.inc";# create object instance$mail = new mime_mail;# set all data slots$mail->from = "$email";$mail->to = "MY EMAIL ADDRESS HERE";$mail->subject = "Order from the website";$body = "The following is an order from the website.The order details are as follows:NAME: $contact_nameCOMPANY: $company_nameADDRESS: $addressTOWN: $townCOUNTY: $countyPOST CODE: $post_codeTELEPHONE: $telephoneMOBILE: $mobileCOMMENTS: $comments";$body .= "\n\nItem\t\tQuantity\n";// put a row for each order line into the body of the emailwhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $body .= "{$row['name']}\t\t{$_SESSION['items'][$row['id']]}\n";}$mail->body = $body;# send email$mail->send();print("<meta http-equiv=\"refresh\" content=\"1;URL=order_form_thank_you.php\">");?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114846 Share on other sites More sharing options...
stualk Posted October 26, 2006 Author Share Posted October 26, 2006 That works perfectly, you are the man!! A crate of Budweiser is on it's way to you in the post! (Is that what you're drinking in your picture? :-DThanks mate, I owe you one. Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-114851 Share on other sites More sharing options...
HuggieBear Posted October 26, 2006 Share Posted October 26, 2006 It is indeed a Bud, and you're more than welcome.Glad to have been able to help.RegardsRich 8) Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-115123 Share on other sites More sharing options...
stualk Posted October 27, 2006 Author Share Posted October 27, 2006 Morning!Just spotted a couple of minor problems with this. One is that if you add a product to the order form and then change the quantity, then go back and add another product, it puts all the quantities you changed back to 1. Is there any way we can make it so that it does nothing with the quantity at all and we'll let the user specify their quantity?The other thing is that if you add a few products to the form and add your quantities just before you submit the form it uses the quantities specified when the product was added to the form (i.e. 1) rather than the quantity you changed it to.Are these two things fairly straight forward to sort out? Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-115280 Share on other sites More sharing options...
HuggieBear Posted October 27, 2006 Share Posted October 27, 2006 Yes, they aren't problems, I just hadn't written those bits yet. If you think you understand some of the code, then why not have a go at those bits yourself and see how you get on.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-115290 Share on other sites More sharing options...
stualk Posted October 27, 2006 Author Share Posted October 27, 2006 I already have but failed miserably!I tried taking the if statements out regarding the quantity (code below) but that didn't work. I also tried changing the quantity in this code from 1 to 0 and even tried putting nothing at all but none of this works.Can't think what else to try. I think i've almost done the right thing, but not quite![code]// if a link has been clickedif (isset($_GET['id'])){ if (!isset($_SESSION['items'][$_GET['id']])){ // if it the first time it's selected, quantity is 1 $_SESSION['items'][$_GET['id']] = 1; } else { // if it's been selected before, increase it by 1 $_SESSION['items'][$_GET['id']]++; }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-115293 Share on other sites More sharing options...
HuggieBear Posted October 27, 2006 Share Posted October 27, 2006 OK, it's always good to start with a plan, so how about this. On all your product pages you allow people to add the product to the order form by selecting a link. We have this in place already. They can't decrease the quantity from these pages, they have to go to the order form for that, is that a fair assumption?I think despite this being an order form, it should be treated like a shopping cart, with the same principles.Then from the order form page, they can remove specific items, remove all items, or change the quantity for individual items?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-115299 Share on other sites More sharing options...
stualk Posted October 27, 2006 Author Share Posted October 27, 2006 Yes that's a fair assumption. The quantity is dealt with on the order page. It would be good if we could have an option to remove individual items from the order form but it's not crucial. The quantity issue is the main thing but if there's a way we can have a 'remove' option in a column beside each product as well that could be useful.I have already put in a 'clear form' option and this works fine. Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-115300 Share on other sites More sharing options...
HuggieBear Posted October 27, 2006 Share Posted October 27, 2006 Can you possibly provide me the complete code that you're using for the order page now.I can work with that.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-115371 Share on other sites More sharing options...
stualk Posted October 27, 2006 Author Share Posted October 27, 2006 Well you did ask...! :-D[code]<?phpif ($action =="" || $action=="default") {print("<form name=\"form1\" method=\"post\"><input type=\"hidden\" name=\"action\" value=\"addscript\" size=\"20\">");if ($action =="" || $action=="default") {$NumRows=mysql_query("SELECT * FROM customers") or die("Sorry Couldn't Complete Query_1");$NumRows=mysql_num_rows($NumRows); if ($NumRows == "0") {echo("<br><br><br><b>SORRY, PLEASE GO BACK AND TRY AGAIN.</b>");}else {$Result = mysql_query("SELECT * FROM customers where id='$clientid'") or die("Sorry Couldn't Complete Query_2"); $id = mysql_result($Result,0,0); $company_name = mysql_result($Result,0,3); $contact_name = mysql_result($Result,0,4); $address = mysql_result($Result,0,5); $town = mysql_result($Result,0,6); $county = mysql_result($Result,0,7); $post_code = mysql_result($Result,0,8); $email = mysql_result($Result,0,9); $telephone = mysql_result($Result,0,10); $mobile = mysql_result($Result,0,11);}if ($NumRows == "0") { echo ("<b>Sorry, please go back and try again.</b><br><br>");}else {$NumRows = mysql_query("SELECT * FROM customers where id='$clientid'") or die("Sorry Couldn't Complete Query_3");$NumRows = mysql_num_rows($NumRows);$Result = mysql_query("SELECT * FROM customers where id='$clientid'") or die("Sorry Couldn't Complete Query_4"); for ($i=1; $i<=$NumRows; $i++) { if ($row = mysql_fetch_array($Result)) { $id = $row["id"]; $company_name = $row["company_name"]; $contact_name = $row["contact_name"]; $address = $row["address"]; $town = $row["town"]; $county = $row["county"]; $post_code = $row["post_code"]; $email = $row["email"]; $telephone = $row["telephone"]; $mobile = $row["mobile"]; }}} print(" <table width='580' border='0' cellspacing='0' cellpadding='0'> <tr> <td width='25%'><b><font face='Verdana' size='2'>Name: </font></b></td> <td colspan='3'><input type='text' name='contact_name' value='$contact_name' size='50'></td> </tr> <tr> <td width='25%'><b><font face='Verdana' size='2'>Company Name: </font></b></td> <td colspan='3'><input type='text' name='company_name' value='$company_name' size='50'></td> </tr> <tr valign='top'> <td width='25%'><b><font face='Verdana' size='2'>Address: </font></b></td> <td colspan='3'><textarea name=\"address\" cols=\"40\" rows=\"4\" wrap=\"VIRTUAL\">$address</textarea></td> </tr> <tr> <td width='25%'><b><font face='Verdana' size='2'>Town: </font></b></td> <td colspan='3'><input type='text' name='town' value='$town' size='25'></td> </tr> <tr> <td width='25%'><b><font face='Verdana' size='2'>County: </font></b></td> <td colspan='3'><input type='text' name='county' value='$county' size='25'></td> </tr> <tr> <td width='25%'><b><font face='Verdana' size='2'>Post Code: </font></b></td> <td colspan='3'><input type='text' name='post_code' value='$post_code' size='10'></td> </tr> <tr> <td width='25%'><b><font face='Verdana' size='2'>Email: </font></b></td> <td colspan='3'><input type='text' name='email' value='$email' size='35'></td> </tr> <tr> <td width='25%'><b><font face='Verdana' size='2'>Daytime Telephone: </font></b></td> <td colspan='3'><input type='text' name='telephone' value='$telephone' size='20'></td> </tr> <tr> <td width='25%'><b><font face='Verdana' size='2'>Mobile: </font></b></td> <td colspan='3'><input type='text' name='mobile' value='$mobile' size='20'></td> </tr> <tr valign='top'> <td width='25%'><b><font face='Verdana' size='2'>Comments: </font></b></td> <td colspan='3'><textarea name=\"comments\" cols=\"40\" rows=\"4\" wrap=\"VIRTUAL\"></textarea></td> </tr> </table><font face='Verdana' size='2'><br>Please enter your order details below and click the <b>Submit Order</b> button at the bottom to automatically send your order through to us.<br></font><font face='Verdana' size='2'><br>Alternatively, please click the <b>Print Order Form</b> button at the bottom to print and fax the order form to us.<br><br></font><table width='580' border='1' cellpadding='2' cellspacing='0' bordercolor='#000000'> <tr valign='top'> <td width='70%'><div align='center'><font size='2'><strong>Product Description</strong></font></div> </td> <td width='15%'><div align='center'><font size='2'><strong>Product Code</strong></font></div></td> <td width='15%'><div align='center'><font size='2'><strong>Quantity</strong></font></div></td> </tr>");?><?php session_start();// clear the cart if the action is clearif ($_GET['action'] == "clear"){ unset($_SESSION['items']);}// if a link has been clickedif (isset($_GET['id'])){ if (!isset($_SESSION['items'][$_GET['id']])){ // if it the first time it's selected, quantity is 1 $_SESSION['items'][$_GET['id']] = 1; } else { // if it's been selected before, increase it by 1 $_SESSION['items'][$_GET['id']]++; }}// put the array of id's into a string for use in the sql query$sqlitems = implode(",", array_keys($_SESSION['items']));// connect to the databaseinclude_once('database.php');// run the query to get the product name from the database$sql = "SELECT id, brand_name, product_name, item_number FROM products WHERE id IN ($sqlitems)";$result = mysql_query($sql) or die ("<b>Unable to run :</b> $sql<br>\n<br>\n<b>Because:</b> " . mysql_error());// echo a row for each of our product lineswhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo "<tr><td><font face='Verdana' size='2'>{$row['brand_name']} {$row['product_name']}</font></td><td><font face='Verdana' size='2'>{$row['item_number']}</font></td><td><input size=\"5\" type=\"text\" name=\"{$row['id']}\" value=\"{$_SESSION['items'][$row['id']]}\"></td></tr>\n";}?><?phpprint (" <tr> <td> </td> <td><div align='center'> <input type='submit' name='Submit' value='Submit Order'> </div></td> <td><font face='Verdana' size='2'><b><div class='navlinks'><a href='order_form.php?action=clear'>CLEAR FORM</a></b></div></font></td> </tr></table></form>");}}if ($action =="addscript" ) {require ('mail.php');if ($multiple == "yes"){print("<meta http-equiv=\"refresh\" content=\"1;URL=?action=submitted\"><font face='Verdana' size='2'><br><br><br><br><br><br><br><br><br><br><br><b>Submitting order...</b><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"); }else {print("<meta http-equiv=\"refresh\" content=\"1;URL=?action=submitted\"><font face='Verdana' size='2'><br><br><br><br><br><br><br><br><br><br><br><b>Submitting order...</b><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"); }// ------------------------------------------------------------------------------- actionif ($action =="submitted" ) {print("<font face='Verdana' size='2'><br><br><br><br><br><br><br><br><br><b>Thank you.</b><br><br>Your order has been submitted and will be processed and dispatched within 5 working days.<br><br><br><br><br><br><br><br></font>"); // ------------------------------------------------------------------------------- action}}?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-115373 Share on other sites More sharing options...
stualk Posted October 30, 2006 Author Share Posted October 30, 2006 Hi Huggie,I've been thinking about it over the weekend and I don't think we really need a 'remove item' button on the form. If people want to remove anything they can clear the form and start again, lazy so and sos!The main thing I need to do is sort the small problem with the quantity keep resetting itself and not submitting properly to the email script. Do you know what could be causing this?Once i've sorted that i'm a happy bunny! Quote Link to comment https://forums.phpfreaks.com/topic/25063-help-with-arrays/page/2/#findComment-116631 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.