Jump to content

Serialize and Unserialize


heartonsleeve

Recommended Posts

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.

[b]what it looks like when I view it unserialize:[/b]
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:
[code]
<?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();

?>
[/code]

And here's the code to unserialize
[code]
<?
      $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;\" />";
        
        
      }
      ?>
      <?
[/code]

Any help would be greatly appreciated. Thanks.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

Just a thought...

[code]<?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

?>[/code]
Link to comment
Share on other sites

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.