• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Table Row Results

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>');

?>
0
fcruz5
Asked:
fcruz5
  • 2
1 Solution
 
Kiran Paul VJComputer EngineerCommented:
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>');

?>
0
 
Kiran Paul VJComputer EngineerCommented:
or

<?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>');
}


    echo ('<tr>');//second row
      echo ('<td>'.$resultArray[0][4].'</td>');
      echo ('<td>'.$resultArray[0][5].'</td>');
      echo ('<td>'.$resultArray[0][6].'</td>');
    echo ('</tr>');


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

?>
0
 
fcruz5Author Commented:
The second one did the trick. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now