Questions › 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 http://i.stack.imgur.com/SJtOM.jpg

so, whats wrong with my script?


2 Answers :
Alex Andrei answered

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.

3 Rows imgur.com/pf9EX7Z i tried ur script but still... cant solve the problem;
Alex Andrei replied
can you provide the value of $output? feel free to edit my answer;
Alex Andrei replied
ok, I can see one issue would be from having the same id for all the div tags. I will edit my answer to show this.;
i updated my script, i forgot to tell u that <li id="project_<?php echo $data['id_album'];?>"> "id" must be the same with <div id='divnavproject_$data2[id_album]' class='project_content'> and <div id='slider$data2[id_album]' class='nivoSlider'>;
Alex Andrei replied
ids must be unique across all html elements in a document. You cannot enforce this rule when iterating over multiple images. Please check again the documentation for the slider you are using and adjust your markup.;

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!

shrmn replied
Please mark his answer.;