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


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

我有5条数据。

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

目前,我能够将数据放入,并成功取出 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;