0

i'm using codeigniter, here's my view :

        <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>   

and here's app_model -> 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);  

}

the problem is i can't get $data->result() in my view, when i debug :

<?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>

i've got :

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
        )

)

how to get the value ? coz i try with foreach($data->result() as $t) it's can't get the data... any solution would be appreciated..

1
  • Have provided with a solution for the request that you have asked the question. Have a try and share thoughts. Commented Oct 7, 2016 at 5:29

8 Answers 8

5

Try to return result array like

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

Or in foreach loop try like

foreach ($data->result_array() as $t) {
    // Do the stuff
}
Sign up to request clarification or add additional context in comments.

4 Comments

just print the $data and tell me
just like above sir, here's 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 ) )
Then try with my second answer
i'll try again, it's get error : Trying to get property of non-object
3

return result_array(); this will solve the problem.

output

Array ( [0] => Array ( [id] => 1 [name] => Jared Leto )

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

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

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

)

Comments

0

Try this:

$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;

or

return $query->result();

If you use return $query->result(), you would only need the variable name in foreach.

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

foreach($data as $t){

//Your code here

}

Comments

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
----------------------------------------------------------------------------------------

Comments

0

I have an alternative solution.

When executed, the row_array is returns twice as many desired results. For example, if content is the input, the output is content content. Then I put the | in the $lastone, to remove with the explosion and separate the two.

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

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

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

return $last;

Comments

0

model --> in app_model, return result like this.

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

View --> then you can get data like this.

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

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

<?php endforeach; ?>

Comments

0

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
    )

)

You can print individually using print($data_stdClass[0]->PRO_PRICE_CODE);

Comments

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

Similarly,

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.