0

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();
        }
    }

I am getting this error.

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.

2
  • Does this answer your question? PHP 'Illegal string offset' in foreach loop Commented Jul 26, 2020 at 10:05
  • Use different identifiers in your loops: foreach ($scheduleMovie as $movie) and foreach ($scheduleTime as $time). Or use $data['scheduleMovies'], since this seems to be a list of movies and then foreach ($scheduleMovies as $scheduleMovie) Commented Jul 26, 2020 at 10:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.