Hi,

I have been working on a shopping cart system where users can select multiple items and then bump them into the cart at once, unlike the common method of dumping one item at a time. Well, i have the script for capturing just one item_id can someone help me out and modify to accept multiple number of item_id's.. The item_id are sent from a different page using check boxes! Will be ever greatful to the person who can help me out here!!! thanks in advance!!

I am using this http://www.thewatchmakerproject.com/...-shopping-cart
script.

the relevant code to change is

<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}

function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$title.' by '.$author.'</td>';
$output[] = '<td>&pound;'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>&pound;'.($price * $qty).'</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>&pound;'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>

This code is in inc folder... function.php.

Please help! Thank you.

    so how do you use those functions?
    what is your script flow?

    your showCart() function is more like editCart() to me.

    I will take a look into your function, but when I do not know the structure of your pages
    and not anything how your script is,
    I can not now answer just anything

    here is how the posted code looks, when using
    bbCode tags [ php ] ... [ /php ]
    instead of [ code ] .... [ /code ] tags ( In POST editor menu )

    regars 🙂
    halojoy

    <?php
    function writeShoppingCart()
    {
        $cart = $_SESSION['cart'];
        if (!$cart){
            return '<p>You have no items in your shopping cart</p>';
            }else{
            $items = explode(',', $cart);
            $s = (count($items) > 1) ? 's':'';
            return '<p>You have <a href="cart.php">' . count($items) . ' item' . $s . ' in your shopping cart</a></p>';
            }
        }
    
    function showCart()
    {
        global $db;
        $cart = $_SESSION['cart'];
        if ($cart){
            $items = explode(',', $cart);
            $contents = array();
            foreach ($items as $item){
                $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
                }
            $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
            $output[] = '<table>';
            foreach ($contents as $id => $qty){
                $sql = 'SELECT * FROM books WHERE id = ' . $id;
                $result = $db->query($sql);
                $row = $result->fetch();
                extract($row);
                $output[] = '<tr>';
                $output[] = '<td><a href="cart.php?action=delete&id=' . $id . '" class="r">Remove</a></td>';
                $output[] = '<td>' . $title . ' by ' . $author . '</td>';
                $output[] = '<td>&pound;' . $price . '</td>';
                $output[] = '<td><input type="text" name="qty' . $id . '" value="' . $qty . '" size="3" maxlength="3" /></td>';
                $output[] = '<td>&pound;' . ($price * $qty) . '</td>';
                $total += $price * $qty;
                $output[] = '</tr>';
                }
            $output[] = '</table>';
            $output[] = '<p>Grand total: <strong>&pound;' . $total . '</strong></p>';
            $output[] = '<div><button type="submit">Update cart</button></div>';
            $output[] = '</form>';
            }else{
            $output[] = '<p>You shopping cart is empty.</p>';
            }
        return join('', $output);
        }
    ?>
      avinash wrote:

      Hi,

      I have been working on a shopping cart system where users can select multiple items and then bump them into the cart at once, unlike the common method of dumping one item at a time. Well, i have the script for capturing just one item_id can someone help me out and modify to accept multiple number of item_id's.. The item_id are sent from a different page using check boxes! Will be ever greatful to the person who can help me out here!!! thanks in advance!!

      I am using this http://www.thewatchmakerproject.com/...-shopping-cart
      script.

      Hi,

      I will offer you a general explanation. There are a few different ways to do what you want to do, but I will show you what I believe is the simplest.

      This is assuming you are using forms. Make sure all the products are added within the same form tag.
      Where you allow the users to select multiple items and bump them into the cart at once, you need to pass all those items [specifically form elements] as an array, i.e., item_id[0], item_id[1] etc. If you are passing other information via the form such as price then you will need to also pass it as price[0], price[1] etc. The numbers can be added in via a while loop counter.

      Then in your code where you update the database, you will need to loop through the array with each loop being an insert into the database.

      Another way you can solve your problem is by using ajax. Each time the customer adds a product, add it to the database without a page reload. If you are familiar with ajax then you will know what I mean.

      Jeff

        Firstly thank you halojoy and Jeff for such quick response!

        well halojoy.... This is what i have till now, I use a form tag like Jeff mentioned, to submit the values, this is how the form code looks like..

        <?php
        echo"<table>";
        echo"<CAPTION>Check items and drop them on your list</CAPTION>";
        $category_id = $_GET['category_id'];
        $sql = "SELECT * FROM `items` WHERE `category_id`='$category_id' ORDER BY item_id";
        $result = $db->query($sql);
          echo"<THEAD>
            <TR>
             <TH  align='center' valign='middle'>Add item </TH>
        	  <TH >item name </TH>
              <TH  align='center' valign='middle'>Type</TH>
              <TH align='center' valign='middle'>Price</TH>
              <TH align='center' valign='middle'>quantity</TH>
            </TR>
               </THEAD>";
        echo"<TBODY><form method='post' name='frm' action='cart-demo/cart.php'>";
          $row_count=0;
          while ($row = $result->fetch()) {
          if($row_count%2!=0)
          {
            echo"<TR class='odd'>
              <TD align='center' valign='middle'><input type='checkbox' name='item_id' /></TD>
              <TD><span>".$row['item_name']."</span></TD>
        	  <TD>".$row['item_decs']." $row_count</TD>
              <TD align='center' valign='middle'> Rs ".$row['item_price']."</TD>
              <TD align='center' valign='middle'><input type='text' size='1' maxlength='1' /></TD>
            </TR>";
        	}
        	else
        	{
        	    echo"<TR>
              <TD align='center' valign='middle'><input type='checkbox' value='".$row['item_id']."' /></TD>
              <TD><span>".$row['item_name']."</span></TD>
        	  <TD>".$row['item_decs']." $row_count</TD>
              <TD align='center' valign='middle'> Rs ".$row['item_price']."</TD>
              <TD align='center' valign='middle'><input type='text' size='1' maxlength='1' /></TD>
            </TR>";
        	}
        	$row_count=$row_count+1;
        	}
        	echo"<input type='button' onclick='CheckAll();' value='Select All' />
        	<input type='button' onclick='CheckAll();' value='Deselect All' />
        	<input type='submit' value='add to list' />";
          echo"</form></TBODY>";
        
        echo"</table>";
        ?>

        Jeff i think you have hit the nail on the head, Your solution is absolutely what i am looking for, unfortunately i am not very experienced in php. But if you could possible make a few changes or write me a code snippet for capturing array of one parameter, say item_id or the price(By modifying the form and function script that i have pasted) that would be really really helpful for me to get an idea of how it can be done in actual coding. Hope i have answered your question as well halojoy!!

        Jeff i do know about use of Ajax, but i am afraid many browsers have java script disabled, many users here in my country do not know how to fix a browser problem. Its better not to risk it. Since i am building a shopping cart for the masses.

        Thanks once again. Jeff and halojoy..

          after
          this form
          we are taken to a page called 'cart.php'
          This is where you USE your FUNCTIONS

          <form method='post' name='frm' action='cart-demo/cart.php'>"; 

          a function does not do anything .. it is just a tool ... defined

          I suppose cart.php is your secret script.
          It is there it all will be happening. Am I right?
          If so, then there is also where we add new such new feature
          as you ask US TO DO for you, as in your first post.

          Now, here is NOT a Free PHP Coding service, give you PHP Code from scratch.
          But here is help with YOUR scripts.
          We can very well fix things YOU already HAVE Coded.

          I now will repeat your request, from first post,
          in hope some nice body can & want to give you assistance, my friend 🙂

          Right now,
          Me, HALOJOY,
          has got nothing to start with.
          No script or some attempt, some try of your own.

          > I have been working on a shopping cart system

          where users can select multiple items and
          then bump them into the cart at once,
          unlike the common method of dumping one item at a time.

          Well, i have the script for capturing just one item_id
          can someone help me out and modify to accept multiple number of item_id's..
          The item_id are sent from a different page using check boxes!

          Will be ever greatful to the person who can help me out here!!!
          thanks in advance!!

            18 days later

            Hi Jeff. The problem has been resolved, and the solution as you rightly pointed was to do with using array index for capturing the item ids of everything that the user selected, and processing it. it is quite simple. Use the array for capture and change the capture php code and voilla! Thank you for your help. i think this post can be put under Resolved section. Thank you once again, for initial inspiration. Bye 🙂

              avinash wrote:

              Hi Jeff. The problem has been resolved, and the solution as you rightly pointed was to do with using array index for capturing the item ids of everything that the user selected, and processing it. it is quite simple. Use the array for capture and change the capture php code and voilla! Thank you for your help. i think this post can be put under Resolved section. Thank you once again, for initial inspiration. Bye 🙂

              That's good news! Don't forget to mark this tread as resolved.

              Jeff

                Write a Reply...