will someone help me with adding an array to a cookie? cause i dont know what i'm doing! :rolleyes:

okay well i have a shopping cart i just made. and i am using cookies to store the information on the products that the users chooses. problem is, with just cookies and a name, the user can only put one product in the cart right? lol so i need to use an array in a cookie, so i'm told. can anyone help? if you want me to post the cart, tell me and i will.🆒

    Your best bet is to use tables to store the shopping cart details. Even if you create a script that deletes the row afterwould if needs be.

    Even flatfile systems would work better than cookies (in this case 😃)

      i dont follow you, but i'm gonna post my cart to show you how its set up, dont laugh.. lol.

      <?
      
      $quantity = abs($quantity);
      $cost = abs($_POST['cost']);
      $totalcost = $cost * $quantity;
      $dbuser = 'Blade';
      $dbpass = '****';
      //connect to the DB and select the database
      $connection = mysql_connect('localhost', $dbuser, $dbpass) 
      or die(mysql_error());
      
      mysql_select_db('cart', $connection) or die(mysql_error());
      //set up the query
      $query = "SELECT * FROM HN_cart 
      	WHERE product='$product' and cost='$cost' and quantity='$quantity'";
      
      //run the query and get the number of affected rows
      $result = mysql_query($query, $connection) or die("error making query");
      
      $affected_rows = mysql_num_rows($result);
      if($affected_rows > 0){ 
      
      	setcookie("product", $product, time()+1800) or die("error");
      	setcookie("cost", $cost, time()+1800) or die("error");
      	setcookie("quantity", $quantity, time()+1800) or die("error");}
      
      echo "
      
      	<table width=\"163\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">
      <tr>
      	<td align=\"center\"><font size=-2><u>Quantity</u></font></td>
      	<td align=\"center\"><font size=-2><u>Product(s)</u></font></td>
      	<td align=\"center\"><font size=-2><u>Price</u></font></td>
      </tr>
      <tr nowrap>
      	<td align=\"center\"><font size=-2>$quantity x</font></td>
      
      	<td colspan=\"2\" width=\"100\">
      	<font size=-2>$product</font>
      	</td>
      
      	<td><font size=-2></font></td>
      </tr>
      </table>
      <table width=\"163\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">
      <tr><td width=\"100%\"></td></tr>
      <tr>
      	<td></td>
      	<td></td>
          <td align=\"right\"><font size=-2>\$";
      	printf("%01.2f", $totalcost);
      	echo "</font></td>
      </tr>
      	</table>
      	";
      ?>
      

        if you really want to use cookies, try to serialize() it before.. then, unserialize() the array to recover it

          like this, to save the cart:
          $myarray = array();
          $_COOKIE['cart'] = serialize($myarray);

          and then, to get it back:
          $myarray = unserialize($_COOKIE['cart']);

          i don't if that was what you were asking for, but anyways...

            Just try to visit this article, may be it can shed some lite.

            Look into user's comments.

              I still think the DB method is more reliable.

              If you are saying that your problem is that the users can only temporarily select 1 product before saying "yes, I wish to add that to my cart", then read on for the solution. Even if you are just wishing to store the products in a temporary place, a DB run system would be a much better idea.

              You can temporarily create tables, then just copy them into a fixed DB if and when they are needed.

              Cookies can be cleared by the user and hve all sorts of nasty thingys to do with them. 😃

              Use a db!

                serialize() to transfer array to string, and save the string in cookie.

                later on read from the string and unserialize() the string back to the array.

                i think you should use both database and this approach. use the serialize() / unserialize() approach save the product code and quantity. and then get the rest of the information from the database.

                  Write a Reply...