问候,我的名字是Tri。我正在使用mysql php将数据序列化到数据库中。但是在我将数据序列化到数据库之后,我不知道如何使用未序列化将其检索到表中。
在我从数据库中反序列化数据后,当我使用print_r();
方法时,数据转换为数组。
这是我的代码:
$cart_id = 9;
$sql = "SELECT details FROM shop WHERE id_order =".$cart_id;
$result = $this->db->query($sql);
if($result->num_rows()>0){
foreach ($result->result_array() as $row) {
$cart = unserialize($row['details']);
}
echo '<pre>';
echo print_r($cart);
echo '</pre>';
我将序列化数据存储在订单表内的列详细信息中。
但是当我尝试使用unserialized检索序列化的colum详细信息时,我只能使用print_r()显示所有这些。
这是我反序列化后的print_r()
结果代码:
Array
(
[c4ca4238a0b923820dcc509a6f75849b] => Array
(
[rowid] => c4ca4238a0b923820dcc509a6f75849b
[id] => 1
[qty] => 4
[name] => Polo Shirt
[price] => 15
[subtotal] => 60
)
)
我的问题是,我如何将每个项目显示在表格中?
答案 0 :(得分:0)
根据您的问题和示例请求,这应该可以帮到您:
if ( $result->num_rows() > 0 ) {
echo '<table>';
foreach ( $result->result_array() as $row ) {
echo '<tr>';
$cart = unserialize( $row['details'] );
echo '<td>' . $cart['rowid'] . '</td>';
echo '<td>' . $cart['id'] . '</td>';
echo '<td>' . $cart['qty'] . '</td>';
echo '<td>' . $cart['name'] . '</td>';
echo '<td>' . $cart['price'] . '</td>';
echo '<td>' . $cart['subtotal'] . '</td>';
echo '</tr>';
}
echo '</table>';
}
}
答案 1 :(得分:0)
尝试这样的事情
public function shop($id_order) {
$data = array();
$query = $this->db->get_where($this->db->dbprefix . 'shop', array('cart_id' => $id_order));
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $setting) {
$data = unserialize($setting['details']);
}
}
return $data;
}
public function index() {
$results = $this->shop(9);
$data['details'] = array();
foreach ($results as $result)
{
$data['details'][] = array(
'rowid' => $result['rowid'],
'id' => $result['id'],
'qty' => $result['qty'],
'name' => $result['name'],
'price' => $result['price'],
'subtotal' => $result['subtotal']
);
}
$this->load->view('someview', $data);
}
也很好读这个
http://www.codeigniter.com/user_guide/general/models.html#loading-a-model
查看
<tbody>
<?php foreach ($details as $detail) {?>
<tr>
<td><?php echo $detail['id'];?></td>
<td><?php echo $detail['qty'];?></td>
<td><?php echo $detail['name'];?></td>
<td><?php echo $detail['price'];?></td>
<td><?php echo $detail['subtotal'];?></td>
</tr>
<?php }?>
</tbody>