njt Posted December 31, 2009 Share Posted December 31, 2009 Hey there all, I have a question regarding php and sql. This code was written by someone else (who broke it) and I've been trying to fix it. I've been stuck on this for about a month now and decided to ask for some help. The objective here is to have the desired information (which has been serialized and put into a sql database) displayed correctly in an excel sheet. Downloading hundreds of excel sheets for testing became annoying so I just commented out the bit that makes the file download (the header()) so now all of the information should just display on the screen. It does, however I can't get the information that I want out of the database and to display properly. From my understanding of php and sql it should work, but alas it does not. Here is the code, and thank you very much for any help I may received <?php session_start(); //ini_set('display_errors', 1); //error_reporting(E_ALL); //check if logged in // include '../engine/engine.access.php'; //include configuration file require_once '../../includes/Config.inc.php'; require_once '../../includes/Database_Tables.inc.php'; //include classes require_once '../../includes/classes/Database.class.php'; require_once '../../includes/classes/Parser.class.php'; require_once '../../includes/classes/ImageResizer.class.php'; require_once '../../includes/classes/FileUploader.class.php'; require_once '../../includes/classes/Paginator.class.php'; $db = new Database(); $db->connect(); $parser = new Parser(); $uploader = new FileUploader(); $resizer = new ImageResizer(); $data = ''; $header = ''; $events_query = $db->query('SELECT', TABLE_ORDERS); /* header("Content-type: application/x-msdownload"); header("Content-Disposition: attachment; filename=order_data.xls"); header("Pragma: no-cache"); header("Expires: 0"); */ // $sql = "select * from orders where order_date > '".date("Y-m-d H:i:s",time()-60*60*24*30)."' and order_id <> 885 order by order_date desc"; /* $res = mysql_query($sql); $cols = array(); while($mr = mysql_fetch_assoc($res)) { //print_r($mr); $mr['order_data'] = unserialize($mr['order_data']); $mr['session_data'] = unserialize($mr['session_data']); if(!$header) { foreach($mr['session_data'] as $key => $val) { if(!in_array($key,array("shoppingcart"))) { $header .= "\t" . $key; } $sess_cols[] = $key; } foreach($mr['order_data'] as $key => $val) { $header .= "\t" . $key; $cols[] = $key; } echo "order_id\tuser_id\torder_date"; echo $header; echo "\r"; } //print_r($cols); //print_r($sess_cols); echo "$mr[order_id]\t$mr[user_id]\t$mr[order_date]"; foreach($sess_cols as $col) { if(!in_array($col,array("shoppingcart"))) { echo "\t".$mr['session_data'][$col]; if($col == 'billing_cc_number'){ $text = $mr['session_data'][$col]; $last = substr($text, strlen($text)-4, 4); echo "\t". $last; } } } foreach($cols as $col) { if($col == "items") { echo "\t"; foreach($mr['order_data'][$col] as $a=>$b) { foreach($b as $c => $val) { echo "($val[qty]x$val[name]),"; } } } else { echo "\t".$mr['order_data'][$col]; } } echo "\r"; } //echo "herE"; exit; */ // print_r($db->fetch_array($events_query)); while($rows = $db->fetch_array($events_query)){ $data = ''; $data .= $rows['event_title'] .' - '; $data .= $rows['event_location'] .' - '; $data .= $rows['event_city'] .' - '; $data .= $rows['event_state'] .' - '; $data .= $rows['event_zip'] ."\r"; $data .= 'First Name'."\t"; $data .= 'Last Name'."\t"; $data .= 'Parent Gaurdian'."\t"; $data .= 'Work/Cell'."\t"; $data .= 'Parent Gaurdian'."\t"; $data .= 'Work/Cell'."\t"; $data .= 'Email'."\t"; $data .= 'Home Phone'."\t"; $data .= 'Address'."\t"; $data .= 'City'."\t"; $data .= 'State'."\t"; $data .= 'Zipcode'."\t"; $data .= 'Age'."\t"; $db->add_where('event_id', $rows['event_id']); $customs_query = $db->query('SELECT', TABLE_CUSTOM_FIELDS_TO_EVENT .' AS cf2e LEFT JOIN '. TABLE_CUSTOM_FIELDS .' AS cf USING (field_id)'); while($rows2 = $db->fetch_array($customs_query)){ $data .= $rows2['field_label'] ."\t"; } if($rows['event_shirt']){ $data .= 'T-Shirt size'."\r"; }else{ $data .= "\r"; } $orders_query = $db->query('SELECT', TABLE_ORDERS); while($row = $db->fetch_array($orders_query)){ $order_data = unserialize($row['order_data']); foreach($order_data['items'] as $item_data){ foreach($item_data as $item){ $week_info = unserialize($item['week_info']); $session_info = unserialize($item['session_info']); if($week_info['event_id'] == $rows['event_id']){ $data .= trim($item['first_name']) ."\t"; $data .= trim($item['last_name']) ."\t"; $data .= trim($item['parent_gaurdian']) ."\t"; $data .= trim($item['work_cell']) ."\t"; $data .= trim($item['parent_gaurdian_2']) ."\t"; $data .= trim($item['work_cell_2']) ."\t"; $data .= trim($item['email']) ."\t"; $data .= trim($item['home_phone']) ."\t"; $data .= trim($item['address']) ."\t"; $data .= trim($item['city']) ."\t"; $data .= trim($item['state']) ."\t"; $data .= trim($item['zip_code']) ."\t"; $data .= trim($item['age']) ."\t"; $db->add_where('event_id', $rows['event_id']); $customs_query = $db->query('SELECT', TABLE_CUSTOM_FIELDS_TO_EVENT .' AS cf2e LEFT JOIN '. TABLE_CUSTOM_FIELDS .' AS cf USING (field_id)'); while($rows2 = $db->fetch_array($customs_query)){ $data .= trim($item['custom_'. $rows2['field_id'] .'']) ."\t"; } if($rows['event_shirt']){ $data .= trim($item['tshirt_size']) ."\r"; }else{ $data .= "\r"; } //echo $session_info['session_start_time'] .' - '; //echo $session_info['session_end_time'] .') <br>'; } } } } $data .= "\r\n"; if ($data == ''){ $data = '\n(0) Records Found!\n'; } echo $header ."\n".$data; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186768-formatting-unserialized-data-in-php/ Share on other sites More sharing options...
RussellReal Posted December 31, 2009 Share Posted December 31, 2009 show us an example of your database.. and possibly some serialized data so we can see how to approach this, because for all we know your serialized data could be an object and not plain text, or an array built inside an array then serialized.. theres no way to KNOW what your data will be.. and with no static information there is no way to accurately help you Quote Link to comment https://forums.phpfreaks.com/topic/186768-formatting-unserialized-data-in-php/#findComment-986299 Share on other sites More sharing options...
njt Posted December 31, 2009 Author Share Posted December 31, 2009 Ah, I'm sorry about that. The events table is pretty standard. No serialized data in it. The orders table has a column named order_data which is where the serialized information is kept. Here is an example of some of it: a:7:{s:11:"total_items";i:2;s:14:"subtotal_price":207.5;s:14:"discount_price";i:0;s:9:"tax_price";i:0;s:14:"shipping_price";i:0;s:11:"total_price":207.5;s:5:"items";a:2:{i:5;a:1:{i:414053;a:29:{s:2:"id";s:1:"5";s:3:"uid";s:6:"414053";s:4:"name";s:49:"2006 Lifeletics Winter Academy - Huntington Beach";s:5:"price";s:3:"195";s:3:"qty":1;s:7:"min_qty";s:1:"1";s:7:"max_qty";s:1:"1";s:8:"subtotal":195;s:14:"allow_decimals";b:0;s:12:"product_type";s:5:"event";s:6:"params";a:19:{s:12:"product_type";s:5:"event";s:8:"event_id";s:1:"5";s:8:"week_num";s:1:"2";s:11:"session_num";s:1:"2";s:9:"week_info";s:188:"a:5:{s:8:"event_id";s:1:"5";s:7:"week_id";s:2:"16";s:10:"week_title";s:30:"2006 Lifeletics Winter Academy";s:15:"week_start_date";s:10:"2006-12-26";s:13:"week_end_date";s:10:"2006-12-30";}";s:12:"session_info";s:214:"a:6:{s:10:"session_id";s:1:"7";s:18:"session_start_time";s:8:"09:00:00";s:16:"session_end_time";s:8:"12:30:00";s:13:"session_price";s:3:"195";s:19:"session_max_members";s:2:"60";s:19:"session_num_members";s:1:"0";}";s:10:"first_name";s:5:"sonny";s:9:"last_name";s:6:"kotler";s:15:"parent_gaurdian";s:4:"toli";s:5:"email";s:21:"sonny@sonnykotler.com";s:10:"home_phone";s:12:"714.408.0448";s:7:"address";s:13:"50 paperwhite";s:4:"city";s:6:"Irvine";s:5:"state";s:2:"CA";s:8:"zip_code";s:5:"92603";s:3:"age";s:2:"22";s:11:"tshirt_size";s:2:"XS";s:8:"custom_4";s:3:"mom";s:8:"custom_5";s:5:"yours";}s:8:"event_id";s:1:"5";s:8:"week_num";s:1:"2";s:11:"session_num";s:1:"2";s:9:"week_info";s:188:"a:5:{s:8:"event_id";s:1:"5";s:7:"week_id";s:2:"16";s:10:"week_title";s:30:"2006 Lifeletics Winter Academy";s:15:"week_start_date";s:10:"2006-12-26";s:13:"week_end_date";s:10:"2006-12-30";}";s:12:"session_info";s:214:"a:6:{s:10:"session_id";s:1:"7";s:18:"session_start_time";s:8:"09:00:00";s:16:"session_end_time";s:8:"12:30:00";s:13:"session_price";s:3:"195";s:19:"session_max_members";s:2:"60";s:19:"session_num_members";s:1:"0";}";s:10:"first_name";s:5:"sonny";s:9:"last_name";s:6:"kotler";s:15:"parent_gaurdian";s:4:"toli";s:5:"email";s:21:"sonny@sonnykotler.com";s:10:"home_phone";s:12:"714.408.0448";s:7:"address";s:13:"50 paperwhite";s:4:"city";s:6:"Irvine";s:5:"state";s:2:"CA";s:8:"zip_code";s:5:"92603";s:3:"age";s:2:"22";s:11:"tshirt_size";s:2:"XS";s:8:"custom_4";s:3:"mom";s:8:"custom_5";s:5:"yours";}}i:7;a:1:{i:7;a:11:{s:2:"id";s:1:"7";s:3:"uid";s:1:"7";s:4:"name";s:36:"Lifeletics T-Shirt: Baseball Is Life";s:5:"price";s:4:"12.5";s:3:"qty":1;s:7:"min_qty";i:0;s:7:"max_qty";b:0;s:8:"subtotal":12.5;s:14:"allow_decimals";b:0;s:12:"product_type";s:7:"product";s:6:"params";a:1:{s:12:"product_type";s:7:"product";}}}}} Quote Link to comment https://forums.phpfreaks.com/topic/186768-formatting-unserialized-data-in-php/#findComment-986312 Share on other sites More sharing options...
RussellReal Posted January 1, 2010 Share Posted January 1, 2010 why don't you just do something like: unserialize and then push a foreach loop on it. because they're arrays that're being serialized.. so doo something like this: <?php $a = unserialize($data); echo "<tr>"; foreach ($a as $v) { echo "<td>{$v}</td>"; } echo "</tr>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/186768-formatting-unserialized-data-in-php/#findComment-986620 Share on other sites More sharing options...
njt Posted January 1, 2010 Author Share Posted January 1, 2010 Doing so, its telling me that there is an array in the unserialized array. This array is ['items'], however when I try to unserialize that array it tells me that it is not an array when it seems to me that it is very obviously one. This is the test code that I wrote which is giving me these errors $sql = "select * from orders"; $res = mysql_query($sql); $mr = mysql_fetch_assoc($res); // echo print_r(unserialize($mr['order_data'])); while($row = mysql_fetch_array($res)){ $order_data = unserialize($row['order_data']); foreach($order_data as $id => $v){ echo " <table border=\"1\"><tr> <td>$id</td><td>$v</td> </tr></table><p></p> "; if(is_array($order_data['items'])){ $item_data = unserialize($order_data['items']); foreach($item_data as $item_id => $item_v){ echo " <table border=\"1\" bordercolor=\"red\"><tr> <td>$item_id</td><td>$item_v</td> </tr></table><p></p> "; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/186768-formatting-unserialized-data-in-php/#findComment-986636 Share on other sites More sharing options...
RussellReal Posted January 1, 2010 Share Posted January 1, 2010 try something like this: $e = unserialize($row['order_data']) print_r($e); and however it shows up thats how you'll hope to use it Quote Link to comment https://forums.phpfreaks.com/topic/186768-formatting-unserialized-data-in-php/#findComment-986722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.