Jump to content

Recommended Posts

I've posted on here in the past & had great help so I'm hoping someone can help again :)

 

Basically what I was trying to do was make the add to cart button grey out and lose the add function if a product is marked as not available in the back end.

 

I found this block of code :-

//Displays the add to cart form. Requires product ID
     $sql = "SELECT code FROM products WHERE productid='".mysql_real_escape_string($_GET['id'])."'";
     $result = mysql_query($sql);
     while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
      {
       $data = '<font size="2" face="Verdana, Arial, Helvetica, sans-serif"color="#000000"><form method="post" action="https://www.domain.com/s_addtocart.php" target="_blank"><input type="hidden" name="code" value="'.$row['code'].'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" style="vertical-align:middle; border: none;" />Quantity: <input type="text" name="quantity" size="3" value="1" /></form></font>';
      }
     break;

which i then modified as follows & it all works as it should :-

//Displays the add to cart form. Requires product ID
     $sql = "SELECT available, due, code FROM products WHERE productid='".mysql_real_escape_string($_GET['id'])."'";
     $result = mysql_query($sql);
     while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
      {
       if ( $row['available'] == '1' )
        {
         $data = '<font size="2" face="Verdana, Arial, Helvetica, sans-serif"color="#000000"><form method="post" action="https://www.domain.com/s_addtocart.php" target="_blank"><input type="hidden" name="code" value="'.$row['code'].'" /><input type="image" src="images/add.png" hspace="5" name="Submit" alt="Add To Cart" style="vertical-align:middle; border: none;" />Quantity: <input type="text" name="quantity" size="3" value="1" /></form></font>';
      }
      elseif ( $row['available'] == '0' )
        {
         $data .= '  <img src="images/addtocartoutofstock.png" width="278" height="39" alt="Sorry, currently out of stock" width="23" height="20"><font color="#C70000" font size="3"><strong>OUT OF STOCK</strong></font><img src="images/outofstock.png" width="23" height="20"> <font size="2" face="Geneva, Arial, Helvetica, sans-serif" color="#000000">'.$row['due'];
        }
      }
     break;

So what's the problem? Well, the actual code that is in use for the add to cart buttons actually has view cart, cross selling & cart summary in the code here is the code in use :-

