2

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. :-(

4

2 回答 2

3

无需指定[0]何时使用foreach循环。尝试

<?php if($course->subcategory_id == $subcategory->subcategory_id) { ?>
于 2013-06-14T08:54:42.173 回答
1

这条线

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

似乎您正在尝试访问数组中的第一个元素,但您已经在循环并且不需要它

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

另一方面,我认为您应该真正使用 php 长标签<?php .... ?>而不是短标签<?...?>

于 2013-06-14T08:55:12.067 回答