Guest User

wishlist

a guest
Oct 6th, 2011
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.72 KB | None | 0 0
  1. <? include "db.php";
  2.  
  3. function createcode($length = 8, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
  4. {
  5.     $chars_length = (strlen($chars) - 1);
  6.     $string = $chars{rand(0, $chars_length)};
  7.     for ($i = 1; $i < $length; $i = strlen($string))
  8.     {
  9.         $r = $chars{rand(0, $chars_length)};
  10.         if ($r != $string{$i - 1}) $string .=  $r;
  11.     }
  12.     return $string;
  13. }
  14.  
  15. if (!$_SESSION[cartid] AND ($_POST[update_product] OR $_GET[loadcode])) {
  16.     //echo "<script>alert('cart setup!')</script>";
  17.     getmycartcode();
  18. }
  19.  
  20. function getmycartcode() {
  21.  
  22.     $mycart = mysql_query("SELECT id,basketcode FROM wishlistsaved WHERE sesid = '$sesid'");
  23.     $cart = mysql_fetch_assoc($mycart);
  24.    
  25.     $sesid = session_id();
  26.    
  27.     if (!$cart[id]) {
  28.         while ($usecode == false) {
  29.             $basketcode = createcode();
  30.             $findcode = mysql_query("SELECT id FROM wishlistsaved WHERE basketcode = '$sesid'");
  31.             if (mysql_num_rows($findcode) == 0) { $usecode = true; }
  32.         }
  33.         mysql_query("INSERT INTO wishlistsaved (sesid, expires, basketcode) VALUES ('$sesid', NOW() + INTERVAL 14 DAY, '$basketcode')");
  34.         $_SESSION[cartid] = $basketcode;
  35.     } else {
  36.         $_SESSION[cartid] = $cart[basketcode];
  37.     }
  38.    
  39.    
  40.     //delete old wishlists!
  41.     $deletethese = mysql_query("SELECT sesid,id FROM wishlistsaved WHERE expires <= NOW()");
  42.     while ($delete = mysql_fetch_assoc($deletethese)) {
  43.         mysql_query("DELETE FROM wishlistbasket WHERE sesid = '$delete[sesid]'");
  44.         mysql_query("DELETE FROM wishlistsaved WHERE id = '$delete[id]'");
  45.     }
  46. }
  47.  
  48. if ($_POST[updatebasket]) {
  49.  
  50.     $mybasket = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$sesid' ORDER BY pid,qid,oid ASC");
  51.     while ($item = mysql_fetch_assoc($mybasket)) {
  52.         $postname = $item[pid]."_".$item[qid]."_".$item[oid];
  53.         $thisqty = $_POST[$postname];
  54.         if ($thisqty) {
  55.             mysql_query("UPDATE wishlistbasket SET qty = '$thisqty' WHERE sesid = '$sesid' AND id = '$item[id]'")or die(mysql_error());
  56.         } else {
  57.             mysql_query("DELETE FROM wishlistbasket WHERE id = '$item[id]' AND sesid = '$sesid'")or die(mysql_error());
  58.         }
  59.        
  60.     }
  61.     header("Location: index.php?id=3&basket=true");
  62.     exit;
  63. }
  64.  
  65. if ($_GET[clearall]) {
  66.     mysql_query("DELETE FROM wishlistbasket WHERE sesid = '$sesid'");
  67. }
  68.  
  69. if ($_POST[update_product]) {
  70.  
  71.     $productid = $_POST[productid];
  72.     $get_product_quantities = mysql_query("SELECT * FROM product_quantities WHERE product_id='$productid' ORDER BY id ASC") or die(mysql_error());
  73.     while ($product_quantity = mysql_fetch_assoc($get_product_quantities)) {
  74.        
  75.         $get_product_options = mysql_query("SELECT * FROM product_options WHERE quantity_id='$product_quantity[id]' ORDER BY id ASC");
  76.         while ($product_option = mysql_fetch_assoc($get_product_options)) {
  77.             $postname = $productid."_".$product_quantity[id]."_".$product_option[id];
  78.             $thisqty = $_POST[$postname];
  79.                 $item = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$sesid' AND pid = '$productid' AND qid = '$product_quantity[id]' AND oid = '$product_option[id]'");
  80.                     if (mysql_num_rows($item) == 0) {
  81.                         $noitem = true;
  82.                         echo "no item<br />";
  83.                     } else {
  84.                         $noitem = false;
  85.                         echo "item found<br />";
  86.                     }
  87.                 if ($thisqty) {
  88.                     if ($noitem) { //doesnt exist yet so add it to basket
  89.                         echo "insert<br />";
  90.                         mysql_query("INSERT INTO wishlistbasket (sesid, pid, qid, oid, qty) VALUES ('$sesid','$productid','$product_quantity[id]','$product_option[id]','$thisqty')")or die(mysql_error());
  91.                     } else { //already exists so update basket!
  92.                         echo "update<br />";
  93.                         mysql_query("UPDATE wishlistbasket SET qty = '$thisqty' WHERE sesid = '$sesid' AND pid = '$productid' AND qid = '$product_quantity[id]' AND oid = '$product_option[id]'")or die(mysql_error());
  94.                     }
  95.                 } else { //no post item found but found in basket so delete from basket!
  96.                     echo "delete<br />";
  97.                     if (!$noitem) { mysql_query("DELETE FROM wishlistbasket WHERE sesid = '$sesid' AND pid = '$productid' AND qid = '$product_quantity[id]' AND oid = '$product_option[id]'")or die(mysql_error()); }
  98.                 }
  99.         }
  100.     }
  101.    
  102.     header("Location: index.php?id=3&productid=$productid");
  103.     exit;
  104. }
  105.  
  106. if ($_GET[loadcode]) {
  107.     $mycode = $_GET[loadcode];
  108.    
  109.     if ($mycode == $_SESSION[cartid]) {
  110.           echo "<br />The code you entered is the same as the code already loaded.<br /><input type=\"text\" name=\"loadcode\" id=\"loadcode\" value=\"$mycode\" class=\"loadcode\" />";
  111.           exit;
  112.     }
  113.    
  114.     $mycart = mysql_query("SELECT sesid,id FROM wishlistsaved WHERE basketcode = '$mycode'");
  115.     $cart = mysql_fetch_assoc($mycart);
  116.    
  117.     if (mysql_num_rows($mycart) == 0) { //did not find code!
  118.         echo "<br />Sorry the code you entered wasn't found, please check the code and try again...<br /><input type=\"text\" name=\"loadcode\" id=\"loadcode\" value=\"$mycode\" class=\"loadcode\" />";
  119.     } else { //found code and do stuff!
  120.         echo "<br />Code successful, your wish list has been loaded and the code '$mycode'<br />is valid for another 14 days to use again.<br />";
  121.         $getitems = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$cart[sesid]'");
  122.         while ($item = mysql_fetch_assoc($getitems)) {
  123.             $searchitems = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$sesid' AND pid = '$item[pid]' AND qid = '$item[qid]' AND oid = '$item[oid]'")or die(mysql_error());
  124.             if (mysql_num_rows($searchitems) == 1) {
  125.                 $searched = mysql_fetch_assoc($searchitems);
  126.                 $newqty = $searched[qty] + $quote[qty] + $sample[qty];
  127.                 mysql_query("UPDATE wishlistbasket SET qty = '$newqty' WHERE sesid = '$sesid' AND pid = '$item[pid]' AND qid = '$item[qid]' AND oid = '$item[oid]'")or die(mysql_error());
  128.             } else {
  129.                 mysql_query("INSERT INTO wishlistbasket (sesid, pid, qid, oid, qty) VALUES ('$sesid','$item[pid]','$item[qid]','$item[oid]','$quote[qty]','$sample[qty]')")or die(mysql_error());
  130.             }
  131.         }
  132.         mysql_query("UPDATE wishlistsaved SET expires = NOW() + INTERVAL 14 DAY WHERE id = '$cart[id]'");
  133.     }
  134.     exit;
  135.    
  136. }
  137.  
  138. if ($_GET[myemail]) {
  139.  
  140.     $message = "<font face=Tahoma size=2>Your wish list code is: $_SESSION[cartid]<br /><br />For an updated view of whats in your wish list please type your code into our website: http://www.dtrmedical.com/products/ <br /><br />Many Thanks,<br />DTR Medical</font>";
  141.     $headers = "From: [email protected]\r\n" .
  142.            'X-Mailer: PHP/' . phpversion() . "\r\n" .
  143.            "MIME-Version: 1.0\r\n" .
  144.            "Content-Type: text/html; charset=utf-8\r\n" .
  145.            "Content-Transfer-Encoding: 8bit\r\n\r\n";
  146.    
  147.     mail($_GET[myemail],'Your DTRMedical Wishlist Code',$message,$headers);
  148.  
  149.     echo "<br />Your code has been sent to: <b>$_GET[myemail]</b>";
  150.  
  151.     exit;
  152. }
  153.  
  154. if ($_GET[newcode]) {
  155.  
  156. if ($_SESSION[cartid]) {
  157.     $mycart = mysql_query("SELECT * FROM wishlistsaved WHERE sesid = '$sesid'");
  158.     $cart = mysql_fetch_assoc($mycart);
  159.    
  160.     if ($cart[id]) { $currentcart = "<br />
  161.    Your current wish list code is: <strong>$cart[basketcode]</strong>, insert your new code below:
  162.    <br />"; }
  163.    
  164. }
  165.  
  166. ?>
  167. <div id="msgbox_title">Load Wish List...<div id="closeme" onclick="jQuery(document).trigger('close.facebox')">x</div></div>
  168. <form method="post">
  169. <div id="msgbox_body">Here you can load a previous wish list by using the code that was provided to you,<br />remember these codes expire 14 days after being created.
  170. <br />
  171. <div id="newcode">
  172. <? echo $currentcart; ?>
  173. <input type="text" name="loadcode" id="loadcode" class="loadcode" />
  174. </div></div>
  175. <div id="msgbox_actions"><input type="button" onclick="loadmycode()" value="Load Code" /></div>
  176. </form>
  177. <script>
  178. function loadmycode() {
  179.         var thiscode = $("#loadcode").val();
  180.         $("#newcode").html('<br />loading code...<br />');
  181.             $('#newcode').load('wishlist.php?loadcode='+thiscode, "",
  182.                 function(responseText, textStatus, XMLHttpRequest) {
  183.                     if(textStatus == 'error') {
  184.                         var msg = "Error:<br />";
  185.                         $("#newcode").html(msg + xhr.status + " " + xhr.statusText);
  186.                     }
  187.                 }
  188.             );
  189. }
  190. </script>
  191. <? } else if ($_GET[addproduct]) {
  192.  
  193. $productid = $_GET[addproduct];
  194. $get_product_quantities = mysql_query("SELECT * FROM product_quantities WHERE product_id='$productid' ORDER BY id ASC") or die(mysql_error());
  195.  
  196. ?>
  197. <form method="post" action="wishlist.php">
  198. <input type="hidden" name="update_product" value="true" />
  199. <input type="hidden" name="productid" value="<? print $productid; ?>" />
  200. <div id="msgbox_title">Please tick if you would like a quote and/or sample<div id="closeme" onclick="jQuery(document).trigger('close.facebox')">x</div></div>
  201. <div id="msgbox_body">
  202. <table border="0" cellpadding="0" cellspacing="5" align="center" style="border-collapse:separate!IMPORTANT;">
  203. <?php
  204.     while ($product_quantity = mysql_fetch_assoc($get_product_quantities)) {
  205. ?>
  206.  
  207.     <tr>
  208.         <td colspan="4" style="padding:0px 9px">
  209.             <strong><?php echo $product_quantity['quantity_name']; ?>:</strong>
  210.         </td>
  211.     </tr>
  212. <?php
  213.     $get_product_options = mysql_query("SELECT * FROM product_options WHERE quantity_id='$product_quantity[id]' ORDER BY id ASC");
  214.     if (mysql_num_rows($get_product_options)==0) {
  215.         echo "<tr><td colspan='4'>No products in this quantity</td></tr>";
  216.     } else {
  217. ?>
  218.         <tr>
  219.         <td style="width: 10px;">&nbsp;</td>
  220.         <td style="width: 50px;"><span style="color: #aaa;">Quote:</span></td>
  221.         <td style="width: 50px;"><span style="color: #aaa;">Sample:</span></td>
  222.         <td style="width: 380px;"><span style="color: #aaa;">Description:</span></td>
  223.         <td>&nbsp;</td>
  224.     </tr>
  225.  
  226. <?php
  227.     while ($product_option = mysql_fetch_assoc($get_product_options)) {
  228.     $thisitem = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$sesid' AND pid = '$productid' AND qid = '$product_quantity[id]' AND oid = '$product_option[id]'");
  229.   $item = mysql_fetch_assoc($thisitem);
  230.   $quote[qty] = 1;
  231.   $sample[qty] = 1;
  232. ?>
  233.     <tr>
  234.         <td></td>
  235.         <td align="center" valign="middle" style="background-color:#C3DCCD;">
  236.             <input style="margin:5px;" type="checkbox" name="<? echo $productid."_".$product_quantity[id]."_".$product_option[id]; ?>" value="<? echo $quote[qty]; ?>" />
  237.         </td>      
  238.         <td align="center" valign="middle" style="background-color:#D3C4DF;">
  239.             <input style="margin:5px;" type="checkbox" name="<? echo $productid."_".$product_quantity[id]."_".$product_option[id]; ?>" value="<? echo $sample[qty]; ?>" />
  240.         </td>
  241.         <td style="font-size:11px;background-color:#efefef;"><p style="margin:0px 0px 5px 0px;padding:5px;"><span style="color:#663399;font-weight:bold;"><?php echo $product_option['product_code']; ?></span> <?php echo $product_option['description']; ?></p>
  242.         </td>
  243.         <td>&nbsp;</td>
  244.     </tr>
  245. <?php
  246.     } // END while ($product_option = mysql_fetch_assoc( ...
  247.    
  248.     } // END if (mysql_num_rows($get_product_options)==0) ... else
  249. ?>
  250.     <tr colspan="4"><td>&nbsp;</td></tr>
  251. <?php
  252.     } // END while ($product_quantity = mysql_fetch_assoc ...
  253. ?>
  254. <script language="JavaScript">
  255. function toggle(source) {
  256.   checkboxes = document.getElementsByName(source.name);
  257.   for(var i in checkboxes)
  258.     checkboxes[i].checked = source.checked;
  259. }
  260. </script>
  261.  
  262.     <tr>
  263.         <td>&nbsp;</td>
  264.         <td valign="middle" align="center" style="background-color:#efefef;"><input style="margin-top:5px;" type="checkbox" onClick="toggle(this)" name="products-quote" value="" /></td>
  265.         <td valign="middle" align="center" style="background-color:#efefef;"><input style="margin-top:5px;" type="checkbox" onClick="toggle(this)" name="products-sample" value="" /></td>
  266.         <td style="background-color:#efefef;"><p style="margin:0px 0px 5px 0px;padding:5px;"><b>Select all</b></p></td>
  267.         <td>&nbsp;</td>
  268.     </tr>
  269. </table>
  270.  
  271. </div>
  272. <div id="msgbox_actions"><input type="submit" value="Add To Wish List" /></div>
  273. </form>
  274. <? } else if ($_GET[basket]) { ?>
  275. <div id="msgbox_title">Your Wish List<div id="closeme" onclick="jQuery(document).trigger('close.facebox')">x</div></div>
  276. <form method="post" action="wishlist.php">
  277. <div id="msgbox_body" style="max-height:400px;min-height:100px;overflow-y:auto;overflow-x:hidden;">
  278. <input type="hidden" name="updatebasket" value="1" />
  279. <table border="0" cellpadding="0" cellspacing="5" align="center" style="min-width:390px;border-collapse:separate!IMPORTANT;">
  280. <?
  281.  
  282.     $mycart = mysql_query("SELECT * FROM wishlistsaved WHERE sesid = '$sesid'");
  283.     $cart = mysql_fetch_assoc($mycart);
  284.  
  285.     $mybasket = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$sesid' ORDER BY pid,qid,oid ASC");
  286.     if (mysql_num_rows($mybasket) == 0) { echo "<tr><td><br /><br /><center>There are no items in your wish list.</center></td></tr>"; $listempty = true; }
  287.     while ($item = mysql_fetch_assoc($mybasket)) {
  288.    
  289.         if ($thispid <> $item[pid]) {
  290.             $products = mysql_query("SELECT product_name FROM products WHERE id = '$item[pid]'");
  291.             $product = mysql_fetch_assoc($products);
  292.        
  293.             if ($thispid <> "") { $br = "<br />"; }
  294.        
  295.             echo "<tr><td colspan=\"5\" style=\"text-align: left;\">$br<strong style=\"font-size:14px;\">$product[product_name]</strong></td></tr>";
  296.             $thispid = $item[pid];
  297.         }
  298.        
  299.         if ($thisqid <> $item[qid]) {
  300.             $quanities = mysql_query("SELECT quantity_name FROM product_quantities WHERE id = '$item[qid]'");
  301.             $thisqty = mysql_fetch_assoc($quanities);
  302.        
  303.             echo "<!-- <tr><td colspan=\"5\"><strong style=\"display:block;padding-top:5px;\">$thisqty[quantity_name]</strong></td></tr> -->";
  304.             $thisqid = $item[qid];
  305.         }
  306.        
  307.         $get_product_options = mysql_query("SELECT * FROM product_options WHERE id = '$item[oid]' ORDER BY id ASC");
  308.         $product_option = mysql_fetch_assoc($get_product_options);
  309.        
  310.         ?>
  311.       <tr>
  312.           <!-- <td style="padding:0px 2px;"><input type="textbox" class="textbox" value="<? echo $item[qty]; ?>" name="<? echo $item[pid]."_".$item[qid]."_".$item[oid]; ?>" /></td> -->
  313.           <td align="center" valign="middle" style="background-color:#C3DCCD;"><input style="margin:5px;" type="checkbox" name="<? echo $item[pid]."_".$item[qid]."_".$item[oid]; ?>" value="<? echo $quote[qty]; ?>" /></td>
  314.           <td align="center" valign="middle" style="background-color:#D3C4DF;"><input style="margin:5px;" type="checkbox" name="<? echo $item[pid]."_".$item[qid]."_".$item[oid]; ?>" value="<? echo $sample[qty]; ?>" /></td>
  315.           <td style="font-size:11px;background-color:#efefef;"><p style="margin:0px 0px 5px 0px;padding:5px;"><span style="color:#663399;font-weight:bold;"><?php echo $product_option['product_code']; ?></span> <?php echo $product_option['description']; ?></p></td>
  316.           <td style="font-size:11px;background-color:#efefef;"><p style="margin:0px 0px 5px 0px;padding:5px; font-weight:bold;"><?php echo $thisqty[quantity_name]; ?></p></td>
  317.       </tr>
  318.         <?
  319.    
  320.     }
  321.  
  322. ?>
  323. </table>
  324. </div>
  325. <script>
  326. function quoteme() {
  327.         $("#msgbox_body").html('<div id="contactform"><form class="form" method="POST" action="http://www.dtrmedical.com"><table border="0"><tbody><tr><td><p class="name"><label for="name">Your Name<span style="color:red;">*</span>:</label></p></td><td>&nbsp;</td><td><input type="text" name="name" id="name" /></td></tr><tr><td><p class="hospital"><label for="hospital">Hospital/Institution:</label></p></td><td>&nbsp;</td><td><input type="text" name="hospital" id="hospital" /></td></tr><tr><td><p class="department"><label for="department">Department:</label></p></td><td>&nbsp;</td><td><input type="text" name="department" id="department" /></td></tr><tr><td><p class="email"><label for="email">E-mail<span style="color:red;">*</span>:</label></p></td><td>&nbsp;</td><td><input type="text" name="email" id="email" /></td></tr><tr><td><p class="tel"><label for="tel">Telephone<span style="color:red;">*</span>:</label></p></td><td>&nbsp;</td><td><input type="text" name="tel" id="tel" /></td></tr><tr><td colspan="3" align="center"><p class="submit"><input style=\"margin-left:27px;cursor:pointer;\" onclick="jQuery.facebox({ ajax: (\'wishlist.php?emailme=true&name=\' + this.form.name.value + \'&email=\' + this.form.email.value + \'&tel=\' + this.form.tel.value + \'&hospital=\' + this.form.hospital.value + \'&department=\' + this.form.department.value) }); return false;" type="submit" value="Send enquiry" name="submit" /></p></td><td>&nbsp;</td><td>&nbsp;</td></tr></tbody></table></form></div>');
  328.         $("#msgbox_actions").html('<input onclick="jQuery.facebox({ ajax: \'wishlist.php?basket=true\' })" type="button" value="Back to Wishlist" />');
  329. }
  330. </script>
  331. <? if (!$listempty) { ?>
  332. <div id="msgbox_actions"><input onclick="quoteme()" type="button" value="Request quote" /> <!-- <input onclick="jQuery.facebox({ ajax: 'wishlist.php?emailme=true' })" type="button" value="Save list for later" /> --> <input type="submit" value="Update Qty's" /> <input onclick="jQuery.facebox({ ajax: 'wishlist.php?basket=true&clearall=true' })" type="button" value="Clear list" /></div>
  333. <? } ?>
  334. </form>
  335. </div>
  336. <? } ?>
  337.  
  338. <?php
  339. // if send email
  340. if(isset($_GET['emailme']) && $_GET['emailme'] == 'true') {
  341.     // to and subject
  342.     $to = "[email protected]";
  343.     $subject = "DTR Medical website - Product enquiry";
  344.    
  345.     // get these from query string
  346.     $name_field = $_GET['name'];
  347.     $hospital_field = $_GET['hospital'];
  348.     $department_field = $_GET['department'];
  349.     $email_field = $_GET['email'];
  350.     $tel_field = $_GET['tel'];
  351.    
  352.     // get wishlist
  353.     $query = "SELECT w.*, p.product_name, q.quantity_name, o.product_code, o.description
  354.                 FROM wishlistbasket w, products p, product_quantities q, product_options o
  355.                 WHERE sesid = '$sesid' AND w.pid = p.id AND w.qid = q.id AND w.oid = o.id ORDER BY w.pid, w.qid, w.oid";
  356.     $res = mysql_query($query);
  357.     $wish_list = '';
  358.     if($res){
  359.         while($row = mysql_fetch_assoc($res))
  360.             $wish_list .= $row['product_name'] . ', ' . $row['quantity_name'] . ', ' . $row['qty'] . ', ' . $row['product_code'] . ', ' . $row['product_options'] . "\n";
  361.     }
  362.    
  363.     // build mail body
  364.     $body = "DTR Medical,\n\n
  365.    
  366.     You have an enquiry from the website, please see the details below:\n\n
  367.     Name: $name_field\n
  368.     Hospital/institution: $hospital_field\n
  369.     Department: $department_field\n
  370.     E-Mail: $email_field\n
  371.     Tel: $tel_field\n
  372.     Wishlist:\n $wish_list";
  373.     mail($to, $subject, $body);
  374.     echo "<div style=\"margin:20px;\">Thank you for your enquiry. We will be in touch within 48 hours.<br /> <br />If you would like to get in touch regarding your enquiry then please call us on:<br /> +44 (0) 1792 79 79 10<br /><br /><div onclick=\"location.href='/products\" style=\"margin-left:135px;cursor: pointer; margin-top: 10px; border-top-left-radius: 5px 5px; border-top-right-radius: 5px 5px; border-bottom-right-radius: 5px 5px; border-bottom-left-radius: 5px 5px; background-color:#663399; width: 143px; height: 30px;\"><a style=\"  color:white;display:block;float:right;height:26px;margin-top:4px;padding-left:0px;padding-top:5px;width:120px;\" href=\"/products\" class=\"basket\">Back to products</a></div></div>";}
  375. ?>
  376.  
Advertisement
Add Comment
Please, Sign In to add comment