0

This is my code:

$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/>
<?

}

The problem is that it is only displaying the last record in the table when I echo $grid1 or $grid2 rather than all of them.

Please can anyone help?

1
  • 2
    they're not arrays, so when you assign new values to them, they overwrite the last value Commented Mar 21, 2013 at 18:40

4 Answers 4

0

$grid1 and $grid2 need to be an arrays. Otherwise you're just overwriting your value each time you go through the loop.

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

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

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

You should echo $grid1 and $grid2 inside the while loop as each time the loop runs, they are overwritten by the next value.

0

Each time you iterate over the results set you are re-assigning $grid1 and $grid2 with the string you are creating, which is why only the last result is being displayed. What you want to do is use the concatenating assignment operator .= e.g.

$grid1 .= $row['contactFamily']." ".$row['Contact']." ".$row['contactOwner'];
1
  • This has fixed the problem fine but now I am getting undefined variable notices for $grid1 and $grid2. Commented Mar 21, 2013 at 19:46
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++;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.