how to sort post titles under their correct category names PHP and SQL

Ryan

I have searched for about 4 hours today on how to do this. I want to pull all post titles and categories from the posts table and essentially want to list the "Funny" category and then list all post that have the funny category under that category. Right now I am getting the following: Funny -post title Funny -post title I want to output Funny -post title -post title

    <?php
$query = "SELECT post_category,post_title FROM posts ";
$select_categories_sidebars = mysqli_query($connection, $query);
?>
<h4>Blog Categories</h4>
<div class="row">
<div class="col-lg-12">
<ul class="list-group">
<?php
while($row = mysqli_fetch_assoc($select_categories_sidebars)) {
$post_title = $row['post_title'];
$post_tags = $row['post_category'];
echo "<li class='list-group-item'>$post_tags</li><ul>";
echo "<li class='list-group-item'><a href='category.php?category=$post_title'> {$post_title}</a></li></ul>";
}
?>
</ul>
Ryan

Well I basically got it working how I want I just need to format it correctly now, but it is working with some help from this question. PHP Group sql query under the same title

<?php

            $query = "SELECT post_category,post_title FROM posts ORDER BY post_tags";
            $select_categories_sidebars = mysqli_query($connection, $query);

            ?>


            <h4>Blog Categories</h4>
            <div class="row">
                <div class="col-lg-12">
                    <ul class="list-group">
                        <?php
                        $title = "";
                        while ($row = mysqli_fetch_array($select_categories_sidebars)) {
                            if ($row['post_category'] != $title) {
                                echo '<li class="list-group-item">'.$row['post_category'].'</li><br>';
                                $title = $row['post_category'];
                            }
                            echo '<li class="list-group-item">'.$row['post_title'];
                        }

                        ?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to sort post titles under their correct category names PHP and SQL

From Java

Category loop is showing all post titles (WordPress)

From Dev

Sort SQL results by column titles?

From Dev

HTML & CSS : How do I change names and job titles to be under the pictures

From Dev

Wordpress Post Category Names Link

From Dev

laravel latest post sort by category

From Dev

How to get Next/Previous Post titles under WP_Query loop?

From Dev

SQL Server How to get the number of news under each category

From Dev

SQL Server How to get the number of news under each category

From Dev

wordpress, get category names for a custom post type

From Dev

Excluding terms (category names) by ID for current post

From Dev

If there is no post in a category, how to hide it

From Dev

If there is no post in a category, how to hide it

From Dev

How to exclude a post by id from the category.php query in wordpress

From Dev

How to exclude a post by id from the category.php query in wordpress

From Dev

How To SELECT Category For Each Post Id In PHP Function

From Dev

How to group items under a category?

From Dev

How to add post into a category programmatically

From Dev

How to determine active post category?

From Dev

How can I show all data under specific category using sql query?

From Dev

How can I show all data under specific category using sql query?

From Dev

How to change tab titles in vim to user-define names?

From Dev

how to load category names in drop down list

From Dev

How to prefix parent category to child category php

From Dev

Laravel accessor to convert category ids to category titles

From Dev

How to append correct details based upon category?

From Dev

How to append correct details based upon category?

From Dev

loop php $_POST array names

From Dev

show correct category name and fix post title permalink wordpress

Related Related

  1. 1

    how to sort post titles under their correct category names PHP and SQL

  2. 2

    Category loop is showing all post titles (WordPress)

  3. 3

    Sort SQL results by column titles?

  4. 4

    HTML & CSS : How do I change names and job titles to be under the pictures

  5. 5

    Wordpress Post Category Names Link

  6. 6

    laravel latest post sort by category

  7. 7

    How to get Next/Previous Post titles under WP_Query loop?

  8. 8

    SQL Server How to get the number of news under each category

  9. 9

    SQL Server How to get the number of news under each category

  10. 10

    wordpress, get category names for a custom post type

  11. 11

    Excluding terms (category names) by ID for current post

  12. 12

    If there is no post in a category, how to hide it

  13. 13

    If there is no post in a category, how to hide it

  14. 14

    How to exclude a post by id from the category.php query in wordpress

  15. 15

    How to exclude a post by id from the category.php query in wordpress

  16. 16

    How To SELECT Category For Each Post Id In PHP Function

  17. 17

    How to group items under a category?

  18. 18

    How to add post into a category programmatically

  19. 19

    How to determine active post category?

  20. 20

    How can I show all data under specific category using sql query?

  21. 21

    How can I show all data under specific category using sql query?

  22. 22

    How to change tab titles in vim to user-define names?

  23. 23

    how to load category names in drop down list

  24. 24

    How to prefix parent category to child category php

  25. 25

    Laravel accessor to convert category ids to category titles

  26. 26

    How to append correct details based upon category?

  27. 27

    How to append correct details based upon category?

  28. 28

    loop php $_POST array names

  29. 29

    show correct category name and fix post title permalink wordpress

HotTag

Archive