StuBlackett
StuBlackett

Reputation: 3857

Undefined offset in an foreach loop

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; ?> - &pound; <?=$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. :-(

Upvotes: 2

Views: 1102

Answers (2)

Fabio
Fabio

Reputation: 23500

This line

if($course[0]->subcategory_id == $subcategory->subcategory_id) { ?>

It seems like you are trying to access to first element in array but you are already looping and it's not needed

if($course->subcategory_id == $subcategory->subcategory_id) { ?>

As side not I think you should really use php long tags <?php .... ?> instead of short ones <?...?>

Upvotes: 1

curious_coder
curious_coder

Reputation: 2468

There is no need to specify [0] when you are using foreach loop. try

<?php if($course->subcategory_id == $subcategory->subcategory_id) { ?>

Upvotes: 3

Related Questions