Welcome Guest, Not a member yet? Register   Sign In
Newbie: foreach is duplicating output
#1

[eluser]ShawnMA[/eluser]
New to CI, and really impressed by it. I'm just getting started and out of the gate I've hit a snag that I can't seem to figure out. I believe it has to do with my model, but I'm unsure. My venue table has a 1-to-many relationship with events.

When using foreach loops in my view, I get the returned results displaying twice. Each event is in the database once, and only assigned to one venue (First Mariner Arena). The first time the page loads it appears fine, but in each subsequent load/refresh, it is always displaying double, for example:

Lady Gaga
lady gaga, music, pop
First Mariner Arena
Lady Gaga
lady gaga, music, pop
Verizon Center
U2
u2, pop, music
First Mariner Arena
U2
u2, pop, music
Verizon Center


Any and all help is greatly appreciated.

Model:

Code:
class Event_model extends CI_Model{

function getAllEvents(){              
        $q = $this->db->get('venue as t1, event as t2, tickets as t3');
           if($q->num_rows() > 0){
            foreach($q->result() as $row){
                $data[] = $row;
            }
            return $data;
        }
    }

}

Controller:

Code:
class Site extends CI_Controller{
   function index(){
       $this->load->model('event_model');
       $data['rows'] = $this->event_model->getAllEvents();      
       $this->load->view('home', $data);

   }
}

View:

Code:
<div id="container">
        <p>My view has been loaded!</p>    

        &lt;?php foreach($rows as $r): ?&gt;
       <h2>&lt;?php echo $r->event_name; ?&gt;</h2>
       <div>&lt;?php echo $r->event_keywords; ?&gt;</div>
       <div>&lt;?php echo $r->venue_description; ?&gt;</div>
        &lt;?php endforeach; ?&gt;

    </div>
#2

[eluser]caleblloyd[/eluser]
Code looks OK- I am guessing that this is a problem in your SQL.

Try putting this line at the top of your controller:

Code:
$this->output->enable_profiler(TRUE);

Then copy and paste the SQL in the profiler into a window in phpmyadmin and go to town debugging SQL!
#3

[eluser]ShawnMA[/eluser]
Yep, its SQL alright. Thanks!
#4

[eluser]vitoco[/eluser]
this query makes all rows on all tables join all the other rows/tables
Code:
$q = $this->db->get('venue as t1, event as t2, tickets as t3');

no especific field to join

Saludos
#5

[eluser]rebornishard[/eluser]
Shawn, idk what i must said, u give me light
after got stuck for 15 mins, i find your post, and find out

Code:
$this->load->view('home', $data);

i forgot to put $data on it, and got

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: records

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

now it's clear, move to days 2 Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB