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) {
if($primerRegistro){
echo "<strong>". $datos['nombre']."</strong><br>";
$primerRegistro= false;//Nos aseguramos que solo se ejecute una vez
}
else{
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;
if($total>=1){
//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>";
}
}else{
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
.
PHP
$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)
<style>
.misdatos li:first-child{
color : red;
}
</style>