How to show more than one result in while loop

Reply

Join Date: Apr 2009
Posts: 83
Reputation: nish123 is an unknown quantity at this point 
Solved Threads: 0
nish123's Avatar
nish123 nish123 is offline Offline
Junior Poster in Training

How to show more than one result in while loop

 
0
  #1
Sep 29th, 2009
hello friends..
  1. <?while($row=(mysql_fetch_array(result))
  2. {?>
  3. <tr>
  4. <td><?echo $row['photo']?></td>
  5. <td><?echo $row['info']?></td>
  6. </tr>
  7. <?}?>

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...???
Last edited by nish123; Sep 29th, 2009 at 1:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 214
Reputation: chrishea is an unknown quantity at this point 
Solved Threads: 32
chrishea's Avatar
chrishea chrishea is offline Offline
Posting Whiz in Training

Re: How to show more than one result in while loop

 
0
  #2
Sep 29th, 2009
How about:

  1. <?PHP
  2. while($row=(mysql_fetch_array(result)) {
  3.  
  4. echo "
  5. <td>$row['photo']</td><td>$row['info']</td>
  6. ";
  7. if ($row=(mysql_fetch_array(result)) {
  8. echo "
  9. <td>$row['photo']</td><td>$row['info']</td>
  10. ";
  11. }
  12.  
  13. echo "</tr>";
  14. }
  15.  
  16. ?>

I find it messy to go in and out of PHP so I modified it to make it all PHP.
Last edited by chrishea; Sep 29th, 2009 at 3:34 pm.
Chris
See my list of PHP development tools at:
InnovationsDesign.net
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: How to show more than one result in while loop

 
1
  #3
Sep 29th, 2009
Originally Posted by chrishea View Post
How about:

  1. <?PHP
  2. while($row=(mysql_fetch_array(result)) {
  3.  
  4. echo "
  5. <td>$row['photo']</td><td>$row['info']</td>
  6. ";
  7. if ($row=(mysql_fetch_array(result)) {
  8. echo "
  9. <td>$row['photo']</td><td>$row['info']</td>
  10. ";
  11. }
  12.  
  13. echo "</tr>";
  14. }
  15.  
  16. ?>

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.

  1. <tr>
  2. <?php $i = 1; while($row=mysql_fetch_array(result)): ?>
  3. <?php if (!($i++ % 2) ): ?>
  4. </tr><tr>
  5. <?php endif ?>
  6. <td><?php echo $row['photo'] ?></td>
  7. <td><?php echo $row['info'] ?></td>
  8. <?php endwhile ?>
  9. </tr>
Last edited by ShawnCplus; Sep 29th, 2009 at 5:28 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 210
Reputation: CFROG is an unknown quantity at this point 
Solved Threads: 14
CFROG's Avatar
CFROG CFROG is offline Offline
Posting Whiz in Training

Re: How to show more than one result in while loop

 
0
  #4
Sep 29th, 2009
This may be a worth while solution ...
  1. $i = 0;
  2. $num_of_cols = 5;
  3. echo "<table cellspacing=\"10\" cellpadding=\"10\"><tr>";
  4. while($row=(mysql_fetch_array(result)) {
  5. echo ($i!= 0 && $i%$num_of_cols == 0)?'</tr><tr>':'';
  6. echo "<td valign=\"bottom\" align=\"center\">";
  7. echo "<Your_output_here>";
  8. echo"</td>";
  9. $i++; }
  10. 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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC