We help IT Professionals succeed at work.

Table Row Results

fcruz5
fcruz5 asked
on
229 Views
Last Modified: 2013-12-13
Hi,

I have two table rows that display data. The first table row displays several results. The second table row should only display one result. But it is repeating itself. It displays the same amount of results as the first table row. So if the the first table row displays 10 results then the second table row will display 10 results.

But since the second table row only has one result, then it repeats itself.

How can I make it only appear once? Would I have to add its own separate mysql query on the second "foreach" line?

The code is below:


<?php

if($result && mysql_num_rows($result) > 0)
{    for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $resultArray[$i] = mysql_fetch_array($result);
}

foreach ($resultArray as $val) {
   echo ('<tr>');//first row
      echo ('<td>'.$val[1].'</td>');
      echo ('<td>'.$val[2].'</td>');
      echo ('<td>'.$val[3].'</td>');
    echo ('</tr>');
}

foreach ($resultArray as $val) {
    echo ('<tr>');//second row
      echo ('<td>'.$val[4].'</td>');
      echo ('<td>'.$val[5].'</td>');
      echo ('<td>'.$val[6].'</td>');
    echo ('</tr>');
}

} // end if results
echo ('</table>');

?>
Comment
Watch Question

Kiran Paul VJComputer Engineer
CERTIFIED EXPERT

Commented:
try this

<?php

if($result && mysql_num_rows($result) > 0)
{    for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $resultArray[$i] = mysql_fetch_array($result);
}

foreach ($resultArray as $val) {
   echo ('<tr>');//first row
      echo ('<td>'.$val[1].'</td>');
      echo ('<td>'.$val[2].'</td>');
      echo ('<td>'.$val[3].'</td>');
    echo ('</tr>');
}

foreach ($resultArray as $val) {
    echo ('<tr>');//second row
      echo ('<td>'.$val[4].'</td>');
      echo ('<td>'.$val[5].'</td>');
      echo ('<td>'.$val[6].'</td>');
    echo ('</tr>');
break;
}

} // end if results
echo ('</table>');

?>
Computer Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
The second one did the trick. Thanks!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.