Criar uma área de grelha para uma pesquisa em PHP
Então eu tentei descobrir como pegar minha consulta PHP e mostrá-los em uma área de grade. Eu quero torná-lo uma grade de 3 colunas para que se pareça com isso: primeira coluna: 1, 2, 3 segunda coluna: 4, 5, 6 e assim por diante...
Aqui está o meu código para ver a consulta MySQL via PHP:
<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-->
eu tentei isto:
<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-->
Alguém quer sugestões? :(
1 answers
Em primeiro lugar, você deve realmente mudar de usar a obsoleta extensão mysql. Em vez disso, deve utilizarMySQLi ouDOP .
A seguir, o que você quer é uma tabela que contém 3 colunas. Cada campo de dados deve conter 3 jogos. Isso significa que depois de cada 3 jogos, você quer fazer a e depois de cada 9 jogos, iniciar uma nova linha. Você pode usar o modulo-operador ( % ) para ver quando você teve 3 (ou 6 ou 9, etc.) jogos, e quando você teve 9/18/27 / etc. jogos assim:
<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-->
Em alternativa, se quiser apenas 1 jogo por campo, pode usar algo do género:
<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-->