Reputation: 27
I am looking to list items (or in this case game titles) in alphabetical order.
using php I am getting games from a mysql databse, i use foreach to easly list all games, but i need it to list them in alphabetical order.
games.php
<table style="width:100%;" class="hover">
<th style="width:5%;">A-Z</th>
<th style="width:70%;">Games</th>
<th style="width:10%;">Clans</th>
<th style="width:10%;">Recruiting</th>
<tr>
<td></td>
<td>None Specified</td>
<td></td>
<td></td>
</tr>
<?php
$query = "
SELECT
az,
games,
clans,
recruiting
FROM games
";
try
{
// These two statements run the query against your database table.
$stmt = $db->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code.
die("Failed to run query: " . $ex->getMessage());
}
// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
?>
<?php foreach($rows as $row): ?>
<tr>
<td><?php echo $row['az']; ?></td>
<td><a href="/games/"><?php echo $row['games']; ?></a></td>
<td><?php echo $row['clans']; ?></td>
<td><?php echo $row['recruiting']; ?></td>
</tr>
<?php endforeach; ?>
<tr>
<th style="width:5%;">A-Z</th>
<th style="width:70%;">Games</th>
<th style="width:10%;">Clans</th>
<th style="width:10%;">Recruiting</th>
</tr>
</table>
Upvotes: 0
Views: 236
Reputation: 8593
You need to update your MySQL to
SELECT
az,
games,
clans,
recruiting
FROM games
ORDER BY games ASC
Upvotes: 0
Reputation: 238
Just put a ORDER in your query:
$query = "
SELECT
az,
games,
clans,
recruiting
FROM `games`
ORDER BY games ASC
";
Upvotes: 1