I would like to order an array that I create from mysqli_fetch_array

2

Greetings friends, I would like to know how to order a list that you create from a mysqli_fetch_array .

Apart from this code I have a search engine, what I want is that the results of the search appear in a specific row, for example code number or name.

<?php
require ("datos_de_acceso.php");
$conexion=mysqli_connect($db_host,$db_usuario,$db_contra);
if (mysqli_connect_errno()){
    echo "Fallo al conectar con la BBDD";
    exit();
}

mysqli_set_charset($conexion, "utf-8");
mysqli_select_db($conexion, $db_nombre) or die ("No se encuentra la base de datos");
mysqli_query($conexion, "SET NAMES 'utf8'");

$busqueda=$_GET["buscar"];

if (is_numeric($busqueda)){
 $consulta="SELECT * FROM hoja1 WHERE codigo LIKE '%$busqueda%'";
 $resultado=mysqli_query($conexion, $consulta);
}else{
 $consulta="SELECT * FROM hoja1 WHERE materia LIKE '%$busqueda%'";
 $resultado=mysqli_query($conexion, $consulta);
}

while($fila=mysqli_fetch_array($resultado, MYSQL_ASSOC)){
 echo "<table border='1px' width='51%' align='left'><tr>";
 echo "<td width='4%'>" . $fila['numero'] . "</td>";
 echo "<td width='8%'>" . $fila['codigo'] . "</td>";
 echo "<td width='30%'>" . $fila['materia'] . "</td>";
 echo "<td width='8%'>" . $fila['seccion'] . "</td>";
 echo "</tr></table>";  
}
?>
    
asked by Jesus Pinto 23.06.2016 в 00:29
source

3 answers

3

You have two options, or even combine them:

  • Use ORDER BY at the time of the query, to sort the results by the field or the fields that you want, in ascending or descending form:

    SELECT * FROM hoja1 WHERE codigo LIKE '%$busqueda%' ORDER BY codigo ASC
    

    Sort by more than one field (first by descending name and then by code):

    SELECT * FROM hoja1 WHERE codigo LIKE '%$busqueda%' ORDER BY nombre DESC, codigo ASC
    
  • Use a Javascript library (so as not to reinvent the wheel) in the frontend to sort the table at any time for the field you want, although depending on the number of records this solution may not be so interesting (in terms of performance ):

    Tablesort: link

    Datatables (requires jQuery): link

  • answered by 23.06.2016 в 03:18
    0

    Here I leave you a function to order an array based on 1 field.

    private function orderMultiDimensionalArray($toOrderArray, $field, $inverse) {
    
        $position = array();
        $newRow = array();
    
        foreach ($toOrderArray as $key => $row) {
            $position[$key] = $row[$field];
            $newRow[$key] = $row;
        }
    
        if ($inverse) {
           arsort($position);
        } else {
            asort($position);
        }
    
        $returnArray = array();
    
         foreach ($position as $key => $pos) {
             $returnArray[] = $newRow[$key];
         }
    
        return $returnArray;
     }
    

    The parameters are:

  • $ toOrderArray = Is the array with the data to be sorted.
  • $ field = It is the field for which you want to order. For example: number or code of your array.
  • $ inverse = TRUE orders ascending.
  • answered by 23.06.2016 в 02:14
    0

    Good I have a query I would like to sort my table by date but I get this syntax error:   $ sql="SELECT * FROM users WHERE user = '$ User' AND ORDER BY folio ASC";

        
    answered by 28.04.2018 в 19:25