Sort table by name

Asked

Viewed 571 times

1

I have a table, which shows all the data from the Sqlite database. I wanted to sort by name all the data and present them. I have a hyperlink that calls a function to sort. The problem is it doesn’t work. How can I present it in the table, on the same page.

$base_hndl  =   new SQLite3($dir.$base);
$requete    =   "SELECT * FROM contact ORDER BY id desc";   
$resultat   =   $base_hndl->query($requete);    // 
$affiche    =   $resultat->fetchArray();// 
$nombreid = $affiche['id'];

function order($base, $dir,$lib_module,$nombreid){

    for($i=1;$i<=$nombreid;$i++)
    {       
        $base_hndl  =   new SQLite3($dir.$base);
        $requete    =   "SELECT prenom FROM $lib_module ORDER BY prenom ASC ";  
        $resultat   =   $base_hndl->query($requete);    // 
        $affiche    =   $resultat->fetchArray();// 

        echo $affiche['prenom'];
    }
}   

if (isset($_GET['ordername'])) {
    order($base,$dir,$lib_module,$nombreid);
}

And here the table, sorted by ID.

echo "<table border=0>\n";      
    echo "<tr align=center>\n";


    echo "<td>ID</td>\n";
    echo "<td><a href=?ordername=true>Nome</a></td>\n";
    echo "<td>Ultimo Nome</td>\n";
    echo "<td></td>\n";

for($i=1;$i<=$nombreid;$i++)
    {       
        $requete    =   "SELECT * FROM contact WHERE (id=$i)";  
        $resultat   =   $base_hndl->query($requete);    // 
        $affiche    =   $resultat->fetchArray();//  tableau 'affiche'

        if($affiche['id']!=0)
        {
            //write data 
            echo "<tr class=event bgcolor=$couleur align=left style='font-size:12px;font-family:helvetica'>\n";

            echo "<td title=\"$lib_id\"><a href=_compil_vcf.php?id=$affiche[id]>$affiche[id]</a></td>\n";
            echo "<td >$affiche[nom]</td>\n";
            echo "<td >$affiche[prenom]</td>\n";
            echo "<td >$affiche[fonction]</td>\n";
        title=\"$clic_for_mail\" >$affiche[mail]</a></td>\n";
            echo "</tr>\n"; 
        }

    }//fin de for               
    echo "</table>\n";
  • 2

    which errors php returns ?

  • Do not sort me by name, I have well the order function to sort by ASC, and qd click on the hyperlink calls the function. I want you to change the table below

  • right, error in copying.

  • @akm, let me get this straight. You have an already populated table, and by clicking on a link, you want that table to be ordered. Check?

  • Exactly, I want it in order of name, and then by the other fields of the table.

1 answer

1

TL;DR

You need to rethink your implementation. It is not possible to "reorder" data already sent to the server-side client. Get informed about Ajax and customer side solutions.

This is a very common confusion. It is important to understand what happens on the client side and the server side when you click on the "sort link".

Roughly, when you click on a link a new http request is made by the browser. This request is received by your http server, which identifies that that call should be handled by a PHP interpreter. Your http server then does what is needed (e.g., through an Apache module): Calls the PHP interpreter and responds to the http call with the content generated by its code (e.g., with Markup HTML generated by PHP). Your browser then displays this new result, discarding what was on the screen earlier.

In this "default" flow there is no way PHP can manipulate anything that has already been delivered to the client (in your case, sort data from a table). There is no relationship between the variable $affiche of the second request and the body of the table that was generated in the first request (even if you used the variable $affiche in the first request to build the table).

So what are my options?

  1. Return a full page with reordered table data: PHP will generate the whole html Markup again, not just the data or the table body.
  2. Use a iframe to the table (don’t do this).
  3. Sort the data on the client itself. You can do this with Javascript.
  4. Build an Ajax stream in which:

    1. The browser sends a request in background (without discarding the current page).
    2. The server responds with the data ordered in some notation (json, xml, yaml, etc)
    3. The browser receives this response and calls a Javascript function from callback, responsible for "discarding" the data in the current table and display the data returned by the server.

Returning a new full page is a common option (although "wasting" a little band may be more feasible in some cases). Sorting data on the client itself is a viable option for small amounts of unpackaged data. Ajax is the most modern solution; although it involves several components (client, server, data transmission format, callbacks asynchronous, etc.), is not as complex as it appears.

  • 1

    Too didn’t; didn’t read?

  • 1

    Too didn’t, didn’t didn’t; Fixed!

  • How can I sort data in Javascript?

  • If you want to "nail" a look at this post from Soen. I would recommend a specialized library such as the Datatables or jqGrid

Browser other questions tagged

You are not signed in. Login or sign up in order to post.