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

4
  • RTFM: php.net/manual/en/mysqli-result.fetch-assoc.php Fetch a result row as an associative array. One SINGLE row... Commented Mar 9, 2016 at 15:03
  • how would i write that into the code? Commented Mar 9, 2016 at 15:06
  • You need to loop through the results: stackoverflow.com/questions/15511367/… Commented Mar 9, 2016 at 15:34
  • please could you provide some code? im really struggling with this Commented Mar 9, 2016 at 15:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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?
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.
Which part of the js code is commented out? When it's not, what is the js error or warning you're getting?
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
Source code is in the question. It looks like the php is going straight to the else and bypassing the javascript. Any ideas why?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.