PHP - Why my slideshows only read 1 image?

For some reason my slideshow is only read 1 image from database, fyi I have 3 images in database.

here's my full script:

<div class="projects">
<ul class="tj_gallery">
<?php
$sql="select * from album where kategori_album='architecture'";
$aksi = mysql_query($sql);
$num_rows = mysql_num_rows($aksi);
while($data=mysql_fetch_array($aksi)){ ?>
<li id="project_<?php echo $data['id_album'];?>"><a href="javascript:;"><img src="<?php echo $SERVER; ?>/img_album/<?php echo $data['gbr_album']; ?>" width="175" height="167" alt=""><span class="descript">Architecture</span></a></li>
<?php } ?>
</ul> </div>

<?php
$sql2="SELECT
        album.kategori_album,
        album.id_album,
        gallery.jdl_gallery,
        gallery.gbr_gallery
        FROM
        gallery
        INNER JOIN album ON gallery.id_album = album.id_album
        WHERE
        album.kategori_album = 'architecture'";
$aksi2 = mysql_query($sql2);
while($data2=mysql_fetch_array($aksi2)){ 
    echo "<div class='openproject'>
    <div id='divnavproject_$data2[id_album]' class='project_content'>
    <div class='slideprojects theme-default'>
    <div id='slider$data2[id_album]' class='nivoSlider'>    
    <img src='$SERVER/img_galeri/$data2[gbr_gallery]' alt='' align='center' height='350'>
    </div>
    </div>
    </div>
    </div>";
}
?> 

here is my database data https://i.sstatic.net/SJtOM.jpg

so, whats wrong with my script?

Upvotes: 2

Views: 76

Answers (2)

Problem Solved.

<?php
$sql3="SELECT
        album.kategori_album,
        album.id_album,
        gallery.jdl_gallery,
        gallery.gbr_gallery
        FROM
        gallery
        INNER JOIN album ON gallery.id_album = album.id_album
        WHERE
        album.kategori_album = 'architecture'";
$aksi3 = mysql_query($sql3);
$num_rows3 = mysql_num_rows($aksi3);
$output2 = '';
while($data3=mysql_fetch_assoc($aksi3)){

 $output2 .= '<img src="' . $SERVER . '/img_galeri/' . $data3['gbr_gallery'] . '" alt="" align="center" height="350">';

}
?>

<?php
$sql2="SELECT
        album.kategori_album,
        album.id_album,
        gallery.jdl_gallery,
        gallery.gbr_gallery
        FROM
        gallery
        INNER JOIN album ON gallery.id_album = album.id_album
        WHERE
        album.kategori_album = 'architecture'
        GROUP BY 
        album.id_album";
$aksi2 = mysql_query($sql2);
while($data2=mysql_fetch_array($aksi2)){ 
    echo "<div class='openproject'>
    <div id='divnavproject_$data2[id_album]' class='project_content'>
    <div class='slideprojects theme-default'>
    <div id='slider$data2[id_album]' class='nivoSlider'>    
    ".$output2."
    </div>
    </div>
    </div>
    </div>";
}
?> 

thanks to Alex Andrei!

Upvotes: 0

Alex Andrei
Alex Andrei

Reputation: 7283

Please note that the mysql_* api is deprecated and you should start using either mysqli_ or pdo.

First, verify how many rows your query returns by using this:

$sql2 = 'select ....';
$aksi2 = mysql_query($sql2);
$num_rows = mysql_num_rows($aksi2);
echo "$num_rows Rows\n";

This way you will know if the problem is with the query or the actual printing of the results.

If you do get more than one result build the full output and then print it out, also we can use the _fetch_assoc function since you are using string keys.

$output = '';
$iterator = 0; // inserted iterator
while($data2=mysql_fetch_assoc($aksi2)){ 

    $output .= '<div class="openproject">
    <div id="divnavproject_' . $data2['id_album'] . '_' . $iterator . '" class="project_content">
        <div class="slideprojects theme-default">
            <div id="slider"' . $data2['id_album'] . '_' .$iterator . '" class="nivoSlider">
                <img src="' . $SERVER . '/img_galeri/' . $data2['gbr_gallery'] . '" alt="" align="center" height="350">
            </div>
        </div>
    </div>
</div>';
$iterator++;
}

print $output;

Update:

This will give you the same id for the following elements div id="navproject and div id="slider" because you have the same value for id_album which is 33.
Which will cause the slider not to work.

To overcome this you need to add an extra bit of value to make your ids unique. See inserts in the code where I add a counter as a suffix to the id.

Upvotes: 1

Related Questions