0

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

Edited by nish123: n/a

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by CFROG
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.

Edited by chrishea: n/a

1

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>

Edited by ShawnCplus: n/a

Votes + Comments
nice and succinct - wish I could see it - class
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.