CodeIgniter Forums
I cannot use result() or result_array() on a variable that has been passed to the vie - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: I cannot use result() or result_array() on a variable that has been passed to the vie (/showthread.php?tid=68453)



I cannot use result() or result_array() on a variable that has been passed to the vie - desbest - 07-12-2017

Say I have this code.

debate.php


Code:
<?php
            $approval = $this->db->get('posts');
            foreach ($approval->result() as $approval)
            {
                echo $approval->content;
            }
            $datax = array(
                    'traffic' => $approval,
                    'nodeview_id' => "$debate[id]",
                    'avenue' => "debate",
                    'whichside' => "acceptside"
            );

            $this->load->view('_comments', $datax);
            ?> 

And inside the _comments.php view there's

Code:
<?php
foreach ($traffic->result() as $approval)
            {
                echo $approval->content;
            }
?>

What happens is that the content from the database shows in the debate.php file, but not in the _comments.php file, when it should as I'm dealing with the same variable.

What is going on?


RE: I cannot use result() or result_array() on a variable that has been passed to the vie - krystian2160 - 07-12-2017

Do

$this->load->view('_comments', array('datax' => $datax));

and in view

var_dump($datax);

will work.


read the documentation Smile


RE: I cannot use result() or result_array() on a variable that has been passed to the vie - Martin7483 - 07-13-2017

If that is the code your are using, then you are overwriting the $approval variable.

PHP Code:
$approval$this->db->get('posts');
foreach (
$approval->result() as $approval// Your overwriting the $approval variable here
{
 
   echo $approval->content;
}
$datax = array(
 
   // So when this line is hit $approval is the last object from the foreach loop, and not the DB object
 
   'traffic' => $approval,
 
   'nodeview_id' => "$debate[id]",
 
   'avenue' => "debate",
 
   'whichside' => "acceptside"
);

$this->load->view('_comments'$datax); 

Change it to this and it should work
PHP Code:
$query $this->db->get('posts'); // Renamed $approval to $query
foreach ($query->result() as $approval// Now using $query instead of $approval
{
 
   echo $approval->content;
}
$datax = array(
 
   'traffic' => $query// Assigned $query instead of $approval
 
   'nodeview_id' => "$debate[id]",
 
   'avenue' => "debate",
 
   'whichside' => "acceptside"
);

$this->load->view('_comments'$datax); 



RE: I cannot use result() or result_array() on a variable that has been passed to the vie - rtenny - 07-13-2017

Should it in the view not be:

<?php
foreach ($traffic as $approval)
{
echo $approval->content;
}
?>

the ->result() is for a database object not an array