//Add to cart, view cart, cart summary, xselling all in one enhancement. Requires product ID
     $sql = "SELECT code FROM products WHERE productid='".mysql_real_escape_string($_GET['id'])."'";
     $result = mysql_query($sql);
     while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
      {
       $prodcode =  $row['code'];
      }
     $data = '<table border="0"><tr><td colspan="2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"color="#000000"><form method="post" action="https://www.domain.com/s_addtocart.php" target="_blank"><input type="hidden" name="code" value="'.$prodcode.'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" style="vertical-align:middle; border: none;" hspace="2" /> Quantity: <input type="text" name="quantity" size="3" value="1" /></form></font></td></tr><tr><td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"color="#000000"><a href="https://www.domain.com/cart.php" target="_blank"><img src="images/viewcart.png" alt="View Cart" border="0px" style="vertical-align:middle" hspace="2" /></a></font></td><td>';
     $data .= '<font size="2" face="Verdana, Arial, Helvetica, sans-serif"color="#000000">';
     if ( !(isset($_SESSION['cartid'])) || ($_SESSION['cartid'] == "") )
     //if ( (!isset($_COOKIE['RITD_Cart_ID'])) || ($_COOKIE['RITD_Cart_ID'] == '') )
      {
       $data .= 'No items have been added to the cart yet.';
      }
     else
      {
       $sql = "SELECT sub FROM carts WHERE cartid='".$_SESSION['cartid']."'";
       //$sql = "SELECT orderno, sub FROM carts WHERE cartid='".$_COOKIE['RITD_Cart_ID']."'";
       $result = mysql_query($sql);
       while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
        {
         if ( $row['sub'] == "" )
          {
           $row['sub'] = "0.00";
          }
         $orderno = $orderpre . ($orderstart+$_SESSION['cartid']);
         $data .= 'Order Number: '.$orderno.' with Sub-Total: £'.$row['sub'];
         $data = str_replace("&", "&", $data);
        }
      }
     $data .= '</font></td></tr></table>';
     $xprods = array();
      $sql = "SELECT xselling.xproductid FROM xselling, products WHERE xselling.xproductid=products.productid AND xselling.active='1' AND products.active='1' AND products.live='1' AND xselling.productid='".mysql_real_escape_string($_GET['id'])."'";
      //$data .= $sql;
      $result = mysql_query($sql);
      while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
       {
        array_push($xprods, $row['xproductid']);
       }
      if ( (count($xprods) <= 4) && (count($xprods) > 0) )
       {
        $a = 0;
        $b = count($xprods);
        while ( $a < $b )
         {
          $sql = "SELECT code, title, img, url, price FROM products WHERE active='1' AND live='1' AND productid='".$xprods[$a]."'";
          $result = mysql_query($sql);
          switch ($a)
           {
            case 0:
             $row = mysql_fetch_assoc($result);
             $x1 = '<a href="'.$producturl.$row['url'].'" style="color:#0000FF;"><img src="'.$imageurl.$row['img'].'" border="none" alt="'.$row['title'].'" /><br />'.$row['title'].'</a>';
             $x1a = '£'.$row['price'];
             $x1b = '<form method="post" action="https://www.domain.com/s_quickadd.php" target="_blank"><input type="hidden" name="code" value="'.$row['code'].'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" /></form>';
             break;
            case 1:
             $row = mysql_fetch_assoc($result);
             $x2 = '<a href="'.$producturl.$row['url'].'" style="color:#0000FF;"><img src="'.$imageurl.$row['img'].'" border="none" alt="'.$row['title'].'" /><br />'.$row['title'].'</a>';
             $x2a = '£'.$row['price'];
             $x2b = '<form method="post" action="https://www.domain.com/s_quickadd.php" target="_blank"><input type="hidden" name="code" value="'.$row['code'].'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" /></form>';
             break;
            case 2:
             $row = mysql_fetch_assoc($result);
             $x3 = '<a href="'.$producturl.$row['url'].'" style="color:#0000FF;"><img src="'.$imageurl.$row['img'].'" border="none" alt="'.$row['title'].'" /><br />'.$row['title'].'</a>';
             $x3a = '£'.$row['price'];
             $x3b = '<form method="post" action="https://www.domain.com/s_quickadd.php" target="_blank"><input type="hidden" name="code" value="'.$row['code'].'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" /></form>';
             break;
            case 3:
             $row = mysql_fetch_assoc($result);
             $x4 = '<a href="'.$producturl.$row['url'].'" style="color:#0000FF;"><img src="'.$imageurl.$row['img'].'" border="none" alt="'.$row['title'].'" /><br />'.$row['title'].'</a>';
             $x4a = '£'.$row['price'];
             $x4b = '<form method="post" action="https://www.domain.com/s_quickadd.php" target="_blank"><input type="hidden" name="code" value="'.$row['code'].'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" /></form>';
             break;
           }
          $a++;
         }
       }
      elseif ( count($xprods) > 4 )
       {
        $c = 0;
        while ( $c < 4 )
         {
          $pick = rand(1,count($xprods));
          $pick = $pick - 1;
          switch ($c)
           {
            case 0:
            $x1 = $xprods[$pick];
            break;
            case 1:
            $x2 = $xprods[$pick];
            break;
            case 2:
            $x3 = $xprods[$pick];
            break;
            case 3:
            $x4 = $xprods[$pick];
            break;
           }
          unset($xprods[$pick]);
          $xprods = array_values($xprods);
          $c++;
         } 
        $sql = "SELECT code, title, img, url, price FROM products WHERE active='1' AND live='1' AND productid='".$x1."'"; 
        $result = mysql_query($sql);
        while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
         {
          $x1 = '<a href="'.$producturl.$row['url'].'" style="color:#0000FF;"><img src="'.$imageurl.$row['img'].'" border="none" alt="'.$row['title'].'" /><br />'.$row['title'].'</a>';
          $x1a = '£'.$row['price'];
          $x1b = '<form method="post" action="https://www.domain.com/s_quickadd.php" target="_blank"><input type="hidden" name="code" value="'.$row['code'].'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" /></form>';
         }
        $sql = "SELECT code, title, img, url, price FROM products WHERE active='1' AND live='1' AND productid='".$x2."'"; 
        $result = mysql_query($sql);
        while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
         {
          $x2 = '<a href="'.$producturl.$row['url'].'" style="color:#0000FF;"><img src="'.$imageurl.$row['img'].'" border="none" alt="'.$row['title'].'" /><br />'.$row['title'].'</a>';
          $x2a = '£'.$row['price'];
          $x2b = '<form method="post" action="https://www.domain.com/s_quickadd.php" target="_blank"><input type="hidden" name="code" value="'.$row['code'].'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" /></form>';
         }
        $sql = "SELECT code, title, img, url, price FROM products WHERE active='1' AND live='1' AND productid='".$x3."'"; 
        $result = mysql_query($sql);
        while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
         {
          $x3 = '<a href="'.$producturl.$row['url'].'" style="color:#0000FF;"><img src="'.$imageurl.$row['img'].'" border="none" alt="'.$row['title'].'" /><br />'.$row['title'].'</a>';
          $x3a = '£'.$row['price'];
          $x3b = '<form method="post" action="https://www.domain.com/s_quickadd.php" target="_blank"><input type="hidden" name="code" value="'.$row['code'].'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" /></form>';
         }
        $sql = "SELECT code, title, img, url, price FROM products WHERE active='1' AND live='1' AND productid='".$x4."'"; 
        $result = mysql_query($sql);
        while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
         {
          $x4 = '<a href="'.$producturl.$row['url'].'" style="color:#0000FF;"><img src="'.$imageurl.$row['img'].'" border="none" alt="'.$row['title'].'" /><br />'.$row['title'].'</a>';
          $x4a = '£'.$row['price'];
          $x4b = '<form method="post" action="https://www.domain.com/s_quickadd.php" target="_blank"><input type="hidden" name="code" value="'.$row['code'].'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" /></form>';
         }
        
       }
     $data .= '
     </font>
     <table border="0" width="600px">
      <tr style="background-color: #2658C8; color:#FFFFFF; font-weight: bold; font-size: 15px; font-family:Verdana, Arial, Helvetica, sans-serif;">
       <td colspan="4">You may also be interested in these products...</td>
      </tr>
      <tr>
       <td align="center" style="vertical-align:top; font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x1.'</td>
       <td align="center" style="vertical-align:top; font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x2.'</td>
       <td align="center" style="vertical-align:top; font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x3.'</td>
       <td align="center" style="vertical-align:top; font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x4.'</td>
      </tr>
      <tr>
       <td align="center" style="font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x1a.'</td>
       <td align="center" style="font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x2a.'</td>
       <td align="center" style="font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x3a.'</td>
       <td align="center" style="font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x4a.'</td>
      </tr>
      <tr>
       <td align="center" style="font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x1b.'</td>
       <td align="center" style="font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x2b.'</td>
       <td align="center" style="font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x3b.'</td>
       <td align="center" style="font-size: 12px; font-family:Verdana, Arial, Helvetica, sans-serif;">'.$x4b.'</td>
      </tr>
     </table>
     ';
     break;

