Sort an array by column in PHP

j-ppduweb -  
 j-ppduweb -
Hello,

I’m in the process of creating a website and I have a small issue.
I would like to create a sortable table by column, so that when the user clicks on the header of a column it sorts by that column.
I searched the net but I can’t solve this problem, I would need an opinion from a more experienced developer to tell me what’s wrong in my code.
 $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; $bdd = new PDO('mysql:host=localhost;dbname=stats', 'root', '', $pdo_options); // Sort by column $tri_autorises = array('ID','Noms','Nation','Arrivee','Depart'); $order_by = in_array($_GET['order'],$tri_autorises) ? $_GET['order'] : 'ID'; // Sort direction $order_dir = isset($_GET['inverse']) ? 'DESC' : 'ASC'; //retrieve contents $result = $bdd->query('SELECT * FROM entraineurs'); // function that displays the links function sort_link($text, $order=false) { global $order_by, $order_dir; if(!$order) $order = $text; $link = '<a href="?order=' . $order; if($order_by==$order && $order_dir=='ASC') $link .= '&inverse=true'; $link .= '"'; if($order_by==$order && $order_dir=='ASC') $link .= ' class="order_asc"'; elseif($order_by==$order && $order_dir=='DESC') $link .= ' class="order_desc"'; $link .= '>' . $text . '</a>'; return $link; } // Display ?> <table> <tr> <th><?php echo sort_link('ID', 'ID') ?></th> <th><?php echo sort_link('Noms', 'Noms') ?></th> <th><?php echo sort_link('Nation', 'Nation') ?></th> <th><?php echo sort_link('Arrivee', 'Arrivee') ?></th> <th><?php echo sort_link('Depart', 'Depart') ?></th> </tr> <?php while( $row=fetch_assoc($result) ) //le probleme viendrait de cette ligne ? ?> <tr> <td><?php echo $row['ID'] ?></td> <td><?php echo $row['Noms'] ?></td> <td><?php echo $row['Nation'] ?></td> <td><?php echo $row['Arrivee'] ?></td> <td><?php echo $row['Depart'] ?></td> </tr> <?php endwhile ?> </table> 


C’est un code récupéré en partie sur le net, je l’ai peut être mal adapté mais j’ai passé plusieurs heures à essayer de solutionner ce problème... en vain, et je commence à ne plus avoir les idées très claires...

Merci d’apporter votre aide à un jeune débutant qui a envie d’apprendre. :)

4 answers

  1. Fallentree Posted messages 2445 Status Member 210
     
    We really don’t know where you’re coming from?
    You have a DB with data and this code that displays a table.
    Already in this code
    // Sort on column $tri_autorises = array('ID','Noms','Nation','Arrivee','Depart'); $order_by = in_array($_GET['order'],$tri_autorises) ? $_GET['order'] : 'ID';

    We need to add a test for the existence of $_GET to be able to display it once.
    That should be it
    0
  2. le père
     
    Hello

    In your query you do not take into account the order you determined beforehand. You must incorporate it:
     $result = $bdd->query('SELECT * FROM entraineurs ORDER BY '.$order_by. ' '.$order_dir); 
    0
    1. j-ppduweb
       
      exact, but it’s just a forgetfulness when I pasted the code, because even with this line it doesn’t work.

      thanks
      0
    2. j-ppduweb
       
      Fatal error: Appel à une fonction inexistante fetch_assoc() dans C:\wamp\www\stats\entest.php à la ligne 80
      Pile d'appels
      # Temps Mémoire Fonction Emplacement
      1 0.0009 383616 {main}( ) ..\entest.php:0
      0
  3. j-ppduweb
     
    Actually I think I’m not able to use the fetch_assoc() function correctly but I can’t find the solution.
    I’m not even sure the problem comes from the...
    0
  4. j-ppduweb
     
    solved. the problem was that I was using a PDO and a mysqli function.
    in fact it was enough to just do $row = $result->fetch()
    0