Jump to content

[SOLVED] Select Distinct??


dropfaith

Recommended Posts

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

Link to comment
Share on other sites

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>
   <?
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.