将json_decode存储在会话变量中

时间:2014-06-02 01:05:25

标签: php json

我正在使用json_encode在表上显示MySQL值。我想将json_encode中的值存储到$_SESSION变量中。

以下是我尝试的内容:

<?php
session_start();
include '../../dbconnect.php';
$result = array();
    foreach($_SESSION['prod'] as $value) {
        //echo $value;
        $rs = mysql_query("select products.product_name as 'product_name', products.product_desc as 'product_desc', order_details.unitcost as 'unitcost', order_details.unitsrp as 'unitsrp', order_details.quantity as 'quantity', order_details.commrate as 'comm', order_details.ma as 'ma', order_details.od_no as 'od_no', `sales-order`.`so-no` as 'sono' from products left join order_details on products.product_id = order_details.product_id left join `sales-order` on order_details.so_number = `sales-order`.`so-number` where order_details.od_no = '$value'");

        while($row = mysql_fetch_object($rs)){
            $result[] = $row;
        }
    }


    echo json_encode($result);
    $data=json_decode($result);
    $qty = $data->quantity;
    $_SESSION['qty']=$qty;

?>

但它给出了一个错误。并且还在我的表中给出错误。我不确定我是否应该encode然后decode放在同一页面上,我对json来说真的很新。

Warning: json_decode() expects parameter 1 to be string, array given in C:\xampp\htdocs\isys\purchasing\po-grid\get.php on line 16

Notice: Trying to get property of non-object in C:\xampp\htdocs\isys\purchasing\po-grid\get.php on line 17

修改

为了进一步说明,我显示包含数据po-grid.php的表的页面调用具有上述代码的get.php数据。它显示以下结果:

[{"product_name":"LEN274357A","product_desc":"Lenovo TP SL400 CD1.86, 1GB, 160GB, DVDRW, DOS\r\n","unitcost":"0.00","unitsrp":"25000.00","quantity":"5","comm":"0.000","ma":"500.00","od_no":"63","sono":"ert"},{"product_name":"HP Pavillion TS 14-n039TU","product_desc":"3rd generation intel Core 13-3217u","unitcost":"0.00","unitsrp":"12000.00","quantity":"3","comm":"0.250","ma":"0.00","od_no":"64","sono":"ASo2"}]

po-grid.php调用get.php来获取数据。

问题是,我只是从here自定义了此代码(您可以查看链接并进行测试,以便了解我所说的内容)并且我不确定代码是怎么做的将数据从json_encode调用到表单数据进行编辑。我认为他们将input名称关联起来,以便在编辑时调用数据。

示例:

<input name="quantity">会显示一个包含数量值的文本框,甚至不会声明它。但我需要的是循环下拉。 <select name=quantity>并非真正有用,它有价值,但我需要的是这样:

<select class="easyui-combobox">
    <?php 
    $qty = $_SESSION['qty']; //this is the part where it calls the variable needed which I'm not sure how to call
    for($x=$qty;$x!=0;$x--)
    {
        echo "<option value='".$x."'>".$x."</option>";  
    }
    ?>
    </select>

如果是quantity = 5,则该选项应为5, 4, 3, 2, 1

1 个答案:

答案 0 :(得分:0)

您没有存储json_encode。你只是将它回显到屏幕上。

尝试:

$encoded_results = json_encode($result);
echo $encoded_results;

$data = json_decode($encoded_results);