2011-05-13 59 views
7

我想包括一个文件输出在页面上的标签。文件本身会拉起来很好,但是当我尝试添加所需的查询字符串时,它给了我一个“无法打开流:没有这样的文件或目录”的错误。与PHP的URL查询字符串包括

我已经尝试了一个直接include,并尝试将querystring设置为一个变量。这就是我现在的位置。

$listingVars = '?mls=' . $_REQUEST['mlid'] . '&lid=0&v=agent';include("agentview.php$listingVars");

有没有人成功地做到了这一点?

回答

12

您不能在include()中包含查询字符串。

假设这是一个本地脚本,你可以使用:

$_REQUEST['mls'] = $_REQUEST['mlid']; 
$_REQUEST['lid'] = 0; 
$_REQUEST['v'] = 'agent'; 
include("agentview.php"); 

,如果它是一个不同的服务器上的远程脚本,不要使用包括。

+0

这工作很好 - 现在我必须弄清楚为什么它打破了我的JS!我可以接受这个答案。 – SickHippie 2011-05-13 23:29:25

+0

是否有任何特殊的原因,你需要手动重新请求查询变量这样...或者,为什么呢? – WoodrowShigeru 2017-03-24 16:01:39

3

我创建的第二页上的一个变量 - 并通过它的第一页上的价值 - 和它的工作对我来说:

*Page with include: 'index.php' 
<?php $type= 'simple'; include('includes/contactform.php'); ?> 


*Page included: 'includes/contactform.php' 

switch($type){ 
    case 'simple': 
    //Do something simple 
    break; 

    default: 
    //Do something else 
    break; 
} 
1

我修改由弗兰克农民给予了一下,为工作接受的答案不同的查询:

包括两次会导致问题:

$_REQUEST['mls'] = $_REQUEST['mlid']; 
$_REQUEST['lid'] = 0; 
$_REQUEST['v'] = 'agent'; 
include("agentview.php"); 

//changing the v to another 
$_REQUEST['v'] = 'agent2'; 
include("agentview.php"); 

对于那些谁遇到这样的多个包含的问题,你可以包装在“agentview.php”里面你的代码功能:

内agentview.php

function abc($mls,$lid,$v){ 
    ...your original codes here... 
} 

文件需要调用agentview.php

include_once("agentview.php"); 
abc($_REQUEST['mlid'], 0, 'agent'); 
abc($_REQUEST['mlid'], 0, 'agent2'); 

希望它可以帮助别人遇到同样的问题和我一样,感谢弗兰克农民为我节省了很多时间。