[英]How do you use a php variable for directory path?
我正在從URL獲取用戶ID。 這就是我目前所擁有的。 我想用$ userid替換一個,但是我不知道如何。 它不起作用,我似乎找不到正確的語法,請有人幫忙嗎?
function returnimages($dirname = "Photos/1")
基本上,我正在嘗試使用html,php和javascript創建照片幻燈片。 在開始將php添加到我的代碼之前,我有一些工作。 我有html和外部javascript,它們可以更改照片,並且它們在循環中淡入淡出。 我在javascript中有照片陣列。 現在,我正在嘗試將php添加到我的html中。 我希望能夠通過url獲取userid,然后從該目錄中獲取來自特定路徑的照片。 然后,我希望創建這些照片的數組,並在我的JavaScript中使用它們。 這是我的html中嵌入的我的php代碼:
<?php
$user_id = $_GET['userid'];
print " Hi, $user_id ";
function returnimages($dirname = "Photos/1") { //will replace 1 with userid once something starts working
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
echo 'galleryarray['.$curimage.']="'.$file .'";';
$curimage++;
}
}
closedir($handle);
}
return($files);
}
echo 'var galleryarray=new Array();'; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names
?>
而我的javascript:
$(document).ready(function(){
var photodisplay =
[
$("#photo1"),
$("#photo2"),
$("#photo3"),
$("#photo4"),
$("#photo5"),
];
//photodisplay[0].hide().fadeIn(3000);
var user = new Array();
[1, 2, 3, 4, 5];
// List of images for user one
/*var userphoto = new Array();
userphoto[0] = "Photos/1/1.jpg";
userphoto[1] = "Photos/1/2.jpg";
userphoto[2] = "Photos/1/1.jpg";
userphoto[3] = "Photos/1/1.jpg";
userphoto[4] = "Photos/1/1.jpg";*/
//preloading photos
var userphoto = <? echo json_encode($galleryarray); ?>;
function preloadingPhotos() {
for (var x=0; x<5; x++)
{
photodisplay[x].attr("src", "Photos/1" + userphoto[x]);
photodisplay[x].hide();
console.log("preloaded photos");
}
displayPhoto();
}
function displayPhoto(){
photodisplay[0].fadeIn(3000);
photodisplay[0].delay(3000).fadeOut(3000, function() { //first callback func
photodisplay[1].fadeIn(3000);
photodisplay[1].delay(3000).fadeOut(3000, function() { //second callback func
photodisplay[2].fadeIn(3000);
photodisplay[2].delay(3000).fadeOut(3000, function() { //third callback func
photodisplay[3].fadeIn(3000);
photodisplay[3].delay(3000).fadeOut(3000, function() { // fourth callback func
photodisplay[4].fadeIn(3000);
photodisplay[4].delay(3000).fadeOut(3000, function() {
setTimeout(displayPhoto(), 3000);
});
});
});
});
});
}// end of function displayPhoto
window.onload = preloadingPhotos;
}); //准備就緒
我獲取用戶ID的網址:
http://example.com/code.php?user_id=1
感謝您的時間!
問題是您總是在設置目錄名,而不是調用函數設置它。 您可以更改:
function returnimages($dirname = "Photos/1") {
至
function returnimages($dirname) {
因為否則$dirname
始終為Photo/1
。 然后,當您調用該函數時,請使用:
returnimages('Photos/'.$user_id);
您可以使用點“。”在PHP中進行串聯。 這將連接兩個字符串,然后將它們分配給變量$ dirname。 例如:
$dirname = "Photos/" . $_GET['ID'];
然后可以將變量$ dirname放在函數returnimages中,例如:
returnimages($dirname);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.