所以我试图找出如何获取我的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-->
我试过了:
<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-->
建议任何人? :(
答案 0 :(得分:3)
首先,你应该真的不再使用不赞成的 mysql扩展。相反,您应该使用MySQLi或PDO。
接下来,你想要一个包含3列的表。每个数据字段应包含3个游戏。所以这意味着在每3场比赛之后,你想要在每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-->
或者,如果你只是想要每场1场比赛,你可以使用这样的东西:
<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-->