I'd like to get that code to work in the same way, ie, if the back end DB has the not available flag set to yes the add to cart button is replaced as in my second code snippet.

 

Hope someone can help! Thanks

Link to comment
https://forums.phpfreaks.com/topic/301121-custom-shopping-cart-code-help/
Share on other sites

You are using obsolete code that has been completely removed from PHP. Hiding errors with @ is a bad idea. You want to fix errors, not hide them, and why in the world are you echoing html? On top of that, page formatting goes in an external CSS file. This code looks like it was written in the 90's. All this x1, x2 x3 is ridiculous.

 

You need to use PDO with prepared statements.

https://phpdelusions.net/pdo

Edited by benanamen

^^^^ i have a similar recommendation. before you worry about modifying how this code does something or have someone in a help forum look at it to try to help you with what it is doing, you need to greatly simplify it and update it -

 

1) use the PDO database extension.

 

2) use css to style elements.

 

3) don't suppress errors (if those fetch statement were producing errors, it means that your queries are failing due to a problem with the database connection, database table, or the query syntax.)

 

edit: 3b) only fetch the data you need/use the simplest syntax that accomplishes a task (KISS - keep it simple...). you are just using associative data from the query, just use a fetch statement that retrieves the data in that format. your current code is fetching both numerical and associative data, which is the default for the statement you are using AND you are also supplying the optional parameter telling it to fetch exactly what the default is.

 

 

