Jump to content

Sorting Pictures By Category.


n00bert

Recommended Posts

Well what I Have right now just displays all my thumbnails. I want it to group the thumbnails By their category and display the category name above that group. Any help is appreciated.

 

My Database looks like so.

 

+-----------------+-----------------+------------------+---------------+------------------+

|Table Name         | Fields-------------------------------------------------------------------

+-----------------+-----------------+------------------+---------------+------------------+

|gallery_category  | category_id        | category_name    |

+-----------------+-----------------+------------------+---------------+----------------- +

|gallery_photos     | photo_id            | photo_filename     | photo_caption | photo_category   |

+-----------------+-----------------+------------------+---------------+------------------+

 

Here is my code.

<?php

include("includes/gallerysettings.php");

 

// initialization

$result_array = array();

$counter = 0;

 

 

// Thumbnail Listing

 

 

$result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos" );

$nr = mysql_num_rows( $result );

 

if( empty( $nr ) )

{

$result_final = "\t<tr><td>No Category found</td></tr>\n";

}

else

{

while( $row = mysql_fetch_array( $result ) )

{

$result_array[] = "<a href='".$images_dir."/".$row[2]."' rel='lightbox[gallery]' title='".$row[1]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>";

}

mysql_free_result( $result );

 

$result_final = "<tr>\n";

 

foreach($result_array as $thumbnail_link)

{

if($counter == $number_of_thumbs_in_row)

{

$counter = 1;

$result_final .= "\n</tr>\n<tr>\n";

}

else

$counter++;

 

$result_final .= "\t<td>".$thumbnail_link."</td>\n";

}

 

if($counter)

{

if($number_of_photos_in_row-$counter)

$result_final .= "\t<td colspan='".($number_of_photos_in_row-$counter)."'> </td>\n";

 

$result_final .= "</tr>";

}

}

 

// Final Output

echo <<<__HTML_END

 

<table width='100%' border='0' align='center' style='width: 100%;'>

$result_final

</table>

 

__HTML_END;

?>

 

 

and keep in mind...i'm still not too hot with this stuff. Just about everything in this i've learned from a tutorial.

Link to comment
Share on other sites

well you need to JOIN in categories you query looks like

SELECT
gallery_photos.photo_id as photo_id,
gallery_photos.photo_filename as filename,
gallery_photos.photo_caption as caption,
gallery_photos.photo_cateogry as photo_cat,
gallery_category.category_id,
gallery_category.category_name as category

FROM
`gallery_photos`

LEFT JOIN `gallery_category` ON(gallery_category.category_id = gallery_photos.photo_category)

GROUP BY gallery_photos.photo_id

ORDER BY gallery_category.category_name

 

Link to comment
Share on other sites

error check your queries

<?php
$q = "
SELECT
gallery_photos.photo_id as photo_id,
gallery_photos.photo_filename as filename,
gallery_photos.photo_caption as caption,
gallery_photos.photo_cateogry as photo_cat,
gallery_category.category_id,
gallery_category.category_name as category

FROM
`gallery_photos`

LEFT JOIN `gallery_category` ON(gallery_category.category_id = gallery_photos.photo_category)

GROUP BY gallery_photos.photo_id

ORDER BY gallery_category.category_name

";
$r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);

Link to comment
Share on other sites

ok now that just outputs the $q variable.

 

SELECT gallery_photos.photo_id as photo_id, gallery_photos.photo_filename as filename, gallery_photos.photo_caption as caption, gallery_photos.photo_cateogry as photo_cat, gallery_category.category_id, gallery_category.category_name as category FROM `gallery_photos` LEFT JOIN `gallery_category` ON(gallery_category.category_id = gallery_photos.photo_category) GROUP BY gallery_photos.photo_id ORDER BY gallery_category.category_name

 

I think i'm doing something wrong here.

 

I'm still kinda dumb to this stuff.

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.