Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
2  
they're not arrays, so when you assign new values to them, they overwrite the last value – Jeff Hawthorne Mar 21 '13 at 18:40

$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'];
share|improve this answer

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

share|improve this answer

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'];
share|improve this answer
    
This has fixed the problem fine but now I am getting undefined variable notices for $grid1 and $grid2. – user2179827 Mar 21 '13 at 19:46

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++;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.