Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文件\u获取\u内容和变量问题_Php_Mysql_Function_Variables_Foreach - Fatal编程技术网

Php 文件\u获取\u内容和变量问题

Php 文件\u获取\u内容和变量问题,php,mysql,function,variables,foreach,Php,Mysql,Function,Variables,Foreach,我有一个函数,它使用file\u get\u内容获取一段html,其中包含一些变量。然后,我们的想法是使用这个函数从数据库中获取并返回的一些内容来更新变量。但是,在输出上,变量只读取$myNameVar而不是My Name。让我粘贴我的代码 function query($query,$item) { $this->item = file_get_contents("/pages/items/".$item.".php"); $this-&g

我有一个函数,它使用file\u get\u内容获取一段html,其中包含一些变量。然后,我们的想法是使用这个函数从数据库中获取并返回的一些内容来更新变量。但是,在输出上,变量只读取$myNameVar而不是My Name。让我粘贴我的代码

    function query($query,$item)
    {
        $this->item = file_get_contents("/pages/items/".$item.".php");
        $this->queryResult = mysql_query($query);
        if(mysql_num_rows($this->queryResult))
      {
            while($row = mysql_fetch_assoc($this->queryResult))
            {
                extract($row);
                // $myNameVar = My Name;
                // in the finished code there will be a bunch (15 or more) vars
                // that needs to be updated in the Item file so the vars
                // must be updated automatically
                return $this->item;

            }
      }
    }
以及item.php的内容:

<p>My name is <span style="color: #000;">$myNameVar</span></p>
何时应该:

My Name is My Name
我尝试将item.php文件中的变量替换为%%,并运行foreach更新变量,如下所示(不同的函数):

这是部分有效的,不幸的是,这只是返回每行的第一项。因此,如果在第一行$myNameVar=Joe和第二行$myNameVar=James中,这两段都会将名称列为Joe,如下所示:Joe-Joe,而不是Joe-James


任何帮助都将不胜感激:)

如果您希望代码实际执行某些操作,那么您可能不应该在第一行返回

 function query($query,$item)
        {
            $item = file_get_contents("/pages/items/".$item.".php");
$this->queryResult = mysql_query($query);
    if(mysql_num_rows($this->queryResult))
  {
        while($row = mysql_fetch_assoc($this->queryResult))
        {
            foreach ($row as $key => $value)
            {
                $this->item = str_replace("%".$key."%",$value,$this->item);
            }

return $item;
我的名字是$myNameVar

需要

<p>My name is <span style="color: #000;"><?php echo $myNameVar ?></span></p>
我的名字是


这是因为
file\u get\u contents()
将文件内容作为字符串传递,而不是作为可执行的PHP代码


改为使用
include()

删除了第一行中的返回值,该返回值在执行一些测试后由于错误而保留。谢谢,也尝试过了,它只是输出而不是修复。错把它放在那里了谢谢你,很有魅力!在此上下文中甚至从未使用include()。。
    function query($query,$item)
    {
        $this->item = file_get_contents("/pages/items/".$item.".php");
        $this->queryResult = mysql_query($query);
        if(mysql_num_rows($this->queryResult))
      {
            while($row = mysql_fetch_assoc($this->queryResult))
            {
                foreach ($row as $key => $value)
                {
                    $this->item = str_replace("%".$key."%",$value,$this->item);
                }
                return $this->item;

            }
      }
    }
 function query($query,$item)
        {
            $item = file_get_contents("/pages/items/".$item.".php");
$this->queryResult = mysql_query($query);
    if(mysql_num_rows($this->queryResult))
  {
        while($row = mysql_fetch_assoc($this->queryResult))
        {
            foreach ($row as $key => $value)
            {
                $this->item = str_replace("%".$key."%",$value,$this->item);
            }

return $item;
<p>My name is <span style="color: #000;">$myNameVar</span></p>
<p>My name is <span style="color: #000;"><?php echo $myNameVar ?></span></p>