0

这是我的代码:

$result = mysql_query ($query) or die (mysql_error());
$row = mysql_fetch_assoc($result);

while($row = mysql_fetch_array( $result )) {

$contactID=$row['contactID'];
$contactFamily =$row ['contactFamily'];
$Contact=$row ['Contact'];
$contactOwner =$row ['contactOwner'];
$Impact=$row ['Impact'];
$Probability =$row ['Probability'];



if ($row ['Impact']=="5" && $row['Probability']=="4")
{
    $grid1=  $row ['contactFamily']." ".$row ['Contact']." .$row['contactOwner'];
}

?>
<br/>
<?php

if ($row ['Impact']=="3" && $row['Probability']=="4")
{
    $grid2=  $row ['contactFamily']." ".$row ['Contact']." .$row['contactOwner'];
}
?>
<br/>
<?

}

问题是当我回显 $grid1 或 $grid2 而不是全部时,它只显示表中的最后一条记录。

请问有人可以帮忙吗?

4

4 回答 4

0

You are filling a variable multiple times but it only keeps the last value, that's the reason you always see the last value. I've changed your code using arrays:

<?php
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_assoc($result);
$i = 0
while($row = mysql_fetch_array($result)) {
    $data[$i]['contactID'] = $row['contactID'];
    $data[$i]['contactFamily'] = $row['contactFamily'];
    $data[$i]['Contact'] =$row['Contact'];
    $data[$i]['contactOwner'] = $row['contactOwner'];
    $data[$i]['Impact'] = $row['Impact'];
    $data[$i]['Probability'] = $row['Probability'];

    if ($row['Impact'] == "5" && $row['Probability'] =="4") {
        $grid1[$i] = $row['contactFamily'] . " " . $row ['Contact'] . " " . $row['contactOwner'];
    }
    if ($row['Impact']=="3" && $row['Probability']=="4") {
        $grid2[$i] = $row['contactFamily'] . " " . $row ['Contact'] . " " . $row['contactOwner'];
    }
    $i++;
}
于 2013-03-21T18:49:19.210 回答
0

$grid1并且$grid2需要是一个数组。否则,您每次通过循环时都会覆盖您的值。

$grid1 = array();
$grid2 = array();
while($row = mysql_fetch_array( $result )) {

$grid1[] =  $row ['contactFamily']." ".$row ['Contact']." ".$row['contactOwner'];

$grid2[] =  $row ['contactFamily']." ".$row ['Contact']." ".$row['contactOwner'];
于 2013-03-21T18:40:22.440 回答
0

您应该在 while 循环中回显 $grid1 和 $grid2 ,因为每次循环运行时,它们都会被下一个值覆盖。

于 2013-03-21T18:41:47.790 回答
0

每次迭代结果集时,您都在重新分配$grid1$grid2使用正在创建的字符串,这就是为什么只显示最后一个结果的原因。你想要做的是使用连接赋值运算符,.=例如

$grid1 .= $row['contactFamily']." ".$row['Contact']." ".$row['contactOwner'];
于 2013-03-21T18:42:19.883 回答