繁体   English   中英

在循环内向var分配多个数据,并在循环外尝试使用var

[英]Assigning multiple data to var inside loop and trying to use var outside the loop

我有5条数据。

我将所有5条数据放入while循环内的变量中。 然后,我尝试在while循环之外使用use变量-但将所有放入的数据仍然回显。

目前,我可以放入数据,并成功取出1条数据。 我想回显所有5条数据。

码:

        $s = <a search query that gets data from external db>
        while($data = $r->FetchRow($s)) { 
        $addr = 'test address';
        if($data['image'] == '') { $data['image'] = 'nophoto.jpg';}
            $a = '<div style="height: 85px; width: 100%;"><img src="http://website.com/'.$data['image'].'" align="left" border="0" hspace="15" alt="Click for details" height="85px" width="120px" />'.$addr.'';
                                    }
        $m = "This is a test message <br />" .
        $m = "".$a."" . 
        $m = "This is the end of a test message";
        echo $m;

在循环中,您正在为$a赋值。

因此,最新值将覆盖旧值,因此您将获得最后一个值。

如果要获取所有数据,则需要循环添加$a

更正的代码:

$a = '';
$s = <a search query that gets data from external db>
while($data = $r->FetchRow($s)) {
 $addr = 'test address';
 if($data['image'] == '') {
  $data['image'] = 'nophoto.jpg';
 }
 $a .= '<div style="height: 85px; width: 100%;"><img src="http://website.com/'.$data['image'].'" align="left" border="0" hspace="15" alt="Click for details" height="85px" width="120px" />'.$addr.'';
}
$m = "This is a test message <br />" .
$m = "".$a."" . 
$m = "This is the end of a test message";
echo $m;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM