开发者

Multi-Column Table Display from one MySQL table

开发者 https://www.devze.com 2023-03-07 20:36 出处:网络
I have a problem with displaying multiple columns from a MySQL table. It is fairly simple - I have 28 records, and I want to make 7 rows of 4 - each record is just two columns - id# and name (\"handle

I have a problem with displaying multiple columns from a MySQL table.

It is fairly simple - I have 28 records, and I want to make 7 rows of 4 - each record is just two columns - id# and name ("handle" in table).

What my problem is that when I use the

> $i<$cols

I am getting four columns, but the fourth one is blank, and every fourth record is being skipped (#4, #8, etc.) Simple I said...just make it $row && $i<=$cols adding the <= conditional to the $i variable...but then it just ignores the if statement and displays all the records across the page. What am I doing wrong? Do I need another if statement wrapped around that one? Not sure where I am going wrong...

Here is where I have gotten so far:

// Table header.
echo '<center><table align="center" BLAH BLAH BLAH</b></td></tr><tr>';
do {
  // Fetch and print all the records:
  $cols = 4;
  for($i=1;$i<=$cols;$i++){ 
    $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
    if($row && $i<$cols){
      echo '<td  align="center" bgcolor="' . '#' . $row['color'] . '"><b>' .
           $row['player_id'] . '</td></b><td  align="left" bgcolor="' . '#' .
           $row['color'] . '">&开发者_运维技巧lt;b>' . strtoupper($row['handle']) . '</td></b>';
    }
    else {
      echo '</tr>';
    }
  }
}              
while ($row);
  echo '</table></center>'; // Close the table.


Just move the echo '</tr>' outside of the for loop. Have the do...while loop open and close the <tr> tags:

// Table header.
echo '<center><table align="center" BLAH BLAH BLAH</b></td></tr>';
do {
  echo '<tr>';
  // Fetch and print all the records:
  $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
  if($row){
    echo '<td  align="center" bgcolor="' . '#' . $row['color'] . '"><b>' .
         $row['player_id'] . '</b></td><td  align="left" bgcolor="' . '#' .
         $row['color'] . '"><b>' . strtoupper($row['handle']) . '</b></td>';
  }
  echo '</tr>';
} while ($row); echo '</table></center>'; // Close the table.

EDIT: just realized you don't need the for loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消