Jump to content

Archived

This topic is now archived and is closed to further replies.

Cloud9247

Table With Fetch

Recommended Posts

I have been developing my graphics site lately, and I have run into a problem. Currently I am setting up a 100x100 avatars page, and I am using mysql_fetch_array. I want it to have in each row of avatars, 4 avatars per row. and I am currently using tables for this, so can someone help me get it to where every 4 rows it inserts a '<tr>' and '</tr>', so I could get more rows?

Share this post


Link to post
Share on other sites
Have you tried testing this in you loop?

[code=php:0]
<?php
$i=0;
while ($row = $result->fetch_object()) {
    if ($i == 3) { // 4
        $content .= '<tr>';
        $i = 0;
    }
}
[/code]

Share this post


Link to post
Share on other sites
[code]
<?php
$sql = "some query";
$result = mysql_query($sql);
$i = 0;
echo "<table><tr>";
while ($list = mysql_fetch_array($result)) {
  echo "<td>{$list['columnamehere']}</td>";
  if ($i == 3) {
      echo "</tr><tr>";
      $i = 0;
  }
  $i++;
}
echo "</tr></table>"
?>
[/code]

Share this post


Link to post
Share on other sites
[code]
<?php
echo $top1.'Avatars'.$top2.$body1;
echo '<table id= "avatars"><tr>';
$i= 0;
        $sqll= "SELECT userid, id, clicks, username, submitted, name, credits, comments, series, imgurl, active FROM avatars WHERE active='1' ORDER BY `id` DESC LIMIT $from, $max_results"; // LIMIT $from, $max_results
$rss= mysql_query( $sqll, $connection );
while($row= mysql_fetch_array( $rss )) {


$dwn= "return false;";


echo '<td id= "avi"><a href= "http://hayalkarga.com/designs.php?viewavatar='.$row['id'].'"><img src= "'.$row['imgurl'].'" id= "graphic" alt= "'.$row['name'].'" oncontextmenu="return false"></a></td>';

  if ($i == 3) {
      echo "</tr><tr>";
      $i = 0;
  }
$i++;
}
echo '</tr></table>';
echo $body2.$foot1.$foot2;
[/code]

comes out as: http://hayalkarga.com/designs.php?designs=avatars

It prints out 4 on the first row, but only 3 on the others. Can someone help me fix this?

Share this post


Link to post
Share on other sites
That's because after setting $i=0 it then adds one before starting the loop.

Replace $i==3 with $i==4 and before the loop replace $i=0; with $i=1;

Share this post


Link to post
Share on other sites
Actually, I just figured out to replace it with $i= -1, which works fine, thanks for your help everyone!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.