0

I'm trying to re-order a HTML table when the user clicks on the table header. Here is all I can do, but still doesn't work.

in the HTML:

// onClick on table header 
var par='ASC';
sort(par);

from: ajax.js

function sort(orderByType)
{
  $.ajax({
    url: "sort.php",
    type: "get",
    data: "orderby="+orderByType,
    success: function(data){
      alert(data);
      $("t1").text(data);
    }
  });
}

sort.php

$con = mysql_connect("localhost","root","root");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

$orderBy = $_GET['orderby'];

mysql_select_db("icrisis", $con);

$result = mysql_query("SELECT * FROM messages,user  
                                        where user_id = from_user
                                        ORDER BY user_name".$orderBy);

while($row = mysql_fetch_array($result))
{      
  echo "<tbody><tr><td>"."&#8226;"."</td><td>".
       $row["Time_Date"]."</td><td>".
       $row["player_name"]."</td><td></td><td></td><tr><td></td><td colspan='4'>".
       $row["msg_desc"]."</td></tr></tbody>";
}

mysql_close($con);

It doesn't see the $orderBy. And then I want to add the new order to my page - how could that be done?

Could the variable sent to function sort be dynamic, i.e. to be ASC or DESC on click?

2
  • Hi, Sarah. Please, use the preview below the form when asking questions to make sure it's formatted correctly before submitting. To tell the form that what you're entering code, use four leading spaces. Commented Apr 15, 2009 at 9:06
  • The answer of MrHus is correct but I think you are doing something wrong. Have you put a space between the field name and the sort direction or is it just like you wrote here? Commented Apr 15, 2009 at 9:19

3 Answers 3

5

You should try: tablesorter its for sorting tables. And you don't even need to use php with this solution just jquery. Hope its usefull.

To reply to your comment on Daan's anwser you could update tablesorter with ajax as described here.

Sign up to request clarification or add additional context in comments.

1 Comment

While this is true, sorting using the tablesorter plugin is almost unusable for larger tables. Just a heads up for Sarah!
1

I'm not sure if that is the cause of your problem, but I believe you miss a space.

The last line of your query is now:

ORDER BY user_name".$orderBy);

But should be:

ORDER BY user_name ".$orderBy);

1 Comment

Thank you so much for your concern MrHus,skorpan and Daan I can't use the tablesorter because i add 2 rows by ajax as for the space i added it and it worked never thoight that could be a problem now i want to update contents of HTML table by jquery how could i do that
0

As Daan said you are missing the space and that is probably the cause, however I will add:

You should probably check the result of the mysql_query() call because currently you don't know whether that is failing or not, which could be the cause of your problem. For example:

$result = mysql_query('SELECT ...');
if (!$result) {
    die(mysql_error());
}

while($row = mysql_fetch_array($result)) {
//etc.

Also you shouldn't really be building the SQL statement based on strings that have come from the browser, as these cannot be trusted, and someone could currently add malicious SQL into what you are executing. See SQL Injection.

You could validate the string first which would be far safer, and is easy because you only have two possible values, e.g.

if (isset($_GET['orderby']) && $_GET['orderby'] == 'DESC') {
    $orderBy = 'DESC';
} else {
    $orderBy = 'ASC';
}

1 Comment

Hey tomhaigh Thank you so much for your concern you all seem to know your way around web coding now i need help i am still newbie and i want someone that i can discuss with and ask what can i do and what to do Also i want to update the content of the HTML table by jquery what should i do

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.