Welcome Guest, Not a member yet? Register   Sign In
Single Database Result
#1

[eluser]webdezzo[/eluser]
Hey all,

I have an issue where I am pulling a single record from my database. It returns the record just fine as :

Code:
Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [news_date] => 1185384665
            [news_title] => The Test Title
            [news_sub_title] => The Test Sub Title

by using the following code within my controller:

Code:
function details($id){
            
            // Load Model
            $this->load->model('News_model','News');
            $main_data['news_details'] = $this->News->list_news($id);
            
            // Template Information
            $template_data['header']         = $this->load->view('content/header', array(), true);
            $template_data['navigation']     = $this->load->view('content/navigation', array(), true);
            $template_data['main_content']     = $this->load->view('content/news_details', $main_data, true);
            
            
            // Load Template
            $this->load->view('templates/subpage', $template_data);
            
            
        }

which is fine. My problem is when I want to echo out my result within in my view, I really want to be able to just echo something like this:

Code:
<?= $news_details->$detail->news_title; ?>

instead of having to loop through a foreach each time like this:

Code:
<? foreach($news_details as $detail){ ?>
    <?= $detail->news_title ?>
<? } ?>

in order to get my single record to spit out. Am I missing something? Or is there a better way to do this?

Thanks in advance!

-undecisive
#2

[eluser]Phil Sturgeon[/eluser]
You may wish to replace your list_news function with a get_news function. get news will be usnig $query->row(); which returns one value, instead of $query->result();

Or you can do:

Code:
$this->load->model('News_model','News');
$news = $this->News->list_news($id);
$main_data['news_details'] = news[0];

But thats a nasty dirty method which goes against the point of models.




Theme © iAndrew 2016 - Forum software by © MyBB