Page 1 of 1

Pulling images by group from the database

Posted: Wed Mar 05, 2014 12:48 pm
by PhpExile
Some how I need to get 1 group (Weddings) then cycle through the image names in that group then once that is done i need to go to the next group (5points) and display the 2 images in that div and so on. Weddings -> 2 images, 5points -> 2 images and Misc -> 2 images. It is putting each image in a separate div. The Wedding, 5points and Misc has 2 images each I am getting 6 divs with 1 image in each I should have 3 divs (wedding, 5points and misc) with 2 images displayed in each of the 3 divs. Hopefully I have explained a little better. This is driving me up the wall XD. Thanks all.

Code: Select all

$pgroup = $Sys->db->query("SELECT dj_photo_group.photo_group, dj_photo.name, dj_photo.img_group FROM dj_photo_group, dj_photo WHERE dj_photo_group.photo_group = dj_photo.img_group ORDER BY dj_photo.img_group DESC");

while($photo = $pgroup->fetch_object()) {

    echo '<div class="photo_head">' . ucfirst($photo->photo_group) . '</div>';
    echo '<div class="photo_sub">';

    while($photo->name) {
        echo '<img src="photogallery/' . $photo->photo_group . '/' . $photo->name . '" />';
    }

    echo '</div>'; 
}
I have been working on this for days. I can't seem to get it exactly how I want it to display. Can anyone help on this matter? Thanks.

Re: Pulling images by group from the database

Posted: Wed Mar 05, 2014 9:30 pm
by Christopher
Usually to add category headings you do something like this:

Code: Select all

$pgroup = $Sys->db->query("SELECT dj_photo_group.photo_group, dj_photo.name, dj_photo.img_group FROM dj_photo_group, dj_photo WHERE dj_photo_group.photo_group = dj_photo.img_group ORDER BY dj_photo.img_group DESC");

$photo_group = '';     // initialize to no group
while($photo = $pgroup->fetch_object()) {
    if ($photo->photo_group != $photo_group) {     // the group has changed
        if ($photo_group != '') {     // not 1st time through loop
               echo '</div>'; 
        }
        echo '<div class="photo_head">' . ucfirst($photo->photo_group) . '</div>';
        echo '<div class="photo_sub">';
        $photo_group = $photo->photo_group;     // set to current group
    }
    echo '<img src="photogallery/' . $photo->photo_group . '/' . $photo->name . '" />';
}
echo '</div>'; 

Re: Pulling images by group from the database

Posted: Thu Mar 06, 2014 12:27 am
by PhpExile
Awesome man. Thanks. Works perfect. I didn't think about setting a variable then check it again at the top to see if it changed. Thanks again.