Welcome Guest, Not a member yet? Register   Sign In
DB Query Return issue
#1

[eluser]Unknown[/eluser]
Hello CI Forums. I am fairly new to CodeIgniter, and recently decided to switch over to it to make developing in PHP easier.

For the CI site I'm developing right now, I have one Controller, bachmax.php. It's supposed to, and from the looks of it, successfully loads the Model Data and the View home (data array passed to it).
Code:
<?php
class Bachmax extends CI_Controller
{
    function index()
    {
        $this->load->model('Data', '', TRUE);
        $data['notice_title'] = $this->Data->notice_title();
        $data['notice_body'] = $this->Data->notice_body();
        
        $this->load->view('home', $data);
    }
}
?>

There's also the Model, data.php. The functions are supposed to pull a title and body of a notice IF they exist, and pass it back to the Controller.

Code:
<?php
class Data extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    
    function notice_title()
    {
        $this->db->select('title');
        $query = $this->db->get('notice');
        if($query->num_rows() > 0)
        {
            $notice_title = $query->result();
            return $notice_title;
        }
        else
            return 0;
    }
    function notice_body()
    {
        $this->db->select('body');
        $query = $this->db->get('notice');
        if($query->num_rows() > 0)
        {
            $notice_body = $query->result();
            return $notice_body;
        }
        else
            return 0;
    }
}
?>

Finally, here's the snippet of the notice box from the View.
Code:
<?php if($notice_title && $notice_body):?>
    <div id="important">
    <div id="importanthead"><h1>&lt;?php echo$notice_title; ?&gt;</h1></div>
    <div id="importantbody">
    <p>&lt;?php echo $notice_body; ?&gt;</p>
    </div>
    </div>
    &lt;?php endif; ?&gt;

The database is queried, and the code works in a sense that it returns something if it's there. But all it returns - literally - is "Array."

I've checked through the User Guide for the past couple of hours to see if anything was incorrect, but I can't find anything that solves the problem.

Any help would be greatly appreciated. Thanks!
#2

[eluser]Twisted1919[/eluser]
Of course it is returning an array() because you are doing it wrong, you don't query a single row, you query the entire table, therefore it might return an array of results.
so here is how you can do it:
Code:
&lt;?php
class Data extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    
    // this returns a single row of data
    function notice_title_row()
    {
        $query=$this->db->select('title')->from('notice')->order_by('some_id','desc')->limit(1)->get();
        if($query->num_rows() > 0)
            return $query->row();//returns an object accessible via $object->title
        else
            return false;
    }
    // this returns an array of rows and you can loop through result.
    function notice_title_rows()
    {
        $query=$this->db->select('title')->from('notice')->order_by('some_id','desc')->get();
        if($query->num_rows() > 0)
            return $query->result();//returns an array of objects
        else
            return false;
    }

}


now, in the controller you can test like:

if($row=$this->data->notice_title_row())
{
   echo $row->title;
}

and
if($rows=$this->data->notice_title_rows())
{
   foreach($rows AS $row)
   {
      echo $row->title;
   }
}


Read the user guide regarding the database query results and the way they are formatted when returned.
#3

[eluser]Unknown[/eluser]
Thanks. I wasn't completely sure about the return on row() and result(). It works great. Big Grin
#4

[eluser]cideveloper[/eluser]
This really cleans up your notice_title function

Code:
function notice_title()
    {
        return $this->db->select('title')->limit(1)->get('notice')->row()->title;
    }

and why are you querying notice table twice, once to get the title and once to get the body? Couldn't you just do this

model
Code:
function notice_title_body()
    {
        return $this->db->limit(1)->get('notice')->row();
    }

controller
Code:
function index()
    {
        $this->load->model('Data', '', TRUE);
        $data['notice'] = $this->Data->notice_title_body();
        $this->load->view('home', $data);
    }

view
Code:
&lt;?php if($notice):?&gt;
    <div id="important">
    <div id="importanthead"><h1>&lt;?php echo $notice->title; ?&gt;</h1></div>
    <div id="importantbody">
    <p>&lt;?php echo $notice->body; ?&gt;</p>
    </div>
    </div>
&lt;?php endif; ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB