Welcome Guest, Not a member yet? Register   Sign In
I cannot use result() or result_array() on a variable that has been passed to the vie
#1

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?
Reply
#2

(This post was last modified: 07-12-2017, 12:32 PM by krystian2160.)

Do

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

and in view

var_dump($datax);

will work.


read the documentation Smile
Reply
#3

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); 
Reply
#4

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
On the package it said needs Windows 7 or better. So I installed Linux.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB