pgrevents Posted June 9, 2009 Share Posted June 9, 2009 I dont understand the problem as it is a loop. I have two sets of image information so thats two images. I have set up a for each loop so that I can display these images as a thumbnail here is the loop statement <?php foreach($gallery as $image) : ?> <li> <a class="thumb" href="<?=$imageloc?>" title="Title #0"> <img src="<?=$imagethumb?>" alt="Title #0" /></a> <div id="clear"></div> <div class="caption"> <div class="image-title"><?=$imagename?></div> <div class="image-desc">see larger</div> </div> </li> <?php endforeach ; ?> ?> now it does display but as I said I have only two images to show but it returns 8 loops of the same first image. Here is the code php mysql code that i include <?php $dbhost = 'localhost'; $dbuser = 'dbuser'; $dbpass = 'dbpass'; $dbname = 'dbname'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); $query = mysql_query("SELECT * FROM gallery"); $gallery = mysql_fetch_array($query); $imageloc = $gallery['image_loc']; $imagename = $gallery['image_name']; $imagethumb = $gallery['thumnail_loc']; ?> There is no errors outputting so I am at a loss can any of you help? Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/ Share on other sites More sharing options...
Julian Posted June 9, 2009 Share Posted June 9, 2009 Try this: <?php foreach ($gallery as $image) { ?> <li> <a class="thumb" href="<?php echo $imageloc; ?>" title="Title #0"> <img src="<?php echo $imagethumb; ?>" alt="Title #0" /></a> <div id="clear"></div> <div class="caption"> <div class="image-title"><?php echo $imagename; ?></div> <div class="image-desc">see larger</div> </div> </li> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-852674 Share on other sites More sharing options...
pgrevents Posted June 9, 2009 Author Share Posted June 9, 2009 tried it it didnt work same result as before I am seeing 8 copies of 1 image when I am supposed to be seeing 2 different images thanks Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-852682 Share on other sites More sharing options...
PFMaBiSmAd Posted June 9, 2009 Share Posted June 9, 2009 The following line of code - $gallery = mysql_fetch_array($query); Fetches ONE row from the result set. From the php manual - Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows Looping over $gallery is just looping over all the columns in that row. You must use the mysql_fetch_array in a while() loop to fetch all the rows. Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-852689 Share on other sites More sharing options...
Julian Posted June 9, 2009 Share Posted June 9, 2009 What about using while <?php while ($gallery = mysql_fetch_assoc($query)) { ?> <li> <a class="thumb" href="<?php echo $imageloc; ?>" title="Title #0"> <img src="<?php echo $imagethumb; ?>" alt="Title #0" /></a> <div id="clear"></div> <div class="caption"> <div class="image-title"><?php echo $imagename; ?></div> <div class="image-desc">see larger</div> </div> </li> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-852691 Share on other sites More sharing options...
pgrevents Posted June 9, 2009 Author Share Posted June 9, 2009 a big DOOH to me I used while in something yesterday but now its only showing one result i will keep working on it thanks peeps Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-852696 Share on other sites More sharing options...
ted_chou12 Posted June 9, 2009 Share Posted June 9, 2009 $dbhost = 'localhost'; $dbuser = 'dbuser'; $dbpass = 'dbpass'; $dbname = 'dbname'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); $query = mysql_query("SELECT * FROM gallery"); while ($gallery = mysql_fetch_array($query)) { $imageloc = $gallery['image_loc']; $imagename = $gallery['image_name']; $imagethumb = $gallery['thumnail_loc'];?> <li> <a class="thumb" href="<?=$imageloc?>" title="Title #0"> <img src="<?=$imagethumb?>" alt="Title #0" /></a> <div id="clear"></div> <div class="caption"> <div class="image-title"><?=$imagename?></div> <div class="image-desc">see larger</div> </div> </li> <?php } ?> Ted Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-852730 Share on other sites More sharing options...
pgrevents Posted June 9, 2009 Author Share Posted June 9, 2009 hi thanks, there wasnt a problem with the vars it does display correctly but instead of the 2 test images there is only one image being shown in the look. Saying that I did try your code but unfortunately it has the same result as before only showing one image thanks Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-852737 Share on other sites More sharing options...
ted_chou12 Posted June 9, 2009 Share Posted June 9, 2009 Hi, sorry didn't solve ur problem, but remind u to check ur mysql db and make sure it does have two images. just to make sure Ted Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-852739 Share on other sites More sharing options...
pgrevents Posted June 9, 2009 Author Share Posted June 9, 2009 yeah i thought it the second might not have gone through but it is set in stone there thes is confudling me ??? Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-852742 Share on other sites More sharing options...
pgrevents Posted June 10, 2009 Author Share Posted June 10, 2009 This works it displays the content BUT it will not show the last entry here is the DB code <?php $internal = $_GET['more']; $dbhost = 'localhost'; $dbuser = 'bates'; $dbpass = 'pass'; $dbname = 'bates'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); $query = mysql_query("SELECT * FROM contact WHERE internal= '$internal'"); $more = mysql_fetch_array($query); ?> here is my page display code <?php while($row=mysql_fetch_array($query, MYSQL_ASSOC)){ ; foreach ($row as $onbox)?> <div class="id_box"><?=$row['internal']?></div> <div class="from_box"><?=$row['name']?></div> <div class="from_email_box"><?=$row['email']?></div> <div class="msg_status_box"><?=$row['status']?></div> <div class="reply_status_box"> <a href="?more=<?=$row['internal']?>">more</a> </div> <div class="updated_box"> <? if(empty($row['reply_time'])){ print "".$row['time']." on ".$row['date'].""; } else{ print ""; } ?> what could be the reason that it wont show the latest entry? Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-853176 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2009 Share Posted June 10, 2009 Because you are fetching a row into $more before your while loop and then not using it - $more = mysql_fetch_array($query); while($row=mysql_fetch_array($query, MYSQL_ASSOC)){ ; Why do you have this - $more = mysql_fetch_array($query); in your code at all? Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-853204 Share on other sites More sharing options...
pgrevents Posted June 10, 2009 Author Share Posted June 10, 2009 just remnants of something else i was doing i removed it since you saying that and still same outcome Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-853288 Share on other sites More sharing options...
pgrevents Posted June 11, 2009 Author Share Posted June 11, 2009 Does any one have any ideas? The latest entry is not showing ?????????? I cant find an answer an since its a messaging system it seems a little silly not showing the latest mail Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-853417 Share on other sites More sharing options...
pgrevents Posted June 11, 2009 Author Share Posted June 11, 2009 http://www.barrybatemanphotography.co.uk/?id=155 now Im not the php programmer for the group im just a designer lol he is on holiday and this project has to be handed in like tommorow. as you will see from the above it displays 2 images but they are the same there are two images in the database and they are different but its looping with the while statement at the top of the page Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-853418 Share on other sites More sharing options...
haku Posted June 11, 2009 Share Posted June 11, 2009 $images = mysql_query("SELECT stuff FROM gallery"); while ($image = mysql_fetch_array($images)) { echo $image['column1']; // column 1 in the database echo $image['column2']; // column 2 in the database //... and so on } Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-853420 Share on other sites More sharing options...
pgrevents Posted June 11, 2009 Author Share Posted June 11, 2009 so the while statement that i have doesnt pull everything from the database? Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-853529 Share on other sites More sharing options...
Ken2k7 Posted June 11, 2009 Share Posted June 11, 2009 while($row=mysql_fetch_array($query, MYSQL_ASSOC)){ ; Take out the semi-colon at the end. haku's example is a good one. The SQL selects one column, but the while loop assumes more than one is returned. Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-853541 Share on other sites More sharing options...
Julian Posted June 11, 2009 Share Posted June 11, 2009 I don't see any image, only the background image... Please post the page code completely. Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-853755 Share on other sites More sharing options...
J.Daniels Posted June 11, 2009 Share Posted June 11, 2009 Working from your display code, you have a semi-colon after the while and you do not need the foreach: <?php while($row=mysql_fetch_array($query, MYSQL_ASSOC)) { ?> <div class="id_box"><?=$row['internal']?></div> <div class="from_box"><?=$row['name']?></div> <div class="from_email_box"><?=$row['email']?></div> <div class="msg_status_box"><?=$row['status']?></div> <div class="reply_status_box"> <a href="?more=<?=$row['internal']?>">more</a> </div> <div class="updated_box"> <? if(empty($row['reply_time'])){ print "".$row['time']." on ".$row['date'].""; } else{ print ""; } } ?> Also, before the while loop, you can echo out the number of rows to make sure there are two rows returned: echo mysql_num_rows($query); Link to comment https://forums.phpfreaks.com/topic/161587-for-each-loop-from-mysql/#findComment-853782 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.