0

I have a MySQL query I'm trying to sort dynamically when a link is clicked.

My link looks like this

                 <form action="topics.php" method="get" class="form">
                   <label class="label">Order Table By</label>
                    <li><a href="topics.php?sort=ID">ID</a></li>
                    <li><a href="topics.php?sort=Title">Title</a></li>
                    <li><a href="topics.php?sort=TAGS">Tags</a></li>
                    <li><a href="topics.php?sort=VIEWS">Views</a></li>
                 </form>

MySQL Query looks like this:

<?php

        $order = mysql_real_escape_string($_GET['sort']);          
$topics = mysql_query("SELECT topic_id AS 'ID', topic_head AS 'Title', 
                              topic_tags AS 'TAGS', topic_views AS 'VIEWS',
                       FROM forum_topics 
                       WHERE topic_id > 0 ORDER BY '$order' DESC") or die (mysql_error());

When I click any of the links above nothing happens. No error is shown, no sorting is done. Where could the problem be ? Thanks

5 Answers 5

1

Try this:

<?php
  $sortParams = ('ID','Title','Tags','Views');
  $order = mysql_real_escape_string($_GET['sort']);
  if(!in_arrray($order,$sortParams))
  {
    $order = 'ID';//Assuming by default you sorting by ID
  }  
  $topics = mysql_query("SELECT topic_id AS 'ID', topic_head AS 'Title', 
        topic_tags AS 'TAGS', topic_views AS 'VIEWS',
        FROM forum_topics 
        WHERE topic_id > 0 ORDER BY $order DESC") or die (mysql_error());
Sign up to request clarification or add additional context in comments.

Comments

1

You are sorting by $_SESSION['order'] and not by $order... I would suggest replacing $_SESSION['order'] with $order or do $_SESSION['order'] = $order before you create your MySQL query.

1 Comment

Thanks for the answer. I have removed the $_SESSION['order'], but still not working
1
error_reporting(E_ALL);

Add that to your code. http://php.net/manual/en/function.error-reporting.php

If you have a closer look at your code, you can see that you are using $_SESSION[order] instead of $_SESSION['order'] notice the quotes

The true problem is:

At line 1 you defined $order, and not $_SESSION['order'].

My tip is: get a proper IDE, a proper IDE like PhpStorm or Netbeans will highlight a lot of mistakes you made like typos and unused variables. Can become handy when you have problems remembering your variables ;)

Comments

0

$_SESSION[order] is another variable other than $order. They are different, so that what you have done would not order anything unless you replace your $_SESSION[order] with the variable $order or they share the same value.

4 Comments

I removed $_SESSION['order'] and replaced with $order, but still not working.
@DotOyes, please add it as an update instead of an edit in the original post. For new visitors of this post, a lot of the answers look useless, but they DID help you. So please: ADD instead of EDIT to your original post ;)
Assuming that your query is: $topics = mysql_query("SELECT topic_id AS 'ID', topic_head AS 'Title', topic_tags AS 'TAGS', topic_views AS 'VIEWS', FROM forum_topics WHERE topic_id > 0 ORDER BY '".$order."' DESC") or die (mysql_error());
If it still does not work, check your return value. For example, topic_id will return as Integer, and others will return as string values. Then using proper quotes at your $order variable.
0

The problem is that you have quotes around $order, so it's sorting by a literal string, not a column. Change them to backquotes.

 WHERE topic_id > 0 ORDER BY `$order` DESC"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.