dropfaith Posted September 1, 2009 Share Posted September 1, 2009 Okay ive avoided coming here for help for awhile on my own but this i cant grasp maybe its been too long a night. Im trying to pull all records from a database on a page and loop thru them but i only want the gallery name to Display once <?php //Connection details up here mysql_connect($mysql['hostname'], $mysql['username'], $mysql['password']) or die("Unable to Connect to mysql database"); mysql_select_db($mysql['database_name']) or die("Unable to Select mysql database"); // generate and execute query $query = "SELECT distinct (gallery) * FROM images "; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // if records present if (mysql_num_rows($result) > 0) { // iterate through resultset // print article titles while($row = mysql_fetch_object($result)) { ?> I want the H3 to be displayed once no matter how many images there are with the same gallery name and all the images from that gallery to still display.. Its all in on db table for now any ideas on the best way to do this? <h3><?php echo $row->gallery; ?></h3> <a href="galleryimage.php?Id=<?php echo $row->Id; ?>" onClick="return popup(this, 'Events')" title="<?php echo $row->newname; ?>"><img src="<?php echo $row->thumbnail; ?>"/></a> [code] then below it just closes the db and crap Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted September 1, 2009 Share Posted September 1, 2009 Don't use SELECT DISTINCT for this, that's not the purpose of DISTINCT // generate and execute query $query = "SELECT * FROM images ORDER BY gallery, id"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // if records present if (mysql_num_rows($result) > 0) { // iterate through resultset // print article titles $previousGallery = ''; while($row = mysql_fetch_object($result)) { if ($row->gallery != $previousGallery) { ?><h3><?php echo $row->gallery; ?></h3><? $previousGallery = $row->gallery; } ?> <a href="galleryimage.php?Id=<?php echo $row->Id; ?>" onClick="return popup(this, 'Events')" title="<?php echo $row->newname; ?>"><img src="<?php echo $row->thumbnail; ?>"/></a> <? } Quote Link to comment Share on other sites More sharing options...
kickstart Posted September 1, 2009 Share Posted September 1, 2009 Hi There doesn't appear to be anything to list the images in a gallery. I would just pull back the data you want, ordered by gallery. On each record compare the gallery with the gallery of the previous record and if different put out the heading line. No need to use DISTINCT doing this. Also I would suggest that the gallery names are pulled off into a seperate table rather than just being stored with the images (eg, what would happen if your system had a gallery that had zero images but you still wanted to have the gallery with its events). All the best Keith All the best Keith Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.