Welcome Guest, Not a member yet? Register   Sign In
Display data from Array
#1

[eluser]sbarow[/eluser]
Hi,

Very new to Codeigniter.

I have a controller that gets the title of a product category

Code:
$this->Product_model->get_title($id);

the result is the returned as an array, I want to make this array equal to my heading i.e.

Code:
$data['heading'] = $this->Product_model->get_title($id);

in my view in order to show the result I have to use a foreach statement
Code:
foreach($sub_heading as $item) {
    $sub_title = $item->title;
}

This just seems to be a long way round, is there a simpler, cleaner way of doing this, such as
Code:
$title = $this->Product_model->get_title($id);
$data['heading'] = $title;

Then in the view just echo $heading?

Look forward to seeing what can be done, thanks.
#2

[eluser]InsiteFX[/eluser]
Code:
$data['heading'] = $this->Product_model->get_title($id);

For your other problem just use the Active Record LIMIT
to limit it by 1

InsiteFX
#3

[eluser]sbarow[/eluser]
Okay but then what would the echo statement in the view look like. That is what I am struggling with.
#4

[eluser]InsiteFX[/eluser]
echo statement:
Code:
<?php echo $heading;?>

View code:
Code:
$this->load->view('your_view_name', $data);

// or
$this->load->vars($data);
$this->load->view('your_view_name');

InsiteFX
#5

[eluser]sbarow[/eluser]
See this is the problem I am having, if I just echo $heading all that is displayed is "array" hence I have to use the foreach statement to run through the array and get the desired heading i.e. title. I want to know if there is an easier way of doing this?
#6

[eluser]InsiteFX[/eluser]
Show me your code for your model $this->Product_model->get_title($id);

InsiteFX
#7

[eluser]sbarow[/eluser]
Code:
function get_title($id)
    {
        $this->db->select('title');
        $query = $this->db->get_where('Products', array('id' => $id));
        return $query->result();
    }

I am unsure about the return statement.
#8

[eluser]reidz[/eluser]
you bet because return
Code:
return $query->result();
return array
so all you have to do is jut edit your get_title($id) this code below to give you the value not in array like you did
Code:
function get_title($id)
    {
        $this->db->select('title');
        $query = $this->db->get_where('Products', array('id' => $id));
        $query = $query->row(); // take the first row
        $return = $query->column_name;
    }
let me know if this is jut not works
#9

[eluser]vitoco[/eluser]
i think it must be

Code:
function get_title($id)
    {
        $this->db->select('title');
        $query = $this->db->get_where('Products', array('id' => $id));
        $query = $query->row(); // take the first row
        $return = $query->title; // <---- title is the name of the column returned
    }




Theme © iAndrew 2016 - Forum software by © MyBB