• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • 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

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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