I’ve stored image information in my mysql database like below…
id, int(10), PRIMARY KEY
title, varchar(255), utf8_unicode_ci
url, varchar(255), utf8_unicode_ci example: http://www.blahblah.com/blahblah.png
caption, varchar(255), utf8_unicode_ci
width, int(10)
height, int(10)
entry_id, int(10), FOREIGN KEY
I’m trying to grab the urls from all the images by doing the following…
<?php
include('connectdb.php');
include_once('functions.php');
include_once('class/class.Image.php');
// get all images for Entry
$imgArray = array();
$imgArray = getEntryImages( 3 );
$imgArray = getImageUrls ( $imgArray );
foreach( $imgArray as $img )
{
echo $img . ', ';
}
?>
Here’s the output I get, and the code for getEntryImages() & getImageUrls() below…
Output: 5, 5, M, M, h, h, T, T, 4, 4, 3, 3, 3, 3,
function getEntryImages ( $entryID )
{
$imageArray = array();
$result = mysql_query( "SELECT * FROM images WHERE entry_id = ".$entryID."" ) or die();
$row = mysql_fetch_array( $result );
foreach ( $row as $image )
{
$temp_image = new Image ( $image['id'], $image['title'], $image['url'],
$image['caption'], $image['width'], $image['height'],
$image['entry_id'] );
array_push( $imageArray, $temp_image );
}
return $imageArray;
}
function getImageUrls( $imgArray )
{
$imageUrls = array();
foreach ( $imgArray as $image )
{
array_push( $imageUrls, $image->getUrl() );
}
return $imageUrls;
}
If anyone is able to point me in any direction on a possible way to fix this problem I’d greatly appreciate it thanks!
Your problem is that you’re only fetching the array only once:
So it’s running a
foreachon the fields of the table and their values, not the entire result set!You need to fetch the array until
$rowisNULL, like this: