简体   繁体   中英

How to display a slideshow of images from a mysql database

I have a table named images , the fields are id, name, photo , the photo field is varchar as the pictures are stored onto the server using a php form. At the minute my code displays only 1 image from the table, please could someone help me so that it does a slideshow of all the images with the query:

SELECT * FROM images WHERE name='$pagetitle'

$pagetitle is defined elsewhere in the script.

The problem is within the index.php file

index.php

 <!-- Image Slide Show Start --> <div style="display: flex; justify-content: center;"> <img align="middle" src="" name="slide" border=0 width=300 height=375> <?php require('mysqli.php'); $data = $conn->query("SELECT * FROM images WHERE name= '$pagetitle'"); $image = mysqli_fetch_array( $data ); $directory = ("/images/".$image['photo'] . ""); $conn->close(); ?> <script> //configure the paths of the images, plus corresponding target links slideshowimages("<?php echo $directory ?>") //configure the speed of the slideshow, in miliseconds var slideshowspeed=2000 var whichlink=0 var whichimage=0 function slideit(){ if (!document.images) return document.images.slide.src=slideimages[whichimage].src whichlink=whichimage if (whichimage<slideimages.length-1) whichimage++ else whichimage=0 setTimeout("slideit()",slideshowspeed) } slideit() </script> </div><br><br> <!-- Image Slide Show End -->

The solved code is below:

 <?php // Connect to the database require('mysqli.php'); // Query for a list of all existing files $sql = "SELECT * FROM images WHERE name= '$pagetitle'"; $result = $conn->query($sql); $directory = ''; while( $image = $result->fetch_assoc() ) $directory .= ($directory != '' ? "," : '') . ('"/images/'.$image["photo"] . '"'); // Check if it was successfull if($directory != '') { // if there are images for this page, run the javascript ?><script> //configure the paths of the images, plus corresponding target links slideshowimages(<?php print $directory ?>) //configure the speed of the slideshow, in miliseconds var slideshowspeed=2000 var whichlink=0 var whichimage=0 function slideit(){ if (!document.images) return document.images.slide.src=slideimages[whichimage].src whichlink=whichimage if (whichimage<slideimages.length-1) whichimage++ else whichimage=0 setTimeout("slideit()",slideshowspeed) } slideit() </script> <? } else { // If there are not any images for this page, leave the space blank echo ""; } // Close the mysql connection $conn->close(); ?>

<?php 
$servername = "";
$username = "";
$password = "";
$dbname = "";
$conn = new mysqli($servername, $username, $password, $dbname);

//Make connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//SQL query
$sql = "SELECT * FROM TABLE_NAME";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        $dir= "" . $row["field"]. "";
       echo" <div class='content section' style='max-width:500px'>
             <img class='slides' src='$dir' style=width:100%></div>";
    }
} else {
    echo "Empty Gallery";
}
?>

<html>
<body>
<script>
var index = 0;
slideshow();
function slideshow() {
  var i;
  var x = document.getElementsByClassName("slides");
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";  
  }
  index++;
  if (index > x.length) {index = 1}    
  x[index-1].style.display = "block";  
  setTimeout(slideshow, 3000); // Change slides every 3 seconds
}
</script>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM