943,900 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 893
  • PHP RSS
Sep 29th, 2009
0

How to show more than one result in while loop

Expand Post »
hello friends..
php Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
nish123 is offline Offline
83 posts
since Apr 2009
Sep 29th, 2009
0

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

How about:

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 210
Solved Threads: 228
Nearly a Posting Virtuoso
chrishea is offline Offline
1,389 posts
since Sep 2008
Sep 29th, 2009
1

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

Click to Expand / Collapse  Quote originally posted by chrishea ...
How about:

PHP Syntax (Toggle Plain Text)
  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.

php Syntax (Toggle Plain Text)
  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.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Sep 29th, 2009
0

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

This may be a worth while solution ...
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 31
Posting Pro in Training
CFROG is offline Offline
405 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Help me convert this ASP into PHP plz :-)
Next Thread in PHP Forum Timeline: Show region behavior for a php generated txt box





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC