为 PHP 查询创建网格视图

dav*_*lot 1 php mysql grid view

所以我试图弄清楚如何获取 PHP 查询并将它们显示在网格视图中。我想让它成为 3 列的网格,所以它看起来像这样: 第一列:1、2、3 第二列:4、5、6 等等...

这是我通过 PHP 查看 MySQL 查询的代码:

<div class='main_col'> <!--Start of the Main-->
<div class='main_content'>
<?php
include("includes/connect.php");

$select_games = "SELECT * FROM games";

$run_games = mysql_query($select_games);

while($row = mysql_fetch_array($run_games)){

$game_id = $row['game_id'];
$game_name = $row['game_name'];
$game_category = $row['game_name'];
$game_keywords = $row['game_name'];
$game_image = $row['game_image'];

?>

<p><a href="game_page.php?id=<?php echo $game_id; ?>"><?php echo $game_name; ?></a></p>

<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" /></a>

<?php } ?>
</div>
</div> <!--End of the Main-->
Run Code Online (Sandbox Code Playgroud)

我试过这个:

<div class='main_col'> <!--Start of the Main-->
<div class='main_content'>
<?php
include("includes/connect.php");

$select_games = "SELECT * FROM games";

$run_games = mysql_query($select_games);

while($row = mysql_fetch_array($run_games)){

$game_id = $row['game_id'];
$game_name = $row['game_name'];
$game_category = $row['game_name'];
$game_keywords = $row['game_name'];
$game_image = $row['game_image'];

?>

<table>

<tr>
<td><p><a href="game_page.php?id=<?php echo $game_id; ?>"><?php echo $game_name; ?></a></p>

<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php      echo $game_image; ?>" width="120" /></a></td>
</tr>
</table>
<?php } ?>
</div>
</div> <!--End of the Main-->
Run Code Online (Sandbox Code Playgroud)

有人建议吗?:(

Tul*_*ris 5

首先,您确实应该放弃使用已弃用的mysql 扩展。相反,您应该使用MySQLiPDO

接下来,您想要一个包含 3 列的表。每个数据字段应包含 3 场比赛。这意味着每 3 场比赛后,您需要进行 1 场比赛,每 9 场比赛后,开始新的一排。您可以使用模运算符 (%) 来查看您何时进行了 3 场(或 6 场或 9 场等)比赛,以及何时进行了 9/18/27/等。游戏,像这样:

<div class='main_col'> <!--Start of the Main-->
<div class='main_content'>
<?php
include("includes/connect.php");

$run_games = mysql_query("SELECT * FROM games");

echo '<table>';
$games = 0;
while($row = mysql_fetch_array($run_games)){
   // make a new row after 9 games
   if($games%9 == 0) {
      if($games > 0) {
         // and close the previous row only if it's not the first
         echo '</tr>';
      }
      echo '<tr>';
   }
   // make a new column after 3 games
   if($games%3 == 0) {
      if($games > 0) {
         // and only close it if it's not the first game
         echo '</td>';
      }
      echo '<td>';
   }

   $game_id = $row['game_id'];
   $game_name = $row['game_name'];
   $game_category = $row['game_name'];
   $game_keywords = $row['game_name'];
   $game_image = $row['game_image'];
   ?>

   <a href="game_page.php?id=<?php echo $game_id; ?>"><?php echo $game_name; ?></a><br />
   <a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" /></a>
   <?php 
   $games++; // increment the $games element so we know how many games we've already processed
}
?>
</table>
</div>
</div> <!--End of the Main-->
Run Code Online (Sandbox Code Playgroud)

或者,如果您只想每个场一场比赛,您可以使用如下内容:

<div class='main_col'> <!--Start of the Main-->
<div class='main_content'>
<?php
include("includes/connect.php");

$run_games = mysql_query("SELECT * FROM games");

echo '<table>';
$games = 0;
while($row = mysql_fetch_array($run_games)){
   // make a new row after 3 games
   if($games%3 == 0) {
      if($games > 0) {
         // and only close it if it's not the first game
         echo '</tr>';
      }
      echo '<tr>';
   }

   $game_id = $row['game_id'];
   $game_name = $row['game_name'];
   $game_category = $row['game_name'];
   $game_keywords = $row['game_name'];
   $game_image = $row['game_image'];
   ?>
   <td>
   <a href="game_page.php?id=<?php echo $game_id; ?>"><?php echo $game_name; ?></a><br />
   <a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" /></a>
   </td>
   <?php 
   $games++; // increment the $games element so we know how many games we've already processed
}
?>
</table>
</div>
</div> <!--End of the Main-->
Run Code Online (Sandbox Code Playgroud)