good day can anyone help me with my sort by? nothing happens when clicking the sort no errors tried the codes 4 years ago don't know if it's still usable the sql.= i think don't work or my other query just disables it. here's my sql command thanks :D. help with the order by.

//table
   <table>
            <tr>
                <th>
                    <a href="?orderBy=date">Type:</a>
                </th>
                <th>
                    <a href="?orderBy=FirstName">Description:</a>
                </th>
                <th>
                    <a href="?orderBy=Lastname">Recorded Date:</a>
                </th>
                <th>
                    <a href="?orderBy=Date">Added Date:</a>
                </th>
            </tr>
        </table>

and here's my php code same page with the links

<?php

 $sort=$_GET['orderBy'];



 $query=mysql_query("select * from persons  order by '$sort' desc LIMIT $start,$per_page ");   
 $count=mysql_query("select * from persons where firstname like '%$searchtext%' or lastname like ' %$searchtext%' order by '$sort'");   

 $numrows = mysql_num_rows($count);

sort don't work outputs nothing example public_search.php?orderBy=FirstName

update when i use the query of :

 select * from persons where firstname like '%$searchtext%' or lastname like '%$searchtext%' ORDER BY status    DESC LIMIT $start,$per_page

it works so i conclude that it don't get the sort command

Put this temporary debug code on line 2:

die($_GET['orderBy']);

It should print the sort condition and stop the script. Check it whether is OK or post it here.

Just a thought: maybe you should add a script name to your links:

 <a href="<?php echo __FILE__; ?>?orderBy=date">Type:</a>

sir got an error

Notice: Undefined index: orderBy in C:\xampp\htdocs\thesis\Capstone\public_search.php on line 113

Notice: Undefined index: orderBy in C:\xampp\htdocs\thesis\Capstone\public_search.php on line 114

updated here's my whole code for the query

<form action="#" method="set">

<th><a href="public_search.php?sort=type" name="Date">Type:</a></th>
<th><a href="public_search.php?sort=desc" name="Firstname">Description:</a></th>
<th><a href="public_search.php?sort=recorded" name="Lastname">Recorded Date:</a></th>
<th><a href="public_search.php?sort=added" name="status">Added Date:</a></th>
 <a href="<?php echo __FILE__; ?>?sort=date">Type:</a>
</form>



<?php



 $sort=$_GET['orderBy'];



$quiz_name = $_GET['sort'];


$searchtext = '';

if(isset($_GET['q'])) $searchtext = mysql_real_escape_string($_GET['q']);
if($searchtext) {

  $per_page =5;
                        $pages_query = mysql_query("SELECT COUNT('PersonID') FROM persons where firstname like '%$searchtext%' or lastname like '%$searchtext%' ");
                        $pages = ceil(mysql_result($pages_query,0) / $per_page);

                        $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
                        $start = ($page - 1) * $per_page;


// And set the first page
$first_page = "1";

    $num = mysql_num_rows($pages_query);
    $last_page = ($num / $per_page);
    $next_page = $page + 1;
    $last_page=$pages;



   $query=mysql_query("select * from persons where firstname like '%$searchtext%' or lastname like '%$searchtext%' ORDER BY  '$quiz_name' ASC  LIMIT $start,$per_page ");
   $count=mysql_query("select * from persons where firstname like '%$searchtext%' or lastname like '%$searchtext%'");
   $numrows = mysql_num_rows($count);


}

else 
{

   $per_page =5;
                        $pages_query = mysql_query("SELECT COUNT('PersonID') FROM persons ");
                        $pages = ceil(mysql_result($pages_query,0) / $per_page);

                        $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
                        $start = ($page - 1) * $per_page;
                        $last_page=$pages;

// And set the first page
$first_page = "1";


   $query=mysql_query("select * from persons order by '$quiz_name' desc LIMIT $start,$per_page ");
   $count=mysql_query("select * from persons where firstname like '%$searchtext%' or lastname like '%$searchtext%'");
   $numrows = mysql_num_rows($count);

}

i guest that the query got messy because it got many queries

Line 1 contains an error:

<form action="#" method="set">

It should be:

<form action="#" method="get">

But the main problem is elsewhere. See my next post (I am working on it).

Firstly, you do not need a form at all if you use links with querystring appended (the links will populate the $_GET array on click).

Secondly: when you first load the script the $_GET array will be empty since no link has been clicked yet. $_GET will get populated only once you click one of the links. So you have to wrap the code with the sorting queries in an if block:

if(isset($_GET['orderBy']) && !empty($_GET['sort'])) {

    $quiz_name = $_GET['sort'];
    ...


}

The same goes for $sort=$_GET['orderBy'].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.