I am trying to loop data from database. I am doing nested loop here. The outer loop is looping the movie name while the inner loop is looping the time when the movie is available for showing.
Below is the code where I want to display the output.
<?php foreach ($scheduleMovie as $scheduleMovie) : ?>
<div class="border-movie">
<h3><?php echo $scheduleMovie['movieName']; ?></h3>
<div class="row container">
<?php foreach ($scheduleTime as $scheduleTime) : ?>
<?php if($scheduleMovie['movieID'] == $scheduleTime['movieID']) : ?>
<button class="btn btn-outline-secondary"><?php echo $scheduleTime['scheduleTime']; ?>
</button>
<?php endif; ?>
<?php endforeach; ?>
<!-- <?php foreach ((array) $test as $test) : ?>
<button class="btn btn-outline-secondary"><?php echo $test; ?></button>
<?php endforeach; ?> -->
</div>
</div>
<?php endforeach; ?>
This is where I set the variable.
<?php
class Schedule extends CI_Controller{
public function index(){
$data['scheduleMovie'] = $this->schedules_model->get_allschedules_Movie();
$data['scheduleTime'] = $this->schedules_model->get_allschedules_Time();
$data['test'] = array(1, 2, 3, 4);
$this->load->view('templates/header');
$this->load->view('schedule/index', $data);
$this->load->view('templates/footer');
}
public function allschedule(){
}
}
This is where I get the data from database.
<?php
class Schedules_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_allschedules_Movie(){
$this->db->select("movie.movieID, movie.movieName");
$this->db->from('schedule');
$this->db->join('movie', 'schedule.movieID = movie.movieID');
$this->db->join('hall', 'hall.hallID = schedule.hallID');
$this->db->group_by('movie.movieID');
$query = $this->db->get();
return $query->result_array();
}
public function get_allschedules_Time(){
$this->db->select("schedule.movieID, scheduleTime");
$this->db->from('schedule');
$this->db->join('movie', 'movie.movieID = schedule.movieID');
$this->db->join('hall', 'hall.hallID = schedule.hallID');
$query = $this->db->get();
return $query->result_array();
}
}
The first loop is successful but the next loop is where the error comes in. I have also tried to load the 'test' array where it hold 1,2,3,4. The same problem occurred, it only manage to load the first loop and not the next.
foreach ($scheduleMovie as $movie)andforeach ($scheduleTime as $time). Or use$data['scheduleMovies'], since this seems to be a list of movies and thenforeach ($scheduleMovies as $scheduleMovie)