你如何使用目录路径的PHP变量? [英] How do you use a php variable for directory path?

查看:194
本文介绍了你如何使用目录路径的PHP变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网址获取用户ID。
这是我目前所拥有的。我想用$ userid替换它,但我不知道如何。它不工作,我似乎无法找到正确的语法,请有人可以帮忙吗?

I am getting userid from the url. This is what I have at the moment. I want to replace the one with $userid but I don't know how. It doesn't work and I can't seem to find the right syntax, please can someone help?

function returnimages($dirname = "Photos/1") 

基本上我试图用html,php创建照片幻灯片和JavaScript。在开始将PHP添加到我的代码之前,我做了一些工作。我有HTML和一个外部的JavaScript改变照片,他们淡入和退出循环。我有一个JavaScript的照片数组。现在我正在尝试将PHP添加到我的html中。我希望能够通过url获得userid,然后从中获取照片从特定路径到目录中的userid。然后,我希望能够创建一组这些照片并在我的javascript中使用它们。这里是我的PHP代码嵌入在我的HTML:

Basically I am trying to create a photo slideshow using html, php and javascript. I had something working before I started adding php into my code. I had html and an external javascript that changes the photos and they fade in and out in a loop. I have a photo array in javascript. Right now I am trying to add php to my html. I want to be able to get userid via url and then from that get the photos from a specific path to the userid in the directory. Then I am hoping to create an array of these photos and use them in my javascript. Here is my php code embedded in my html:

<?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:

And my javascript:

$(文件).ready(function(){

$ (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;

}); // end ready

}); //end ready

我的网址获得用户ID:

My url to get userid:

http://example.com/code.php?user_id=1

感谢您的时间!

推荐答案

问题是你总是设置dirname而不是让函数调用它。您可以更改:

The problem is that you are always setting the dirname instead of letting calling the function set it. You could change:

function returnimages($dirname = "Photos/1") {

function returnimages($dirname) {

因为否则 $ dirname 是总是照片/ 1 。然后,当您调用该函数时,请使用:

because otherwise the $dirname is always Photo/1. Then, when you call the function, use:

returnimages('Photos/'.$user_id);

这篇关于你如何使用目录路径的PHP变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