Search the Community
Showing results for tags 'alphabetical order'.
-
Can you figure this out? I've got two tables, `articles` and `categories`. Each article has been assigned to a category. I want to present all of the articles on a single page, one list per category, thus: Category Article 1 Article 2 Article 3 Article 4 Category Article 1 Article 2 Article 3 Category Article 1 Article 2 Article 3 Article 4 I want the categories to be in alphabetical order and the articles to be in alphabetical order. So far, I have only managed to get the categories in alphabetical order. How can I also get the articles in alphabetical order? As always, any help will be much appreciated. Here's my code: $sql = "SELECT title, url, cat FROM `articles` LEFT JOIN `categories` ON articles.catid = categories.catid ORDER BY cat ASC"; $results = mysql_query($sql, $conn) or die(mysql_error()); if(mysql_num_rows($results)) { $last_cat = ''; while($row = mysql_fetch_assoc($results)) { if($row['cat'] != $last_cat) { if($last_cat) { echo "</ol>"; } $last_cat = $row['cat']; echo "<h3>$last_cat</h3>"; echo "<ol>"; echo "<li><a href=\"{$row['url']}\">{$row['title']}</a></li>"; } else { echo "<li><a href=\"{$row['url']}\">{$row['title']}</a></li>"; } } echo "</ol>"; } else { echo "<p>No articles found.</p>"; }
- 4 replies
-
- alphabetical order
- mysql select
-
(and 1 more)
Tagged with: