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...???

Recommended Answers

All 3 Replies

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.

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>
commented: nice and succinct - wish I could see it - class +4

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.