字符串不支持[]运算符.只需一页就可以了 [英] [] operator not supported for strings. It happens JUST in one page

查看:85
本文介绍了字符串不支持[]运算符.只需一页就可以了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote (thanks to Stackoverflow help, too) a PHP script (called wunder_temp.php) which shows the temperature and location for a given number of Weatherunderground.com Stations.

我将此脚本包含在页脚中,但在单个页面上运行良好.如果我打开 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中的字符串不支持[]运算符

I included this script in my footer, and it works well BUT on a single page. If I open http://www.flapane.com/guestbook/guestbook.php the temperatures won't be shown in my footer, and error.log says: [09-Sep-2012 09:46:45 UTC] PHP Fatal error: [] operator not supported for strings in /home/xxx/public_html/wunder_temp.php on line 47

$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无法正常工作.

The weird thing is that guestbook.php is the ONLY page of my website where wunder_temp.php doesn't work.

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

What the above code does is reading the cachefile and put in a $temperature[] array the even lines and in $location[] array the odd lines. Here's a sample from my cachefile:

26.8
Stadio San Paolo di Napoli, Napoli
24.4
Pozzuoli

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

Honestly I don't know why I see that errors just on my guestbook page.

事实证明,罪魁祸首"是函数loadmore.php,它使用Twitter风格的ajax函数加载留言簿注释(包括在guestbook.php中).如果我不包含它,则wunder_temp.php效果很好,并且不会产生任何错误.

It turns out that the "culprit" is the function loadmore.php which loads the guestbook comments (and which is included in guestbook.php) using a twitter-style ajax function. If I don't include it, wunder_temp.php works well, and it doesn't produce any error.

loadmore.php:

loadmore.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样式的加载更多注释功能:

ajax_more.js AJAX twitter-style load-more-comments function:

$(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(上述脚本需要):

ajax_more.php (needed by the above script):

<? 

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

感谢您的帮助

推荐答案

$ location 已经是该页面上的字符串.这就是为什么在使用变量之前正确初始化变量的原因:

$location is already a string on that page. This is why you properly initialize variables before you use them:

$temperature = $location = array();

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

更好的是,更好地分离变量范围,这样就不会出现名称冲突.将函数和类与它们的私有变量范围一起使用,不要将所有内容都放在全局范围内.

Even better, separate your variable scopes better so you don't get a name clash like that. Use functions and classes with their private variable scopes and don't put everything in the global scope.

这篇关于字符串不支持[]运算符.只需一页就可以了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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