codeigniter extract stdClass对象

时间:2013-11-14 07:19:40

标签: php codeigniter

我正在使用codeigniter,这是我的观点:

        <select name="kode_promo_air" id="kode_promo_air" class="span3 input2">
            <option value="">-PILIH-</option>
            <?php
            $data = $this->app_model->get_promo_air();  

            foreach($data->result() as $t){
            ?>
                <option value="<?php echo $t->PRO_PRICE_CODE;?>"><?php echo $t->PRO_MST_NM;?> - <?php echo $t->PRO_AMT_CUST;?></option>
            <?php
            }
            ?>
        </select>   

这里是app_model - &gt; get_promo_air()

function get_promo_air()
{
    $sql = "
                SELECT
                    c.PRO_PRICE_CODE,
                    a.PRO_MST_NM,
                    c.PRO_AMT_CUST
                FROM
                    promo_master a
                inner join  promo_type b on a.pro_type_id = b.pro_type_id
                inner join  promo_price c on a.PRO_MST_ID = c.PRO_MST_ID 
                WHERE sysdate() BETWEEN a.PRO_PRICE_DATE_FROM
                AND a.PRO_PRICE_DATE_TO
                AND b.PRO_TYPE_ID = 1       
            ";

    return $this->db->query($sql);  

}

问题是我无法在我的视图中获得$ data-&gt; result(),当我调试时:

<?php
                    $data = $this->app_model->get_promo_air();  

                    echo "<pre>";
                    print_r($data->result());
                    die();

?>              

                <select name="kode_promo_air" id="kode_promo_air" class="span3 input2">
                    <option value="">-PILIH-</option>
                    <?php
                    $data = $this->app_model->get_promo_air();  

                    foreach($data->result() as $t){
                    ?>
                        <option value="<?php echo $t->PRO_PRICE_CODE;?>"><?php echo $t->PRO_MST_NM;?> - <?php echo $t->PRO_AMT_CUST;?></option>
                    <?php
                    }
                    ?>
                </select>

我有:

Array
(
    [0] => stdClass Object
        (
            [PRO_PRICE_CODE] => AAA001001
            [PRO_MST_NM] => Promo Air Asia
            [PRO_AMT_CUST] => 65000.00
        )

    [1] => stdClass Object
        (
            [PRO_PRICE_CODE] => AGI001001
            [PRO_MST_NM] => Promo Garuda
            [PRO_AMT_CUST] => 40000.00
        )

)

如何获得价值?因为我尝试使用foreach($data->result() as $t)它无法获取数据......任何解决方案都会受到赞赏..

8 个答案:

答案 0 :(得分:5)

尝试返回result array之类的

return $this->db->query($sql)->result_array();

或者在foreach循环中尝试

foreach ($data->result_array() as $t) {
    // Do the stuff
}

答案 1 :(得分:2)

return result_array(); 这将解决问题。

输出

阵 (     [0] =&gt;排列         (             [id] =&gt; 1             [name] =&gt;杰瑞德莱托         )

[1] => Array
    (
        [id] => 2
        [name] => Scott Eastwood
    )

[2] => Array
    (
        [id] => 3
        [name] => Kate Mara
    )

[3] => Array
    (
        [id] => 4
        [name] => Holland Roden
    )

答案 2 :(得分:0)

试试这个:

$sql = "SELECT
             c.PRO_PRICE_CODE
             a.PRO_MST_NM,
             c.PRO_AMT_CUST
                FROM
                    promo_master a
                inner join  promo_type b on a.pro_type_id = b.pro_type_id
                inner join  promo_price c on a.PRO_MST_ID = c.PRO_MST_ID 
                WHERE sysdate() BETWEEN a.PRO_PRICE_DATE_FROM
                AND a.PRO_PRICE_DATE_TO
                AND b.PRO_TYPE_ID = 1       
            ";

$query = $this->db-query($sql);
return $query;

return $query->result();

如果您使用return $query->result(),则只需要foreach中的变量名称。

$data = $this->app_model->get_promo_air(); 

foreach($data as $t){

//Your code here

}

答案 3 :(得分:0)

$query->result();=$t=

Array
(
    [0] => stdClass Object
        (
            [PRO_PRICE_CODE] => AAA001001
            [PRO_MST_NM] => Promo Air Asia
            [PRO_AMT_CUST] => 65000.00
        )

   [1].........
------------------------------------------------------------------------------------

$t[0]=stdClass Object
        (
            [PRO_PRICE_CODE] => AAA001001
            [PRO_MST_NM] => Promo Air Asia
            [PRO_AMT_CUST] => 65000.00
        )
-------------------------------------------------------------------------------------------
$t[0]->PRO_PRICE_CODE; = AAA001001
----------------------------------------------------------------------------------------

答案 4 :(得分:0)

我有另一种解决方案。

执行时,row_array返回两倍的期望结果。例如,如果content是输入,则输出为content content。然后我将|放在$lastone中,以便将爆炸移除并将两者分开。

if ($query->num_rows() > 0)
    {
        $row = $query->row_array(); 

        $lastone= $row['column']."|";
    }

    list ($last, $before) = explode ("|", $lastone);

return $last;

答案 5 :(得分:0)

型号 - &gt; 在app_model中,返回这样的结果。

return $this->db->query($sql)->result();

查看 - &gt; 那么你可以得到这样的数据。

<?php foreach($data as $key): ?>

           <p>PRO_PRICE_CODE : <?php echo $key->PRO_PRICE_CODE; ?></p>

<?php endforeach; ?>

答案 6 :(得分:0)

数组 (     [0] => stdClass对象         (             [PRO_PRICE_CODE] => AAA001001             [PRO_MST_NM] =>亚洲促销航空             [PRO_AMT_CUST] => 65000.00         )

GeometryFactory factory = new GeometryFactory();
ArrayList<Coordinate> coordList = new ArrayList<>();
//...track mouse clicks to fill arrayList
// close loop
coordList.add(coordList.get(0));
//create polygon
Polygon poly=factory.createPolygon(coordList.toArray(new Coordinate[] {}));

您可以使用进行单独打印 打印($ data_stdClass [0]-> PRO_PRICE_CODE);

答案 7 :(得分:0)

print_r($data['0']->PRO_PRICE_CODE);  //you will get AAA001001
print_r($data['0']->PRO_PRICE_CODE);  //you will get AAA001001
print_r($data['0']->PRO_MST_NM);      //you will get Promo Air Asia
print_r($data['0']->PRO_AMT_CUST);    //you will get 65000.00

类似地,

print_r($data['1']->PRO_PRICE_CODE);  //you will get AGI001001