Sort table by name

1

I have a table, which shows all data in 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 that it does not work. How can I make it to 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";
    
asked by anonymous 01.12.2014 / 13:12

1 answer

1
  

TL; DR

     

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

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

Roughly, the more 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 call to be handled by a PHP interpreter. Your http server then does what it needs to do (eg through an Apache module): Calls the PHP interpreter and responds to the http call with the content generated by your code (eg, 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 this case, sort data from a table). There is no relation between the $affiche variable of the second request and the table body that was generated on the first request (even if you used the $affiche variable in the first request to construct the table).

So what are my options?

  • Return a full page with the reordered table data: PHP will generate all the html markup again, not just the data or the body of the table.
  • Use iframe for the table (do not do this).
  • Sort the data on the client itself. You can do this with JavaScript.
  • Build an Ajax flow where:

  • The browser sends a request to background (without discarding the current page).
  • The server responds with the data ordered in some notation (json, xml, yaml, etc.)
  • The browser receives this response and calls a callback JavaScript function, responsible for "discarding" the data in the current table and displaying the data returned by the server.
  • Returning a whole new page is a common option (even if it "wastes" a bit of bandwidth, it may be more viable in some cases). Sorting data on the client itself is a viable option for small amounts of non-paged data. Ajax is the most modern solution; although it does involve many components (client, server, data transmission format, asynchronous callbacks, etc.), it is not as complex as it looks.

        
    01.12.2014 / 13:50