Jump to content


Photo

Serialize and Unserialize


  • Please log in to reply
4 replies to this topic

#1 heartonsleeve

heartonsleeve
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 April 2006 - 08:43 PM

I'm working on a shopping cart and I've just about got everything set up and ready to go but one thing I'd like to fix is the way I can view what the customer ordered.

They way the order has been recorded to the database is by serializing the multiple products someone has ordered. Then I unserialize it to view it and it just looks kind of strange. I can tell what's been ordered but is there anyway I can make it a little easier to read.

what it looks like when I view it unserialize:
Array ( [3] => Array ( [qty] => 1 [name] => Windmill Shirt [size] => small ) [2] => Array ( [qty] => 1 [name] => Robot Shirt [size] => small ) [1] => Array ( [qty] => 1 [name] => Goat Shirt [size] => small ) )

and here's the code to serialize:
<?php

$get_cart = mysql_query("SELECT * FROM
                shopping_carts
                WHERE cart_identifier='$cart_id'");
                              
while($row = mysql_fetch_array($get_cart)){
      mysql_query("UPDATE shopping_products
      SET product_qty = (product_qty - {$row['product_qty']})
      WHERE product_id ='{$row['product_id']}'");
      
$storage_array[$row['product_id']]['qty'] = $row['product_qty'];
$storage_array[$row['product_id']]['name'] = $row['product_title'];
$storage_array[$row['product_id']]['size'] = $row['size'];

      }

$sproducts = serialize($storage_array);

        mysql_query("INSERT INTO shopping_cart_orders
              (order_date, token, products, total, type, 
              user_id, name, email, st_address, st_address2,
              city, state, zip, country, zone, international)
              VALUES (now(), '$token', '$sproducts', '$complete_total',
              '$type', '{$_SESSION['userid']}', '$name', '$email', 
              '$st_address', '$st_address2', '$city', '$state', '$zip', 
              '$country', '$zone', '$international')") or die (mysql_error());
        
        // Empty the shopping cart             
        //$cart->empty_cart();

?>

And here's the code to unserialize
<?
      $sql = mysql_query("SELECT *
                   FROM shopping_cart_orders 
                   ORDER BY orderid");
      while($row = mysql_fetch_array($sql)){
         stripslashes(extract($row));
         $order=unserialize($products);      
         echo "<strong>$name made an order. They ordered the following:</strong><br />";
         print_r ($order); 
         echo('<br /><br />');
         echo "<strong>Ship To:</strong><br />
                $name<br />
               $st_address 
               $st_address2<br />
               $city, $state<br />
               $zip<br />
               $country<br /><br />";
         echo "<strong>Their total was:</strong> $$total<br /><br /><hr style=\"width: 100%; border: solid 1px #000000;\" />";
         
         
      }
      ?>
      <?

Any help would be greatly appreciated. Thanks.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 April 2006 - 09:29 PM

Instead of
<?php print_r ($order); ?>
use
<?php echo '<pre>' . print_r($order,true) . '</pre>'; ?>

This will format the dumped array better.

Ken

#3 heartonsleeve

heartonsleeve
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 18 April 2006 - 03:06 AM

That looks a little better, but is there a way I can get rid of the word "Array" and all the arrows and whatnot? It's not a real huge deal if I can't, but it just seems like there should be a way to get rid of it. Right now it looks like this:

Array
(
[3] => Array
(
[qty] => 1
[name] => Windmill Shirt
[size] => small
)

[2] => Array
(
[qty] => 1
[name] => Robot Shirt
[size] => small
)

[1] => Array
(
[qty] => 1
[name] => Goat Shirt
[size] => small
)

)


It takes up a little more room than I'd like it to.

#4 heartonsleeve

heartonsleeve
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 19 April 2006 - 01:34 AM

*bump*

Anything else that I can do?

#5 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 19 April 2006 - 03:58 AM

Just a thought...

<?PHP

#####################################################
#    presume $order is a multidimensional array. each element 
#    is itself an array containing qty, name, size
#
#    in practice you will get your arrrays from your database

$order[0] = array(1,"Windmill Shirt","Small");
$order[1] = array(2,"Robot Shirt","Small");
$order[2] = array(2,"Goat Shirt","Small");

####################################################
#    count the number of elements in the array $order

$item_count = count($order);

####################################################
#    loop thru the array displaying all of the elements of the 'inner' arrays
#    although example has pre-knowledge of the number of 
#    elements in the 'inner' arrays, you could rewrite the script
#    to add cells as needed

$i = 0;
?>
<table border=1>
   <tr><td>Qty</td><td>Name</td><td>Size</td></tr>

<?PHP

for ($i=0;$i<$item_count;$i++) {
  ?>
   <tr><td><?PHP echo $order[$i][0]; ?> </td><td><?PHP echo $order[$i][1]; ?> </td><td><?PHP echo $order[$i][2]; ?> </td></tr>
  <?PHP
}
?>
</table>
<?PHP

?>

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users