0

I want to let the user sort the table base on what the user wants. I have two options for this, sort by name or sort by exam.

code

echo "<table border=1 align=center><tr class=style2><td><input type=radio name=sort value='byname'>Sort By Name<td><input type=radio name=sort value='byexam'>Sort By Exam";

$sort = $_POST['name'];
if ($sort == "byname"){
    $sort=mysql_query("select * from mst_adminresult order by login ASC",$cn) or die(mysql_error());

    while($row=mysql_fetch_row($sort))
    echo "<table border=1 align=center><tr class=style2><td>Student Name <td> Test<br> Question <td> Score";
    echo "<tr class=style8><td>$row[1] <td align=center> $row[2] <td align=center> $row[3]/20";
    echo "</table>";

}else{
    $sort=mysql_query("select * from mst_adminresult order by test_id ASC",$cn) or die(mysql_error());
    while($row=mysql_fetch_row($sort))
    echo "<table border=1 align=center><tr class=style2><td>Exam<td width=300>Student Name<td> Score";
    echo "<tr class=style8><td>$row[1] <td align=center> $row[2] <td align=center> $row[3]/20";
    echo "</table>";
}

echo "<table border=1 align=center><tr class=style2><td width=300>Student Name <td> Test<br> Question <td> Score";

while($row=mysql_fetch_row($rs))
{
echo "<tr class=style8><td>$row[1] <td align=center> $row[2] <td align=center> $row[3]/20";
}
echo "</table>";

the problem I am encountering is it doesn't function properly. The default arrangement of the data is listed by the latest exam taker to the last. Now what I am aiming for is, if the user checks the by the name option, it will sort by name. for the by exam, it will be listed by exam.

output: enter image description here

3
  • 3
    Note: The mysql_* functions are deprecated, they have been removed from PHP 7, your code will stop working when you upgrade to that version. You should not write new code using them, use mysqli_* or PDO instead. Commented May 18, 2016 at 8:05
  • Your select has name="sort", yet you check $_POST['name'] later. Change this to $_POST['sort']. Commented May 18, 2016 at 8:08
  • it works fine..but the layout is in mess... i'll edit the question so that everyone can see the layout. it should just be one table @GeraldSchneider Commented May 18, 2016 at 8:33

1 Answer 1

1

Your input radio button name="sort" so you need to use $_POST['sort'].

You can use input value as database field name so that we can easily use it in query without if...else condition.

echo "<table border=1 align=center><tr class=style2>
     <tr><td><input type=radio name='sort' value='byname'>Sort By Name</td>
     <td><input type=radio name='sort' value='byexam'>Sort By Exam</td></tr></table>";

$sort = isset($_POST['sort']) ? $_POST['sort'] : "test_id"; // change of $_POST['name'] to $_POST['sort']

$sortQuery = mysql_query("select * from mst_adminresult order by ".$sort." ASC",$cn) or die(mysql_error());

while($row=mysql_fetch_row($sortQuery))
{
    echo "<table border=1 align=center><tr class=style2><td>Student Name</td><td>Test<br> Question</td><td>Score</td></tr>";
    echo "<tr class=style8><td>$row[1]</td> <td align=center> $row[2]</td> <td align=center> ". ($row[3]/20) ."</td></tr> ";
    echo "</table>";
}

echo "<table border=1 align=center><tr class=style2><td width=300>Student Name</td><td> Test<br> Question </td><td> Score</td></tr>";

while($row=mysql_fetch_row($rs))
{
echo "<tr class=style8><td>$row[1]</td> <td align=center> $row[2]</td> <td align=center> ". ($row[3]/20) ."</td></tr> ";
}
echo "</table>";
Sign up to request clarification or add additional context in comments.

3 Comments

it works fine..but the layout is in mess... i'll edit the question so everyone can see... it should just be one table only
Try edited code and let me know your output. @Jennifer
And say hello to MySQL injection.. Don't use mysql_ since the functions are deprecated. And if you use the above code anyways, at least add mysql_real_escape_string to your $_POST data.

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.