First of all, I apologize for my bad English. I have 2 array. First one is Sections and second one Lessons. I'm trying to get $next_lesson id element using this code:
foreach ($sections as $key => $section):
$lessons = $this->crud_model->get_lessons('section', $section['id'])->result_array();
foreach ($lessons as $key => $lesson):
$current_lesson = $lesson['id'];
$next = $lessons[$key+1];
$next_lesson = $next['id'];
endforeach;
endforeach;
Javascript redirect:
window.location.href = "<?php echo site_url('home/lesson/')."/".$course_id."/".$next_lesson; ?>";
but after 1 lesson loop $next_lesson getting first value of section 1. What should I do to move to the second part when the last lesson of the first part is over?
I'd say it is more about data structure in the database and querying it than a php algorithm (this 2 loops force you to query all the lessons and it might be time consuming).
First of all you'd have to ask yourself : what is the ordering parameter ? (it is not obvious in the screenshot provided).
Let's say it's the order
(edited from comment) field, that is unique among all the lessons (not reset for a new section). In such a case, a new sql query would be :
SELECT * -- or just id
FROM lesson
WHERE `order` > ?
ORDER BY `order` ASC
LIMIT 1;
With the parameter (the ?
) equals to the current lesson order
field, generally passed with the method of the ORM used to query the Database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.