Highlight the first record brought from a Mysql query with DESC


I do not know if the title is descriptive enough, but I did not know how to ask the question. The problem is this:

I have a query that I use to show a "top" of the 20 ages of some people. What I want is that the first record shown, in this case the one with the highest age, is highlighted in some way to show that it is the first. Maybe there is some way to select it from php and then apply the required css. Here is the code:

$sel = mysqli_query($con,"SELECT * FROM personas ORDER BY edad DESC LIMIT 0,20");
foreach ($sel as $datos) {
echo $datos['nombre']."<br>";
asked by Jalkhov 29.04.2018 в 03:12

2 answers


The simplest way to do it with a classic foreach for queries in PHP would be to declare a flag variable to know when it is the first record, in the first iteration we already modify the flag to false and we can print the values with the normal format. (for the example I use object oriented)

$conn = new mysqli("localhost", "root", "mipassword", "midb");
$sel = $conn->query("SELECT * FROM personas ORDER BY edad DESC LIMIT 0,20");
$primerRegistro= true;
foreach ($sel as $datos) {
    echo "<strong>". $datos['nombre']."</strong><br>";
    $primerRegistro= false;//Nos aseguramos que solo se ejecute una vez
    echo $datos['nombre']."<br>";

The other option would be a% classic% co_ and access the first element of    for array of data returned by the query.

Another option to avoid checking a [0] within a loop in each iteration, would be to obtain the data row by row with the typical method if

$sel = $conn->query("SELECT * FROM personas ORDER BY edad DESC LIMIT 0,20");
//Obtenemos el total de las filas devueltas por la consulta
$total= $sel->num_rows;
  //Obtenemos el primer elemento e imprimimos en negrita
  $data = $sel->fetch_array();
  echo "<strong>". $data['nombre']."</strong><br>";
  // Si hubiesen más entra al while si no , no 
  while($data = $sel->fetch_array())
    //Impresión Normal
     echo $data['nombre']."<br>";
  echo "No Hay Resultados";

Leaving aside the treatment of the first element from fetch_array , and if you only want to style the first element, we could simply focus on PHP rules, including pseudo-classes like CSS .


$sel = $conn->query("SELECT * FROM personas ORDER BY edad DESC LIMIT 0,20");
echo "<ol class='misdatos'>";
foreach ($sel as $datos) {
  echo "<li>".$datos['nombre']."</li>";
echo "</ol>";

you will get a list with the class :first-child , this would be the selector for the .misdatos getting the first element CSS that you find it will apply the color red. (for the example, you decide what rules apply)

.misdatos li:first-child{
  color : red;
answered by 29.04.2018 / 03:27

Just ask in an if it is the first record in the array

for($i=0; $i<count($sel); $i++) {
   if($i==0) {
      //Imprimir destacado EJ: <span class="destacado">$datos['nombre']</span>
      //<i >$datos['nombre']</i>
      echo '<strong>'.$datos['nombre'].'</strong><br>';
   } else {
      echo $datos['nombre'].'<br>';     
answered by 29.04.2018 в 03:26