Shopping Cart

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    Shopping Cart

    Hi,
    I am Building small shopping cart that enables customers to select books from the list and Add it to the Cart.

    When i click Add Button the Cart Should Update with these things.

    No of Books in the Cart.
    Book Name And Quantity

    I'll Submit the Entire Coding that i have currently.

    My Problem is I cant Update the No of Books and Book name along with Qty.
    Can anyone provide some help.
    Is there any other way to Do this.

    products.php
    [PHP]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitl ed Document</title>
    </head>

    <body>
    <p><?php include('cart.p hp'); ?></p>
    <p>
    <?php include('result .php'); ?>

    </p>
    </body>
    </html>
    [/PHP]
    result.php
    [PHP]<?php
    if ($_GET['cat'])
    {$cat = $_GET['cat'];}
    else
    {$cat = "kids";}
    require 'dbcon.php';
    $sql="SELECT * FROM products GROUP BY p_id ORDER BY p_id asc";
    $result=mysql_q uery($sql) or die("Error Occured while Searching Records : " . mysql_error());
    $num=mysql_num_ rows($result);
    if (mysql_num_rows ($result) == 0)
    {
    echo '<table width="700" border="0" cellspacing="0" cellpadding="0" >
    <tr>
    <td align="left" valign="top" class="text">No Maching Result Found<br></td>

    </tr></table>';
    }
    else
    {

    echo '<table width="550" border="0" cellspacing="0" cellpadding="0" >';
    $colsPerRow = 3;
    // width of each column in percent
    $colWidth = (int)(100/$colsPerRow);
    $i = 0;
    while ($row = mysql_fetch_ass oc($result))
    {
    if ($i % $colsPerRow == 0)
    {
    // start a new row
    echo '<tr>';
    }
    echo
    '<td align="center">

    <img src="images_pro ducts/'.$row['p_image'].'" width="154" height="154" border="0" alt="'.$row['p_image'].'"/><br>
    <form action="'.$PHP_ SELF .'" method="post">
    <input name="pid" type="submit" value="ADD"/>
    <input name="pid" type="hidden" value="'.$row['p_id'].'"/>
    </form>
    </td>';

    if ($i % $colsPerRow == $colsPerRow - 1)
    {
    echo '</tr><td align="center" valign="top">&n bsp;</td>';
    }
    $i += 1;
    }
    // print blank columns
    if ($i % $colsPerRow != 0)
    {
    while ($i++ % $colsPerRow != 0)
    {
    echo '<td width="' . $colWidth . '%">&nbsp;</td>';
    }
    echo '</tr>';
    }

    echo '</table>';
    }

    ?>[/PHP]
    dbcon.php
    [PHP]<?
    $con = mysql_connect(' localhost', 'root', 'dba') or die ("Could not connect to the Database");
    mysql_select_db ('test', $con) or die (mysql_error()) ;
    ?>[/PHP]
    cart.php
    [PHP]<?php $pid = $_POST['pid'];

    if($pid){

    $Items = $pid;

    }else{
    $Items = "No Items";
    }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitl ed Document</title>
    </head>

    <body>
    <a href="view_cart .php">Your Cart<br />
    </a>
    <?php echo $Items;?>
    </body>
    </html>[/PHP]

    Code:
    CREATE TABLE `products` (
      `p_id` int(10) NOT NULL auto_increment,
      `p_name` varchar(25) NOT NULL,
      `p_image` varchar(15) NOT NULL,
      PRIMARY KEY  (`p_id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1005 ;
    
    INSERT INTO `products` VALUES (1001, 'PHP MySQL Starter Kit', '1001.jpg');
    INSERT INTO `products` VALUES (1002, 'ASP.Net Beginers Guide', '1002.jpg');
    INSERT INTO `products` VALUES (1003, 'Learn Ajax', '1003.jpg');
    INSERT INTO `products` VALUES (1004, 'Ajax Bible', '1004.jpg');
  • xwero
    New Member
    • Feb 2007
    • 99

    #2
    Originally posted by ajaxrand
    result.php
    [PHP]<?php

    <form action="'.$PHP_ SELF .'" method="post">
    <input name="pid" type="submit" value="ADD"/>
    <input name="pid" type="hidden" value="'.$row['p_id'].'"/>
    </form>
    [/PHP]
    You have two inputs called pid, i think that is the problem. The easy way to do a tablebased item submit is using the button tag

    [HTML]<button type="submit" name="pid" value="'.$row['p_id'].'">ADD</button>[/HTML]

    This way you don't have to use hidden fields and you can put the table in one form instead of as many forms as there are items.

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Originally posted by xwero
      You have two inputs called pid, i think that is the problem. The easy way to do a tablebased item submit is using the button tag

      [HTML]<button type="submit" name="pid" value="'.$row['p_id'].'">ADD</button>[/HTML]

      This way you don't have to use hidden fields and you can put the table in one form instead of as many forms as there are items.
      In my Original Application I am using a Image Button.Thats what I am using a Hidden field. By removing it doesn't make a Answer to my Question.

      All i need to Update my Shopping Cart for each and Every Add.

      Currently I can send Just only the Item that I am selecting from the List.
      But what i need I want to Display,

      1001 x 1 items
      1003 x 3 items

      Like this as a Shopping Cart.

      Thanks for reading my POST.

      Comment

      • xwero
        New Member
        • Feb 2007
        • 99

        #4
        Originally posted by ajaxrand
        In my Original Application I am using a Image Button.Thats what I am using a Hidden field. By removing it doesn't make a Answer to my Question.

        All i need to Update my Shopping Cart for each and Every Add.

        Currently I can send Just only the Item that I am selecting from the List.
        But what i need I want to Display,

        1001 x 1 items
        1003 x 3 items

        Like this as a Shopping Cart.

        Thanks for reading my POST.
        I understand now but then you need to have only one form for that to work

        [PHP]
        <?php
        if(isset($_POST['send'])){
        foreach($_POST['send'] as $buttonvalue){
        if(is_numeric($ _POST['quantity_'.$bu ttonvalue]) && $_POST['quantity_'.$bu ttonvalue] > 0){
        echo $_POST['quantity_'.$bu ttonvalue].' x '.$buttonvalue. '<br>';
        // database code and count subtotals and total
        }
        }
        }
        ?>

        <form action="'.$PHP_ SELF .'" method="post">
        <!-- build the items table -->
        <!-- this is an example how to output a row -->
        <td><input type="text" name="quantity_ <?php echo $row['p_id']; ?>" value="0"></td>
        <td><button type="submit" name="send[]" value="<?php echo $row['p_id']; ?>">image if needed</button></td>
        </form>
        [/PHP]

        this should do the trick.

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          Please Run this Script After Creating MySQL Table in my first post.
          I cant update my CART for each and every click.

          If I am selecting 1001.jpg in the cart I can display
          1 x 1001
          But when i click it again I want to Display
          2 x 1001

          Like wise if there is Multiple selection That also should Update.

          Eg:

          2 x 1001
          1 x 1004
          2 x 1003

          I am stucked with this pls help.

          [PHP]<?php
          // CART VALUES
          if(isset($_POST['send']))
          {

          foreach($_POST['send'] as $buttonvalue)
          {

          if(is_numeric($ _POST['quantity_'.$bu ttonvalue]))
          {

          echo $_POST['quantity_'.$bu ttonvalue].' x '.$buttonvalue. '<br>';

          // database code and count subtotals and total

          }

          }

          }
          //END CART

          // DISPLAY THE TABLE DATA
          if ($_GET['cat'])
          {$cat = $_GET['cat'];}
          else
          {$cat = "kids";}
          require 'dbcon.php';
          $sql="SELECT * FROM products GROUP BY p_id ORDER BY p_id asc";
          $result=mysql_q uery($sql) or die("Error Occured while Searching Records : " . mysql_error());
          $num=mysql_num_ rows($result);
          if (mysql_num_rows ($result) == 0)
          {
          echo '<table width="700" border="0" cellspacing="0" cellpadding="0" >
          <tr>
          <td align="left" valign="top" class="text">No Maching Result Found<br></td>

          </tr></table>';
          }
          else
          {

          echo '<table width="550" border="0" cellspacing="0" cellpadding="0" >';
          $colsPerRow = 3;
          // width of each column in percent
          $colWidth = (int)(100/$colsPerRow);
          $i = 0;
          while ($row = mysql_fetch_ass oc($result))
          {
          if ($i % $colsPerRow == 0)
          {
          // start a new row
          echo '<tr>';
          }
          echo
          '<td align="center">

          <img src="images_pro ducts/'.$row['p_image'].'" width="154" height="154" border="0" alt="'.$row['p_image'].'"/><br>
          <form action="'.$PHP_ SELF .'" method="post">
          <input type="hidden" name="quantity_ '.$row['p_id'].'" value="1">
          <input type="submit" name="send[]" value="ADD"/>
          <input type="hidden" name="send[]" value="'.$row['p_id'].'"/>
          </form>
          </td>';

          if ($i % $colsPerRow == $colsPerRow - 1)
          {
          echo '</tr><td align="center" valign="top">&n bsp;</td>';
          }
          $i += 1;
          }
          // print blank columns
          if ($i % $colsPerRow != 0)
          {
          while ($i++ % $colsPerRow != 0)
          {
          echo '<td width="' . $colWidth . '%">&nbsp;</td>';
          }
          echo '</tr>';
          }

          echo '</table>';
          }

          ?>[/PHP]

          Comment

          Working...