Warning: Invalid argument supplied for foreach()

5 posts by 2 authors in: Forums > CMS Builder
Last Post: July 9, 2013   (RSS)

By Toledoh - July 9, 2013

Hi Guys,

I'm using the following to show suppliers grouped by category;

            <?php $suppliersByCategory = array_groupBy($suppliersRecords, 'category', true); ?>
            <?php foreach (getListOptions('suppliers', 'category') as $value => $label): ?>
            <?php $suppliers = @$suppliersByCategory[$value]; ?>
            
            <h4 style="color:<?php echo htmlencode($detailRecord['colour']) ?>;"><?php echo $label;?></h4>
            
            <?php foreach ($suppliers as $sup): ?>
            
                        <div class="row">
                            <div class="threefifth">
                                <h5><?php echo htmlencode($sup['title']) ?></h5>
                                <?php echo $sup['content']; ?>
                                <?php if ($sup['video']): ?>
                                    <p><a href="<?php echo htmlencode($sup['video']) ?>">> view video</a></p>
                                <?php endif ?>
                                <?php if ($sup['website']): ?>
                                    <p><a href="<?php echo htmlencode($sup['website']) ?>">> view website</a></p>
                                <?php endif ?>
                            </div>
                            <div class="twofifth">
                                <?php foreach ($sup['logo'] as $index => $upload): ?>
                                <img src="<?php echo $upload['urlPath'] ?>" alt="<?php echo htmlencode($record['title']) ?>" class="fullWidth">
                                <?php endforeach ?>
                            </div>
                        </div>
            <?php endforeach ?>
            
            <?php endforeach ?>

If a category doesn't have any suppliers associated with it, I get an error... Warning: Invalid argument supplied for foreach()... how do I not show the category if it's empty?

Cheers,

Tim (toledoh.com.au)

By jenolan - July 9, 2013

Add like this not real code only the 'logic'

if( is_array( $theWidget ) )
    foreach( $theWidget AS ... )
else
    cat is empty

---
<?= "Jenolan(Larry) :: Coding Since 1973" ?>
Peace and Long Life

By Toledoh - July 9, 2013

Thanks Larry, but I didn't explain well enough.

The aim is to produce;

Heading 1
- item
- item

Heading 2
-item

Heading 3
-item
-item

However, if there is no item under Heading 2... not show heading 2.

Cheers,

Tim (toledoh.com.au)

By Toledoh - July 9, 2013

Thanks Larry...  you were right the first time, I was just putting the code in the wrong place.  Much appreciated!

Cheers,

Tim (toledoh.com.au)