【问题标题】:[] operator not supported for strings. It happens JUST in one page [duplicate][] operator not supported for strings. It happens JUST in one page [duplicate]
【发布时间】:2012-09-02 12:39:09
【问题描述】:

我(也感谢 Stackoverflow 的帮助)编写了一个 PHP 脚本(称为 wunder_temp.php),它显示了给定数量 Weatherunderground.com 站点的温度和位置。

我在页脚中包含了这个脚本,它运行良好,但在单个页面上。 如果我打开http://www.flapane.com/guestbook/guestbook.php,温度将不会显示在我的页脚中,并且 error.log 会显示: [09-Sep-2012 09:46:45 UTC] PHP 致命错误:第 47 行 /home/xxx/public_html/wunder_temp.php 中的字符串不支持 [] 运算符

$display = file($cachename, FILE_IGNORE_NEW_LINES); //ignore \n for non-reporting stations
foreach ($display as $key=>$value){
    if($key % 2 == 0){  
         $temperature[] = $value; // EVEN (righe del file cache pari)
    }else{
         $location[] = $value;  // ODD - **HERE IS LINE 47**
    }
}

奇怪的是,guestbook.php 是我网站上 wunder_temp.php 不起作用的唯一页面。

上述代码所做的是读取缓存文件并将偶数行放入 $temperature[] 数组中,将奇数行放入 $location[] 数组中。 这是我的缓存文件中的一个示例:

26.8
Stadio San Paolo di Napoli, Napoli
24.4
Pozzuoli

老实说,我不知道为什么我只在我的留言簿页面上看到这些错误。

事实证明,“罪魁祸首”是函数 loadmore.php,它使用 twitter 风格的 ajax 函数加载留言簿 cmets(并且包含在 guestbook.php 中)。如果我不包含它,wunder_temp.php 运行良好,并且不会产生任何错误。

加载更多.php:

<div id='contcomment'>
<div class="timeline" id="updates">
<?php
$sql=mysql_query("select * from gbook_comments ORDER BY id DESC LIMIT 9");
while($row=mysql_fetch_array($sql))
{
$msg_id=$row['id'];
$name=$row['name'];
$url=$row['url'];
$email=$row['email'];
$location=$row['location'];
$date= strtotime($row['dt']); //unix timestamp
$country_code=$row['country_code'];
$message=$row['body'];


        $link_open = '';
        $link_close = '';

        if($url){

            // If the person has entered a URL when adding a comment,
            // define opening and closing hyperlink tags

            $link_open = '<a href="'.$url.'" target="_blank" rel="nofollow">';
            $link_close =  '</a>';
        }

// Needed for the default gravatar image:
        $url_grav = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';



        // Show flags
        $image = strtolower($country_code) . ".png";
        if (file_exists("./img/flags/" . $image)){
        $show_image = true;
        $image_link = "<img src='/guestbook/img/flags/$image' alt='user flag' />";
        }else{
        $show_image = false;
        }


        echo    '<div class="comment">
                <div class="avatar">
                    '.$link_open.'
                    <img src="http://www.gravatar.com/avatar/'.md5($email).'?size=50&amp;default='.urlencode($url_grav).'" alt="gravatar icon" />
                    '.$link_close.'
                </div>

                <div class="name">'.$link_open.$name.$link_close.' </div><div class="location"><i>(da/from '.$location.' '.$image_link.' )</i></div>
                <div class="date" title="Added at '.date('H:i \o\n d M Y',$date).'">'.date('d M Y',$date).'</div>
                <p>'.$message.'</p>

            </div>' ;
} ?>
</div>
<div id="more<?php echo $msg_id; ?>" class="morebox">
<a href="#" class="more" id="<?php echo $msg_id; ?>">Carica Commenti pi&ugrave; vecchi / Load older entries</a>
</div>
</div>

ajax_more.js AJAX twitter-style load-more-cmets 函数:

$(function() {
//More Button
$('.more').live("click",function() 
{
var ID = $(this).attr("id");
if(ID)
{
$("#more"+ID).html('<img src="moreajax.gif" />');

$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID, 
cache: false,
success: function(html){
$("div#updates").append(html);
$("#more"+ID).remove();
}
});
}
else
{
$(".morebox").html('Nessun altro commento / No more comments');

}


return false;


});
});

ajax_more.php(上面脚本需要):

<? 

include "connect.php"; 

