I am having a nightmare with a Foreach loop on my site.
The error I am getting is :
Message: Undefined offset: 0
What I would like to do is ideally check that their is a match of course & category, If their isnt not show the accordion.
An output of my Data is as follows....
Array ( [0] => stdClass Object ( [course_id] => 2 [course_title] => Make Or Break 1 Starting Your Business [course_description] => Make or Break, Starting your business. [course_cost] => 35.99 [delivery_method_id] => 1 [subcategory_id] => 1 [course_duration] => 2 Hours [course_level] => Intermediate [date_added] => 2013-06-14 09:57:49 [date_edited] => [status] => active ) )
However I am also getting an empty array :
Array ( )
My Code is as follows :
<div class="row">
<div class="span12">
<h1><span class="green"><?=$category_details[0]->category_title; ?></span></h1>
<p><?=$category_details[0]->category_description; ?></p>
<? if(empty($courses[0][0])) { ?>
<p>Their are currently no courses in this category.</p>
<? } else { ?>
<p>Courses in <?=$category_details[0]->category_title; ?></p>
<div class="accordion-content">
<? foreach($subcategories as $subcategory) : ?>
<h3><a href="#"><?=$subcategory->subcategory_title; ?></a></h3>
<div>
<? foreach($courses as $course): ?>
<? if($course[0]->subcategory_id == $subcategory->subcategory_id) { ?>
<ul class="accordion-course">
<? foreach($course as $course): ?>
<li>
<a href="<?=base_url(); ?>view-course/<?=$course->course_id; ?>" title="View Course - <?=$course->course_title; ?>">
<?=$course->course_title; ?> - £ <?=$course->course_cost; ?>
</a>
<a class="course-btn pull-right" href="<?=base_url(); ?>view-course/<?=$course->course_id; ?>" title="View Course - <?=$course->course_title; ?>">
View Course
</a>
</li>
<? endforeach; ?>
</ul>
<? } ?>
<? endforeach; ?>
</div>
<? endforeach; ?>
</div>
<? } ?>
</div>
<?= $template['_partials']['user_navigation']; ?>
The error line is question is :
<? if($course[0]->subcategory_id == $subcategory->subcategory_id) { ?>
Hope someone can help me out on this offset nightmare I find myself in. :-(