Welcome Guest, Not a member yet? Register   Sign In
Array display "Array" text
#1

[eluser]clintonbeattie[/eluser]
Hi,

I have created a controller, model and view and get teh correct number of entries from my database displayed in my view, but that all say "Array".

I'm sure this is simple but can someone please help?

Thanks.

Code:
class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index(){
    $data['title'] = "Welcome to my Blog";
    $data['navlist'] = $this->MPosts->getAllPosts();
    $this->load->vars($data);
    $this->load->view('template');
    }
    
}

Code:
class MPosts extends Model {

    function MPosts()
    {
        parent::Model();    
    }
    
    function getAllPosts(){
        $data = array();
        $Q = $this->db->get('posts');
        if ($Q->num_rows() > 0){
        foreach ($Q->result_array() as $row){
        $data[] = $row;
        }
        }
        $Q->free_result();
        return $data;
    }
    
    
    
}
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt; &lt;?php echo $title; ?&gt; &lt;/title &gt;
&lt;link href="&lt;?= base_url();?&gt;css/default.css" rel="stylesheet" type="text/css" / &gt;
[removed]
// < ![CDATA[
base_url = '&lt;?= base_url();?&gt;';
//]] >
[removed]
&lt;/head&gt;
&lt;body&gt;

<div id="nav">
&lt;?php $this->load->view('navigation');?&gt;
</div>

&lt;/body&gt;
&lt;/html&gt;

Code:
&lt;?php
if (count($navlist)){
echo "<ul>";
foreach ($navlist as $id => $intro){
echo "<li>";
echo anchor("welcome/blog/$id",$intro);
echo "</li>";
}
echo "</ul>";
}
?&gt;
#2

[eluser]brianw1975[/eluser]
in your controller do a var_dump on $data['navlist'] to verify the data

i bet you have to do something like

echo anchor("welcome/blog/$id",$intro['name']);
#3

[eluser]fesweb[/eluser]
More likely, it will be:
Code:
foreach ($navlist as $row){
echo "<li>";
echo anchor("welcome/blog/$row['id']",$row['intro']);
echo "</li>";
}
#4

[eluser]fesweb[/eluser]
Also, dBug is a really great debugging tool when it comes to dealing with arrays and objects. Just save it as dbug_helper, load it like any other helper and when there is an array to investigate use it.
Code:
// load it in your controller
$this->load->helper('dBug');

// call it and it will echo a useful table...
new dBug($navlist);
Way more useful than var_dump.




Theme © iAndrew 2016 - Forum software by © MyBB