if(isSet($_POST['lastmsg']))
{
$lastmsg=$_POST['lastmsg'];
$lastmsg=mysql_real_escape_string($lastmsg);
$result=mysql_query("select * from gbook_comments where id<'$lastmsg' order by id desc limit 9");
$count=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$msg_id=$row['id'];
$name=$row['name'];
$url=$row['url'];
$email=$row['email'];
$location=$row['location'];
$date= strtotime($row['dt']); //unix timestamp
$country_code=$row['country_code'];
$message=$row['body'];


        $link_open = '';
        $link_close = '';

        if($url){

            // If the person has entered a URL when adding a comment,
            // define opening and closing hyperlink tags

            $link_open = '<a href="'.$url.'" target="_blank" rel="nofollow">';
            $link_close =  '</a>';
        }

// Needed for the default gravatar image:
        $url_grav = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';



        // Show flags
        $image = strtolower($country_code) . ".png";
        if (file_exists("./img/flags/" . $image)){
        $show_image = true;
        $image_link = "<img src='/guestbook/img/flags/$image' alt='user flag' />";
        }else{
        $show_image = false;
        }


    echo    '<div class="comment">
                <div class="avatar">
                    '.$link_open.'
                    <img src="http://www.gravatar.com/avatar/'.md5($email).'?size=50&amp;default='.urlencode($url_grav).'" alt="gravatar icon" />
                    '.$link_close.'
                </div>

                <div class="name">'.$link_open.$name.$link_close.' </div><div class="location"><i>(da/from '.$location.' '.$image_link.' )</i></div>
                <div class="date" title="Added at '.date('H:i \o\n d M Y',$date).'">'.date('d M Y',$date).'</div>
                <p>'.$message.'</p>

            </div>' ;       

}


?>

<div id="more<?php echo $msg_id; ?>" class="morebox">
<a href="#" id="<?php echo $msg_id; ?>" class="more">Carica Commenti pi&ugrave; vecchi / Load older entries</a>
</div>

<?php
}
?>

感谢任何帮助

【问题讨论】:

  • 在 foreach 循环之前添加行 $location=array(); 看看是否有帮助

标签: php ajax arrays string


【解决方案1】:

$location 已经是该页面上的字符串。这就是为什么您在使用它们之前正确地初始化变量:

$temperature = $location = array();

foreach ($display as $key => $value){
    if ($key % 2 == 0){  
         $temperature[] = $value;
    } else {
         $location[] = $value;
    }
}

更好的是,更好地分离你的变量范围,这样你就不会遇到这样的名称冲突。使用具有私有变量范围的函数和类,不要将所有内容都放在全局范围内。

【讨论】:

    【解决方案2】:

    这似乎是一个变量范围的问题。

    当您在guestbook.php 中包含loadmore.php 时,您将$location 隐式声明为字符串:

    $location=$row['location'];
    

    所以你循环的这一行:

     $location[] = $value;  // ODD - **HERE IS LINE 47**
    

    不是在第一次迭代时隐式声明一个新的数组变量,而是试图将 $value 附加到先前声明的字符串变量,因此出现错误。

    尝试在 loadmore.php 或 guestbook.php 中为 $location 赋予不同的名称,或者使 loadmore.php 成为一个函数(因此它在自己的范围内运行),然后在包含其代码后从 guestbook.php 脚本中调用它在上面。或者,如果您想将 $location 变量作为数组重用,则使用显式声明(只需在循环前添加 $location = array();)。

    【讨论】:

      【解决方案3】:

      我认为 loadmore.php 和/或 ajaxmore.php 正在将全局变量 ($location) 设置为字符串。当留言簿页面尝试索引此变量时,您会收到错误。

      解决方案:使用函数和局部变量。

      【讨论】:

      • $location 可能仍然是“本地的”,但 PHP 没有块级范围,所以因为它不在函数内......它在请求期间保持在范围内。跨度>
      【解决方案4】:

      $location=$row['location']; 是问题所在。因为 PHP 没有块级作用域,所以您实际上是在预先设置此变量并尝试使用 [] 设置它的数组索引。因为它是一个字符串,所以会中断。

      您应该始终初始化变量以避免这些类型的冲突: IE。 $location = array();

      【讨论】:

        【解决方案5】:

        为其中一个脚本重命名 $location。您在某处有 $location=$row['location'] 。另外,在将其用作数组时声明该变量:

        $array = array();
        $array[] = 'item'; // when you add an item here, $array will always accept an array push.
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-08
          • 1970-01-01
          • 2019-04-15
          • 2011-03-25
          • 1970-01-01
          • 2022-12-02
          • 1970-01-01
          • 2022-12-27
          相关资源
          最近更新 更多