Steve
Steve

Reputation: 1

How to order a column alphabetically in table using links and Get using PHP,MySQL?

I have a table. In the table, there is the headings customerID, name, city, address. They are in the top of of the table and they are links to the same page.

What I want to do is that, if I press Name in the table, it will put all the names in the column in order; same with customerID, if I press the link it will order all the numbers in the column.

Here I have

if ($_POST['sort'] == 'customerid') {
    $query .= " ORDER BY customerid";
} elseif ($_POST['sort'] == 'name') {
    $query .= " ORDER BY name";
} elseif ($_POST['sort'] == 'city') {
    $query .= " ORDER BY city";
} elseif ($_POST['sort'] == 'address') {
    $query .= " ORDER BY address";
}

But am not sure where its meant to go in the code so any help would be greatly appreciated

I was hoping you guys would help.

   <html>
    <head>
    </head>
    <body>
        <?php
        $options = array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
        );
        $dsn = 'mysql:host=localhost;dbname=agmarcel';
        $password = 'asdf';
        $username = 'agmarcel';
        $db = new PDO($dsn, $username, $password, $options);

        $query = "SELECT";

        if ($_POST['sort'] == 'customerid') {
            $query .= " ORDER BY customerid";
        } elseif ($_POST['sort'] == 'name') {
            $query .= " ORDER BY name";
        } elseif ($_POST['sort'] == 'city') {
            $query .= " ORDER BY city";
        } elseif ($_POST['sort'] == 'address') {
            $query .= " ORDER BY address";
        }
        $stmt = $db->prepare($query);
        $stmt->execute();

        echo "<table border=2>";
        echo "<tr>";
        echo "<th><a href="mypage.php?sort = customerid">Customer ID:</a></th>";
        echo "<th><a href="mypage.php?sort = name">Name:</a></th>";
        echo "<th><a href="mypage.php?sort = city">City:</a></th>";
        echo "<th><a href="mypage.php?sort = address">Address:</a></th>";
        echo "</tr>";

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo "<tr>";
            $customerid = $row['customerid'];
            $name = $row['name'];
            $city = $row['city'];
            $address = $row['address'];


            echo "<td>Customer ID: $customerid</td>";
            echo "<td>$name</td>";
            echo "<td>$city</td>";
            echo "<td>$address</td>";
            echo "</tr>";
        }
        echo "</table>";
        ?>      
    </body>  
</html>

Upvotes: 0

Views: 75

Answers (1)

chandresh_cool
chandresh_cool

Reputation: 11830

Change your $query = "SELECT"

to

    $query  = "SELECT * from your tablename";



    if ($_POST['sort'] == 'customerid')
    {
        $query .= " ORDER BY customerid";
    }
    elseif ($_POST['sort'] == 'name')
    {
        $query .= " ORDER BY name";
    }
    elseif ($_POST['sort'] == 'city')
    {
        $query .= " ORDER BY city";
    }
    elseif($_POST['sort'] == 'address')
    {
        $query .= " ORDER BY address";
    }

Upvotes: 2

Related Questions