1

I have some images in the table images, the fields are id, name and photo. Where the image exists in photo.

At the minute, the code below not getting any images, although there should be about 4 images that match the query. The images that meet the query should go into the slideshowimages("") variable.

<?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($image) {

// if there are images for this page, run the javascript
?><script>


//configure the paths of the images, plus corresponding target links

        //NEED TO GET ALL RELEVANT IMAGE LOCATIONS INTO LINE BELOW
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> <?php
} else {
    // If there are not any images for this page, leave the space blank
    echo "";
    }

// Close the mysql connection
$conn->close();
?>      

The JavaScript that is in the head

<script language="JavaScript1.1">

var slideimages=new Array()
var slidelinks=new Array()
function slideshowimages(){
for (i=0;i<slideshowimages.arguments.length;i++){
slideimages[i]=new Image()
slideimages[i].src=slideshowimages.arguments[i]
}
}

</script>   

Screenshot of source code, when code is commented out

Liam
  • 102
  • 8

1 Answers1

1

The nice and simply way, is to use an AJAX call, to get your image urls in a JSON array, which you can parse to a javascript array, and iterate and so on. In that case, the added bonus is that you can separate your code to different files by language, and it makes a way nicer code.

But in your current code, you have to iterate your mysqli results with a simple loop. For example:

//...
// 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'] . "'");
//...

In this case, your $directory variable be like something like this:

'/images/image1.jpg','/images/image2.jpg','/images/image3.jpg'

And you hopefully can pass it to the javascript function as an argument list.

I hope I could help.

Derenir
  • 537
  • 1
  • 12
  • 29
  • First of all, thanks! - I have copied your edit into my code. However, it is not displaying any images. How do i pass the code to the as an argument list? – Liam Mar 09 '16 at 16:05
  • i have got the `slideshowimages("")` displaying the variable correctly! however this is only when the javascript is commented out. Why doesnt it work when i uncomment? - i have updated my code in the question. – Liam Mar 09 '16 at 16:24
  • Which part of the js code is commented out? When it's not, what is the js error or warning you're getting? – Derenir Mar 09 '16 at 20:00
  • im not getting any error or warning. Its just not showing any images. When i comment out anything after `if($image)` to just before `$conn->close();` i can see in the source code that the `slideshowimages` is filling the `()` with the file locations. When i uncomment it, as i say, no images appear on the screen – Liam Mar 10 '16 at 09:09
  • Source code is in the question. It looks like the php is going straight to the `else` and bypassing the javascript. Any ideas why? – Liam Mar 10 '16 at 09:54
  • That's because the `$image` variable does not exists any more in the scope of the `if($image)`. If a variable is defined in a scope (usually defined by `{}` brackets, which I left out in the case of the while loop, cause there's only one line in the scope), it's going to become a so called local variable, meaning it's only exists it the context of said scope. In your case it means, that you have to check `if($directory != '')` instead of `if($image)`, to check if you have any value for your js function. – Derenir Mar 10 '16 at 14:35
  • Thanks, that fixed it. Silly mistake not replacing the variable really - sorry. – Liam Mar 10 '16 at 14:47
  • I'm glad i could help. :) – Derenir Mar 10 '16 at 14:48