heartonsleeve Posted April 17, 2006 Share Posted April 17, 2006 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 https://forums.phpfreaks.com/topic/7638-serialize-and-unserialize/ Share on other sites More sharing options...
kenrbnsn Posted April 17, 2006 Share Posted April 17, 2006 Instead of [code]<?php print_r ($order); ?>[/code] use [code]<?php echo '<pre>' . print_r($order,true) . '</pre>'; ?>[/code]This will format the dumped array better.Ken Link to comment https://forums.phpfreaks.com/topic/7638-serialize-and-unserialize/#findComment-27856 Share on other sites More sharing options...
heartonsleeve Posted April 18, 2006 Author Share Posted April 18, 2006 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 https://forums.phpfreaks.com/topic/7638-serialize-and-unserialize/#findComment-28019 Share on other sites More sharing options...
heartonsleeve Posted April 19, 2006 Author Share Posted April 19, 2006 *bump*Anything else that I can do? Link to comment https://forums.phpfreaks.com/topic/7638-serialize-and-unserialize/#findComment-28428 Share on other sites More sharing options...
litebearer Posted April 19, 2006 Share Posted April 19, 2006 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><?PHPfor ($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 https://forums.phpfreaks.com/topic/7638-serialize-and-unserialize/#findComment-28466 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.