Welcome Guest, Not a member yet? Register   Sign In
Pulling a single row and displaying bits of it in multiple views
#1

[eluser]incog03[/eluser]
Hi Everyone

I'm trying to pull information from a single row in the database and display it in multiple views. In the model i have
Code:
$pagey   = $pre->pageyinfo;
and CI is telling me I can't do this on a non-object - is there a way to fix this or anyway I can work around it? The code works fine except for the line above (and x5 times below).


Preview.php Model
Code:
function getPreview(){
    
    $pre = $this->db->get('deals', 1);
    
    if($pre->num_rows() > 0){
        foreach($pre->result() as $row){
            $data[] = $row;
        }
    }  

      foreach($row as $pre){
        $pagex        = $pre->pageXinfo;
        $pagex2       = $pre->pagex2info;
        $pagey        = $pre->pageyinfo;
        $pagey2       = $pre->pagey2info;
        $pagez        = $pre->pagezinfo;
        $pagez2       = $pre->pagez2info;
    }
    
    $data['pagex'] = $pagex;
    $data['pagex2'] = $pagex2;
    $data['pagey'] = $pagey;
    $data['pagey2'] = $pagey2;
    $data['pagez'] = $pagez;
    $data['pagez2'] = $pagez2;

    
    return $data;
}

In the controller I have this;
preview.php
Code:
function index{

$data = $this->previewm->getPreview();
$this->load->view('header', $data);
$this->load->view('bodytop', $data);
$this->load->view('bodybottom', $data);
$this->load->view('footer', $data);

}

So I can dynamically update say the title in the view by;
header.php
Code:
<title><?php echo $pagex ?></title>


Any possible solutions would be fantastic, the reason I want to do this is because I found having a foreach loop stretching throughout all the views to be really messy I guess.
Maybe the pros do it like that but I don't know...
#2

[eluser]Bart Mebane[/eluser]
Model:
Code:
function getPreview(){
    
    $pre = $this->db->get('deals', 1);
    
    if($pre->num_rows() > 0){
        return $pre->row_array();
    }
    else {
        return array('pagexinfo'=>'', 'pagex2info'=>'',
            'pageyinfo'=>'', 'pagey2info'=>'', 'pagezinfo'=>'', 'pagez2info'=>'');
    }
}
View:
Code:
<title><?php echo $pagexinfo ?></title>
#3

[eluser]Nick_MyShuitings[/eluser]
Even simpler:

Model:

Code:
//If you know its only going to be one row cuz your sql mandates
return $this->db->get('deals',1)->row()

Controller:

Code:
$data['pageinfo'] = $this->previewm->getPreview();
//or
$pageinfo = $this->previewm->getPreview();
$data['pagex'] = $pageinfo->pageXinfo;
//etc
View:

Code:
<?= $pageinfo->pageXinfo ?> OR <?= $pagex ?>
#4

[eluser]InsiteFX[/eluser]
If you use the below code it will allow $data to all views.

Code:
function index
{
    $data = $this->previewm->getPreview();

    $this->load->vars($data);    

    $this->load->view('header');
    $this->load->view('bodytop');
    $this->load->view('bodybottom');
    $this->load->view('footer');
}

InsiteFX
#5

[eluser]incog03[/eluser]
Thanks a lot for your help guys, I got it working now. Very much appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB