0

Im trying to send an array of data from the model to the controller but when I echo on controller, I only see the first value no all the array of the values. Whats wrong?

Model:

        $sql = "
        SELECT car_id
        FROM cars
        WHERE price= 1";

        $res = mysql_query($sql);

        $bought_cars= array();
        while ($row_ya = mysql_fetch_object($res)) 
        {
            $bought_cars[] = $row_ya->car_id;
        }
        foreach ($bought_cars as $bought_car) 
        {
                return $bought_car;
        }

Controller

$bought_car = $this->car_model->car();

echo($bought_car);

Thank you in Advance!

3
  • you should use CI active record db library and $bought_cars is array so why are not returning whole array? Commented Aug 12, 2014 at 9:03
  • foreach ($bought_cars as $bought_car) { return $bought_car; } i think this will return you the last carid am i right Commented Aug 12, 2014 at 9:04
  • Possible duplicate of codeigniter passing array data from model to controller Commented Jul 1, 2017 at 17:30

5 Answers 5

2

You have to use Codeigniter's Active Record Query. In Model (recommended) :

$this->db->select('car_id');
$this->db->from('cars');
$this->db->where('price',1);
$query=$this->db->get();
return $query->result();

Updated Without Active record (Not recommended) :

$sql = "
        SELECT car_id
        FROM cars
        WHERE price= 1";

        $res = mysql_query($sql);

        $bought_cars= array();
        while ($row_ya = mysql_fetch_object($res)) 
        {
            $bought_cars[] = $row_ya->car_id;
        }
        return $bought_cars;//no need to run foreach, you just have to return the array after completing your push operation.
Sign up to request clarification or add additional context in comments.

6 Comments

It isn't possible to do it without Codeigniter's Active Record Query?
But you can do it with Active records ,so no need to use the normal ones.Just change your model function with my code and you will get the array of car_id.
But the problem is that when I try to echo on Controller, dont write all the array.
if you made changes to return array than you have to use print_r($bought_car) to print the array.
With print_r($bought_car) I got the values like this : [0] => 232453 [1] => 212345 [2] How can I do to got them like 232453,212345?
|
0

you can also use this way like:-

$sql = "SELECT car_id FROM cars WHERE price= 1";
$query = $this->db->query($sql);
return $query->result();

Comments

0

You're using return in your model, which is causing the loop/function to end and only return the first value within your array. Just remove your foreach loop entirely and simply return $bought_cars within your model.

In order to show all the values of your array within your controller you'd have to loop through it.

foreach($bought_car as $car) {
    echo $car . '<br>';
}

Or, if you want to make it more readable you can use:

<pre><?php print_r($bought_car); ?></pre>

Comments

0

You can use active record to achieve this:

Model

    $this->db-select('car_id')
    $this->db->where('price', 1);               
    $query = $this->db->get('cars'); if($query->num_rows() > 0){
         foreach ($query->result() as $row)
         {
            $data[] = $row;
         }
        return $data;
    }

Controller

    $bought_car = $this->car_model->car(); foreach($bought_car as $car) { echo $car->car_id

}

Remember you SELECTED only car_id so that is the only available column to echo

Comments

0

It ends in first loop returning the first value it gets that's why you get a first value while echoing on controller.Rather do this to return a whole array.

while ($row_ya = mysql_fetch_object($res)) 
    {
        $bought_cars[] = $row_ya->car_id;
    }
return $bought_cars;

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.