4) don't loop to retrieve query results when there's only one row. there's only one place in this code where the query can match more than one row. that's the only place where there should be a loop.

 

5) separate your database 'business' logic from your 'presentation' logic. this will also help you avoid trying to run queries inside the presentation logic, who's responsibility is to produce output.

 

6) don't run queries inside of loops and DRY (Don't Repeat Yourself). the main part of that repetitive logic can all be replaced with simple code. there won't be a TON of related products for any selected product. just run one JOINed query to get all the related product information at once and retrieve it into an array. if there's more than 4 results, shuffle the array, then split off the first 4 entries. then, just loop over the 4 random entries or the original data, in the case where there were 4 or less results. you would produce the final output in this loop.

 

edit: 7) the input to this code is an id. you should make sure it was supplied before trying to use it.

Edited by mac_gyver
//Add to cart, view cart, cart summary, xselling all in one enhancement. Requires product ID
     $sql = "SELECT code FROM products WHERE productid='".mysql_real_escape_string($_GET['id'])."'";
     $result = mysql_query($sql);
     while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
      {
       $prodcode =  $row['code'];
      }
     $data = '<table border="0"><tr><td colspan="2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"color="#000000"><form method="post" action="https://www.domain.com/s_addtocart.php" target="_blank"><input type="hidden" name="code" value="'.$prodcode.'" /><input type="image" src="images/add.png" name="Submit" alt="Add To Cart" style="vertical-align:middle; border: none;" hspace="2" /> Quantity: <input type="text" name="quantity" size="3" value="1" /></form></font></td></tr><tr><td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"color="#000000"><a href="https://www.domain.com/cart.php" target="_blank"><img src="images/viewcart.png" alt="View Cart" border="0px" style="vertical-align:middle" hspace="2" /></a></font></td><td>';
     $data .= '<font size="2" face="Verdana, Arial, Helvetica, sans-serif"color="#000000">';
     if ( !(isset($_SESSION['cartid'])) || ($_SESSION['cartid'] == "") )
     //if ( (!isset($_COOKIE['RITD_Cart_ID'])) || ($_COOKIE['RITD_Cart_ID'] == '') )
      {
       $data .= 'No items have been added to the cart yet.';
      }
     else
      {
       $sql = "SELECT sub FROM carts WHERE cartid='".$_SESSION['cartid']."'";
       //$sql = "SELECT orderno, sub FROM carts WHERE cartid='".$_COOKIE['RITD_Cart_ID']."'";
       $result = mysql_query($sql);
       while ( $row = @mysql_fetch_array($result, MYSQL_BOTH) )
        {
         if ( $row['sub'] == "" )
          {
           $row['sub'] = "0.00";
          }
         $orderno = $orderpre . ($orderstart+$_SESSION['cartid']);
         $data .= 'Order Number: '.$orderno.' with Sub-Total: £'.$row['sub'];
         $data = str_replace("&", "&", $data);
        }
      }
     $data .= '</font></td></tr></table>';
     break;

I should add, I know NOTHING about PHP! This was written for me about 7 years ago. It all works I'm just trying to modify that one section. I've since worked out that it is just this section of code that needs modifying.

It all works right now - it won't work in the very near future. PHP7 has removed the mysql_* functions that are used all over this code. So at some point in the near future (depending on how quickly your host updates their servers), this one section of code will be the absolute least of your worries. As benanamen and mac_gyver said, you're going to need to have this code rewritten using PDO (or do it yourself).

Edited by maxxd

Thanks, Barand - I meant to link that...

 

@ohno - There are plenty of people on this board that can. Post in the Job Offerings forum and I'm sure you'll get replies. And depending on your server setup, no, you may not have to upgrade; however, running outdated, unsupported versions of PHP is dangerous to your company and to your company's clients, so you'll want to.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.