Friday, April 29, 2011

Sort a mysql table using php,ajax and jquery

Hello Everyone Ok here is the deal i am building a messaging system by php,ajax,jquery and mysql i add the new message and i add in 2 rows now what i want to do is to send an ajax request to server and recieve an updated version of the table but sorted So how can i do so

//here i add and call the message the message

function addMsg()
 {
      //alert(post);
  $.get("db.php",$('form').serialize(),
     function(data){
   getMsg(data);},"html");
}

function getMsg(data) { alert(data); $("#t1").append(data); } //php

 $sql="INSERT INTO -----

mysql_query($sql) or die(mysql_error()); 


$result = mysql_query("SELECT ----");

    while($row = mysql_fetch_assoc($result))
   {     
   echo "<tbody><tr><td>".""."</td><td>".$row["col"]."</td><td>".$row["col"]."</td><td></td><td></td><tr><td></td><td colspan='4'>".$row["col"]."</td></tr></tbody>"; 
   }

?>

//Now for sorting //javascript

function sort()
 {
  $.ajax({
    url: "sort.php",
    dataType: "text",
    success: function(data){
    $("t1").text(data);
        }

  });

 }

//php

$result = mysql_query("SELECT * FROM table ORDER BY col desc");

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

?>

From stackoverflow
  • Use ORDER BY when you do the SELECT. Check this: http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html

  • Ok here is all i can do but still doesn't work any ideas Come on gurus i need help

    //index.php

    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

0 comments:

Post a Comment