rinkeeh
rinkeeh

Reputation: 23

Jquery, php, mysql table sorting using tablesorter

I'm doing a very simple site on which I need to have a table sorting system, I've got the table set up, it feeds back data from MySQL table but I cannot seem to be able to sort rows, any ideas?

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="main.css"/>
<title>Assessment 2</title>
    <script type="text/javascript" src="jquery-1.11.1.min.js"></script> 
    <script type="text/javascript" src="jquery.tablesorter.js"></script> 
    <script type ="text/javascript">

        $(document).ready(function() 
            { 
                $("products").tablesorter(); 
            } 
        ); 
    </script>
</head>
<body>
    <?php
      include '\navigation\navigation.php';
    ?>

<div class="center">
    <h1>Products</h1>



    <?php
        include "connectionlocalhost.php";


        $query = "SELECT * FROM products";
        $result = mysqli_query($connection, $query) or exit ("Error $query . ".mysqli_error());

            echo "<table border='1' id='products' class='tablesorter'>
            <thead>
                <tr>
                    <th>Product Name</th>
                    <th>Product Price</th>
                    <th>Product Image</th>
                </tr>
                </thead>
                <tbody>";   

        while ($row = mysqli_fetch_assoc($result)){
            echo "<tr>";
            echo "<td>". $row['productname'] . "</td>";
            echo "<td>". $row['productprice'] . "</td>";
            echo "</td>";

            }
            mysqli_free_result($result);



        echo "</tbody></table>";

    ?>
</div>

</body>
</html>

I am aware that one of columns is empty, its just for testing purposes, I thought there was an error with mysql query.

Upvotes: 0

Views: 2013

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

You need to add the pound sign # to "products" for:

$("products").tablesorter();

in regards/reference to the table's "id"

<table border='1' id='products' class='tablesorter'>

change that to:

$("#products").tablesorter(); 

Upvotes: 2

Related Questions