【问题标题】:How do you use a php variable for directory path?如何使用 php 变量作为目录路径?
【发布时间】:2014-02-07 12:02:50
【问题描述】:

我从 url 获取用户 ID。 这就是我目前所拥有的。我想用 $userid 替换那个,但我不知道怎么做。它不起作用,我似乎找不到正确的语法,请有人帮忙吗?

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

基本上我正在尝试使用 html、php 和 javascript 创建照片幻灯片。在我开始将 php 添加到我的代码中之前,我有一些工作。我有 html 和一个外部 javascript 来更改照片,并且它们在循环中淡入淡出。我在 javascript 中有一个照片数组。现在我正在尝试将 php 添加到我的 html 中。我希望能够通过 url 获取用户 ID,然后从中获取从特定路径到目录中用户 ID 的照片。然后我希望创建这些照片的数组并在我的 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:

$(文档).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;

}); //结束准备好

我获取用户名的网址:

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

感谢您的宝贵时间!

【问题讨论】:

    标签: javascript php html directory


    【解决方案1】:

    问题是你总是设置目录名而不是让调用函数来设置它。你可以改变:

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

    function returnimages($dirname) {
    

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

    returnimages('Photos/'.$user_id);
    

    【讨论】:

    • 我还是不明白为什么将路径放在函数参数中不起作用。不一样吗?使困惑。但它有效!你知道如何在 javascript 中从 php 调用数组吗? ://
    • 不客气。问题是您没有在函数参数中放置路径。您基本上是在设置变量 dirname ,而忽略了调用函数时设置的任何内容。如果你想在 javascript 中放置一个 php 数组,这取决于你是如何编码的。你是把php放在一个完全javascript、html、还是php页面的javascript中?
    • 但这不也可以吗? function returnimages($dirname = "Photos/" .$user_id) { 我正在将变量 dirname 设置为该变量,但它会根据用户 ID 而变化? (抱歉只是不明白,php 很难!) 嗯,我有一个 html 页面,但现在是 php,因为我已经添加了 php。并由此调用外部javascript。所以不知道该怎么做:S我只是从不同的网站得到不同的方法,只是感到困惑:(
    • 我知道我似乎没有做太多研究或尝试过,但我有:(这真的很令人困惑。距离完成我打算做的事情还很远,但我很自豪我有一些工作 ish :) 但是,是的,再次感谢你解决了我的另一个里程碑 :D:D
    • 是的,该函数调用也可以,但主要问题实际上是声明该函数不调用它。然而,这种写法并不像它应该的那样简洁。
    【解决方案2】:

    您可以在 PHP 中使用点“.”进行连接。这将连接两个字符串,然后将它们分配给变量 $dirname。例如:

     $dirname = "Photos/" . $_GET['ID'];
    

    然后可以将变量 $dirname 放在函数 returnimages 中,例如:

    returnimages($dirname);
    

    【讨论】:

    • 感谢您的回复。我只是这样做了,但它仍然不起作用:SI 收到此错误:解析错误:语法错误,意外 '.',期待 ')' 在......第 17 行我是 PHP 的完整初学者,并希望有任何帮助!
    • 更多关于您工作的信息可以帮助我们,URL 是什么样的?困扰您的部分的代码是什么?您已经尝试过什么,结果如何?
    • 我刚刚编辑了我的问题。谢谢!希望这对我想要实现的目标更有意义? :S 很抱歉让我们如此混乱和含糊。
    • 函数有参数,即 dosomething($var1) { }。你必须为函数提供一个值,这个 dosomething("Hello world");当您将函数参数定义为 $var = "Hello world" 时,您可以在不为 $var1 提供值的情况下调用该函数,但是已经定义了一个参数的函数不是函数,而是代码的一部分。当您调用 returnimages 函数时,您必须提供输入,此输入是 $dirname。这个变量需要分配一个值,在你的情况下 $dirname = "Photos/" 。 $user_id;
    猜你喜欢
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2017-10-23
    • 2018-07-28
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    相关资源
    最近更新 更多