[英]Get images in mysql table to appear in javascript slideshow
I have some images in the table images
, the fields are id
, name
and photo
. 我在表格
images
有一些图像,字段是id
, name
和photo
。 Where the image exists in photo
. photo
中photo
。
At the minute, the code below not getting any images, although there should be about 4 images that match the query. 此刻,下面的代码没有得到任何图像,尽管应该有大约4个与查询匹配的图像。 The images that meet the query should go into the
slideshowimages("")
variable. 符合查询条件的图像应放入
slideshowimages("")
变量中。
<?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
JavaScript的是在
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>
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. 简便的好方法是使用AJAX调用,以JSON数组形式获取图像URL,您可以将其解析为javascript数组,然后进行迭代等等。 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. 但是在当前代码中,您必须通过一个简单的循环来迭代mysqli结果。 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: 在这种情况下,您的
$directory
变量如下所示:
'/images/image1.jpg','/images/image2.jpg','/images/image3.jpg'
And you hopefully can pass it to the javascript function as an argument list. 希望您可以将其作为参数列表传递给javascript函数。
I hope I could help. 希望我能帮上忙。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.