Welcome Guest, Not a member yet? Register   Sign In
Ouputting content from database returns the word Array
#1

[eluser]Unknown[/eluser]
Hello codeigniter community, I am going through some of the examples in the documentation and when I output my database content it only outputs the word Array. I am using codeigniter with XAMPP. Also I am new to all these technologies so it is possible that I could have made a mistake creating my database or made a mistake anywhere else. Here is the code:

Code:
<?php
class Blog extends Controller {
    function Blog(){
        parent::Controller();
        $this->load->model('Blogmodel');
    }

    function index()
    {
        $data['title'] = "My Real Title";
        $data['heading'] = "My Real Heading";
        
        
        //$data['insert'] = $this->Blogmodel->insert_entry();
        $data['query'] = $this->Blogmodel->get_entries();
        $this->load->view('blogview', $data);
    }
}

?>

Code:
?php
class Blogmodel extends Model {

    
    var $name = '';
    var $section    = '';

    function Blogmodel()
    {
        // Call the Model constructor
        parent::Model();
    $this->load->database();
    }
    
    function get_entries()
    {
    
        $query = $this->db->get('links');
    if($query->num_rows()>0){

    // return result set as an associative array

    return $query->result_array();

    }
    }

    function insert_entry()
    {
    $data = array(
               'url' => 'My title' ,
               'name' => 'My Name' ,
               'section' => 'My date'
            );

    $this->db->insert('links', $data);
        
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}
?>

Code:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
    <h1>&lt;?php echo $heading;?&gt;</h1>
    <h2>&lt;?php echo $query;?&gt;</h2>
&lt;/body&gt;
&lt;/html&gt;

Here are my database settings:
Code:
INSERT INTO `montage`.`links` (`name`, `url`, `section`) VALUES ('Jimmy', 'blablabla', 'home');

Thank you for your time :-)
#2

[eluser]BrianDHall[/eluser]
Sure, is extremely easy to fix for you. In your get_entries() you are calling result_array(), which returns...an Array Smile

So if you just try to echo $query PHP will throw an error (your error level probably isn't set to show such lower level notices/errors as they are not critical) and output Array. Try this in your view:

&lt;?php var_dump($query);?&gt;

This will let you know the structure of the array you are dealing with, thus you can determine if you need to use a foreach, acccess an element directly (like, say, $query['name']), or if you would prefer to use a result method other than result_array()
#3

[eluser]Unknown[/eluser]
Thanks BrianDHall that helped a lot. :coolsmile:




Theme © iAndrew 2016 - Forum software by © MyBB