Show Random image - Invalid argument error
7 posts by 2 authors in: Forums > CMS Builder
Last Post: January 7, 2010 (RSS)
By theclicklab - January 2, 2010 - edited: January 2, 2010
I'm trying to show a random image from a separate table in a page but am getting the following error:
"Invalid argument supplied for foreach()
The images are in a table called "header_images_small" and I'm trying to select a subset of images based on a field "category" in "header_images_small"
Here's the code:
<?php else: ?>
<?php // Display random image from selected category in table "header_images_small"
list($header_images_smallRecords, $header_images_smallMetaData) = getRecords(array(
'tableName' => 'header_images_small',
'where' => "category ='{$blogRecord['static_image_random']}'", //Gets the category id chosen in "blog"
'allowSearch' => '0',
));
?>
<div class="feature">
<?php foreach ($header_images_smallRecord['image'] as $upload): //Throwing an error here "Invalid argument supplied for foreach()" ?>
<?php shuffle($record['image']) ?>
<?php if ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="<?php echo $header_images_smallRecord['title'] ?>" />
<?php endif ?>
<?php break ?>
<?php endforeach ?>
</div>
Re: [aquaman] Show Random image - Invalid argument error
By Chris - January 5, 2010
I think the error you were getting was because some of your variable names didn't match up.
Does this work for you?
<?php // Display random image from selected category in table "header_images_small"
list($header_images_smallRecords, $header_images_smallMetaData) = getRecords(array(
'tableName' => 'header_images_small',
'where' => "category ='{$blogRecord['static_image_random']}'", //Gets the category id chosen in "blog"
'orderBy' => 'RAND()',
'limit' => 1,
'allowSearch' => '0',
));
$header_images_smallRecord = $header_images_smallRecords[0]; // get first record
?>
<div class="feature">
<?php shuffle($header_images_smallRecord['image']) ?>
<?php foreach ($header_images_smallRecord['image'] as $upload): ?>
<?php if ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="<?php echo $header_images_smallRecord['title'] ?>" />
<?php endif ?>
<?php break ?>
<?php endforeach ?>
</div>
I hope this helps. Please let me know if you have any questions.
Chris
Re: [chris] Show Random image - Invalid argument error
Notice: Undefined offset: 0 in /home/naia/www/staging.naia.com.fj/post.php on line 81
Warning: shuffle() expects parameter 1 to be array, null given in /home/naia/www/staging.naia.com.fj/post.php on line 85
Warning: Invalid argument supplied for foreach() in /home/naia/www/staging.naia.com.fj/post.php on line 86
Have attached the full page below. Look for the section:
<?php elseif ($blogRecord['header_type'] == 'random'): ?>
A few notes:
The random images are being pulled from a table called:
header_images_small which has the following fields: title, image, category. The field "category" is setup to pull the values from a different table - see attached image of setup.
Many thanks for your help!!
Jan
Re: [aquaman] Show Random image - Invalid argument error
By Chris - January 5, 2010
Please change this:
$header_images_smallRecord = $header_images_smallRecords[0]; // get first record
to this:
$header_images_smallRecord = @$header_images_smallRecords[0]; // get first record
if (!$header_images_smallRecord) { print "Could not find a header_images_small record for category = '{$blogRecord['static_image_random']}'"; exit; }
Hopefully that will give us more information about the problem. Are you sure there are records for that category?
Please let me know what you discover.
Chris
Re: [chris] Show Random image - Invalid argument error
I have added that code and it spits out this:
Could not find a header_images_small record for category = '1'
I checked in phpmyadmin to be sure there are a bunch of images with category set to 1 (see attached)
Re: [aquaman] Show Random image - Invalid argument error
By Chris - January 7, 2010
Ahh, I can see from your screenshot that your category field is a "multi-value" list field. To check if a value is in a multi-value list field, you'll need to use a LIKE test:
'where' => "category LIKE '%\t{$blogRecord['static_image_random']}\t%'", //Gets the category id chosen in "blog"
I hope this helps. Please let me know if you have any questions.
Chris
Re: [chris] Show Random image - Invalid argument error
Thanks so much for your outstanding help & support.
cheers
Jan