0

So I'm trying to sort an SQL query based on which submit button is clicked

My Sort options:

echo "<form method='post' action adminview.php'>"
    ."<input type='submit' name='sort' value='Sort by Attempt ID'/><br />"
    ."<input type='submit' name='sort' value='Sort by Student ID'/><br />"
    ."<input type='submit' name='sort' value='Sort by lowest Score'/><br />"
    ."<input type='submit' name='sort' value='Sort by highest Score'/><br />"
    ."</form>";

And when the page reloads here is the code to check which button was clicked

switch($_POST['sort'])
{
    case "Sort by Attempt ID":
        $sortby = "attempt_id";
        break;
    case "Sort by Student ID":
        $sortby = "student_id";
        break;
    case "Sort by lowest Score":
        $sortby = "score";
        break;
    case "Sort by highest Score":
        $sortby = "score desc";
        break;
}

My issue is that when the page reloads it says that sort is undefined.

If you were curious here is the SQL statement

$sqlstring = "select * from quizattempts order by '$sortby'";
3
  • before switch add echo '<pre>';print_r($_POST); - this will give you clue Commented Nov 3, 2013 at 5:51
  • You should have a default: in your switch case. Commented Nov 3, 2013 at 7:26
  • $sqlstring = "select * from quizattempts order by '" . $sortby. "' "; would have probably worked. Commented Nov 3, 2013 at 15:44

3 Answers 3

2

The single quotes are preventing $sortby from being evaluated. Just remove them:

$sqlstring = "select * from quizattempts order by $sortby";
Sign up to request clarification or add additional context in comments.

2 Comments

this worked even when it is more than one work, maybe it is because my query is simple, I'm not sure but thank you so much Mureinik!!
@BrendonRother This actually worked because there is no multi-word attribute but you may sometimes have multi-word attributes in the database. In that case it is bound to fail. Check my update.
0

Can you please try this,

<?php 
      echo "<form method='post' action='adminview.php'>"
           ."<input type='submit' name='sort' value='Sort by Attempt ID'/><br />"
           ."<input type='submit' name='sort' value='Sort by Student ID'/><br />"
           ."<input type='submit' name='sort' value='Sort by lowest Score'/><br />"
           ."<input type='submit' name='sort' value='Sort by highest Score'/><br />"
          ."</form>";

        if(isset($_POST['sort'])){

            switch($_POST['sort'])
            {
                case "Sort by Attempt ID":
                    $sortby = "attempt_id";
                    break;
                case "Sort by Student ID":
                    $sortby = "student_id";
                    break;
                case "Sort by lowest Score":
                    $sortby = "score";
                    break;
                case "Sort by highest Score":
                    $sortby = "score desc";
                    break;
            }

            $OrderBy = "order by $sortby ";
        }

         echo $sqlstring = "select * from quizattempts $OrderBy";

Comments

0

instead of single quote(') you need to use grave accent(`).May be in this case you don't need it because it is certain that the attributes are of single word. But when you are not sure whether the attributes are of multiple words or single word you should always use the grave accent.

Syntax

select * from tab_name order by `attribute name` [ordering=asc/desc];

So use the following:

$ordering="asc";
switch($_POST['sort'])
{
    case "Sort by Attempt ID":
        $sortby = "attempt_id";
        break;
    case "Sort by Student ID":
        $sortby = "student_id";
        break;
    case "Sort by lowest Score":
        $sortby = "score";
        break;
    case "Sort by highest Score":
        $sortby = "score";
        $ordering="desc";
        break;
}
$sqlstring = "select * from quizattempts order by `$sortby` $ordering";

Here I have specified the ordering in a separate variable to keep it out of ambiguity.

2 Comments

you solution did work for everything except when $sortby = score desc. However the first answer given by Mureinik worked
@BrendonRother Sorry for that but in case of $sortby = "score desc" score desc is not an attribute name and hence didn't work. I specified the use of grave accent(`) to show you the ideal way to use order by with any type of attribute name(multiword or single-word). But according to the particular state of your code i.e. you merged the attribute name with the ordering sequence. That's I think simple but not a smart way to do it in a global approach. Updating my answer. Check it if it solves your issue.

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.