954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to show more than one result in while loop

hello friends..

<?while($row=(mysql_fetch_array(result))
{?>
   <tr>
       <td><?echo $row['photo']?></td>
       <td><?echo $row['info']?></td>
</tr>
<?}?>


above code i use for fetching data.. one result in one row at a time..!!!

now i m try to show two result in one row..!!!
in other words.. two different data in one row..!!!
how can i do that...???

nish123
Junior Poster in Training
83 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

How about:

<?PHP
while($row=(mysql_fetch_array(result)) {

   echo "       
   <td>$row['photo']</td><td>$row['info']</td>
         ";
   if ($row=(mysql_fetch_array(result)) {
      echo "
      <td>$row['photo']</td><td>$row['info']</td>
      ";
   }

   echo "</tr>";
}

?>


I find it messy to go in and out of PHP so I modified it to make it all PHP.

chrishea
Nearly a Posting Virtuoso
1,428 posts since Sep 2008
Reputation Points: 210
Solved Threads: 230
 

How about:

<?PHP
while($row=(mysql_fetch_array(result)) {

   echo "       
   <td>$row['photo']</td><td>$row['info']</td>
         ";
   if ($row=(mysql_fetch_array(result)) {
      echo "
      <td>$row['photo']</td><td>$row['info']</td>
      ";
   }

   echo "</tr>";
}

?>

I find it messy to go in and out of PHP so I modified it to make it all PHP.


Just no. For two reasons: Reason 1) You're duplicating code, if he wants to change the td's to dd's or something like that he has to change it in two places, Reason 2) I'm not even going to begin with how much more ugly that looks when compared to exiting PHP


The best way to go about it is to keep a counter and whenever it's divisible by 2 you've looped twice so end the row and start another one.

<tr>
<?php $i = 1; while($row=mysql_fetch_array(result)): ?>
  <?php if (!($i++ % 2) ): ?>
    </tr><tr>
  <?php endif ?>
    <td><?php echo $row['photo'] ?></td>
    <td><?php echo $row['info'] ?></td>
<?php endwhile ?>
</tr>
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

This may be a worth while solution ...

$i = 0; 
$num_of_cols = 5; 
echo "<table cellspacing=\"10\" cellpadding=\"10\"><tr>"; 
while($row=(mysql_fetch_array(result)) {
echo ($i!= 0 && $i%$num_of_cols == 0)?'</tr><tr>':''; 
echo "<td valign=\"bottom\" align=\"center\">";
echo "<Your_output_here>";
echo"</td>"; 
$i++; }
echo '</tr></table>';

You can change the number if columns to whatever you want and it will simply start a new row when you reach that number of columns. Adjust the cellspacing, cellpadding, and aligns accordingly. Just a thought.

CFROG
Posting Pro in Training
408 posts since Jul 2009
Reputation Points: 21
Solved Threads: 31
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You