Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] model which return data array
#1

[eluser]souri84[/eluser]
hi everybody,

Sorry for my noob question and my bad english (I'm french - nobody is perfect... ;-D ) I just began with MVC and more precisly with codeIgniter.

To start, it's my code :

Controller :
Code:
<?php

class Visualisation extends CI_Controller

{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function index()
    {
        $this->load->model('sidebar');
        
            $data['title'] = 'titre';
            $data['heading'] = 'titre2';
            $data['menu']= $this->sidebar->get_all_entries();
            $this->load->view('recherche',$data);
    }

}
?>

Model:
I'm using PDO :
Code:
<?php
class Sidebar extends CI_Model
{
    
    function __construct()
    {
        parent::__construct();
    }
    
    public function get_all_entries()
    {
        $query = $this->db->prepare('SELECT CH_TITRE FROM f_sidebar');
    $data = $query->execute();
    $data = $query->fetchAll();
    
    return $data;
    }


}
?>

View:
Code:
<!DOCTYPE html>
&lt;html&gt;
    &lt;head&gt;
            &lt;link rel="stylesheet" href="&lt;?php echo base_url(); ?&gt;css/style.css" /&gt;
            &lt;title&gt;&lt;?php if(isset($title)) echo $title; ?&gt;&lt;/title&gt;
            
    &lt;/head&gt;

    &lt;body&gt;
        
        &lt;header&gt;
            <h3>&lt;?php if(isset($title)) echo $title; ?&gt;</h3>
        &lt;/header&gt;
        
        <div id="sidebar">
        &lt;?php if(isset($menu)) echo $menu; ?&gt;    
        </div>
        
        <div id="content">
        &lt;?php
        if(isset($heading)) echo $heading;
        ?&gt;
        </div>
    
    &lt;/body&gt;
&lt;/html&gt;

My problem is my query returns an array so in my view, it indicates "Array" and that's all...

So my question is how to view datas listed in my database and not "Array" ?

Thanks,

Thibaut
#2

[eluser]jwburnside[/eluser]
If you select all of the columns in your query, it would be similar to this:
Code:
foreach($menu as $menu_item) {

echo $menu_item['title'];

}
#3

[eluser]souri84[/eluser]
GREAT !

it's ok. Here is the final code in my view :

Code:
foreach($menu as $menu_item)
        {

            echo $menu_item[0].'<br/>';

        }

if I had selected more column, 0 will be replaced by 1, 2, 3 etc...

Thanks a lot !

Thibaut




Theme © iAndrew 2016 - Forum software by © MyBB