davidgpilot
davidgpilot

Reputation: 69

Create a grid view for a PHP query

So I tried to figure out how to take my PHP query and display them in a grid view. I want to make it a grid of 3 columns so it looks like this: First column: 1, 2, 3 Second column: 4, 5, 6 and so on...

Here is my code to view MySQL query 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-->

I tried this:

<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-->

Suggestions anybody? :(

Upvotes: 1

Views: 12082

Answers (1)

Tularis
Tularis

Reputation: 1506

First of all, you should really switch away from using the deprecated mysql extension. Instead you should either use MySQLi or PDO.

Next, what you want it a table which contains 3 columns. Each data field should contain 3 games. So that means that after every 3 games, you want to make a and after every 9 games, start a new row. You can use the modulo-operator (%) to see when you have had 3 (or 6 or 9, etc.) games, and when you have had 9/18/27/etc. games, like so:

<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-->

Alternatively, if you simply want 1 game per field, you can use something like this:

<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-->

Upvotes: 5

Related Questions