To highlight a line in the result of php+sql query

Asked

Viewed 209 times

2

I am developing an App that uses a page PHP where I will have a ranking of points that the user makes when performing some interaction with it. I have a script that generates the list in order (from largest to smallest) showing from first to last.

However I wish to HIGHLIGHT the user who makes the query, passed through a form ($user =$_GET['user'];), formatting your line in bold. I believe to be an IF condition but I can’t get to the problem resolution.

Quote my current code, working perfectly, but without highlighting the user who makes the query.

  $usuario =$_GET['usuario'];

   $conexao = mysqli_connect('XXXXXXX','XXXXXX','XXXXXXX');
  mysqli_select_db($conexao,'XXXXXX');

 $sql="select * from login order by pontos DESC";

  $resultado = mysqli_query($conexao,$sql) or die ("Erro: " . mysqli_error());

$posicao = 1; //variavel

echo  <table width='90%' style='padding:10px;'><tr><td width='20%'><b>Class.</td><td width='60%'><b>Participante</td><td width='20%'><b>Pontos</td>";

//faz um looping e cria um array com os campos da consulta
while($array = mysqli_fetch_object($resultado)) {

echo "<tr><td>";
     echo $posicao;   // COLUNA DA POSIÇÃO NO RANKING

echo "</td><td>";

    echo $array->usuario; // AQUI EU QUERO UM IF PARA DIFERENCIAR O USUÁRIO PASSADO LÁ EM CIMA.

echo "</td><td>";
echo $array->pontos; // coluna dos pontos
echo "</td></tr>";
$posicao = $posicao + 1; // acumula próxima posição até terminar
} 

echo "</table>"
  • Hello, welcome to Stack Overflow in English if you haven’t done a community tour, access here, and learn how to elaborate your questions and answers. By the way, your code is with some quote errors... probably this could be causing your script errors.

  • Hello Ivan, thank you for your support. As I commented in the Post. The code DOES NOT contain errors, it is working perfectly, I just want to ADD this functionality to it. The possibility to, when making the loop, identify the user in the BD and check if it is the same $user passed through the $_GET.

  • 1

    @Leomanalvesmoitinho As you are new here, I suggest you see [Ask] and also tell you that it is not a forum, so you do not have to edit the question with "solved" in the title, but only mark the correct answer. This site is a Q&A. Welcome.

  • Thank you for your guidance. I’m new because I just registered, but I use as a learning (as well as thousands of other people) all the questions and answers from the site. I recommend in trainings that I give to students to research in their vast library of knowledge and share the results.

2 answers

2

Only as a complement, an alternative with ternary operator:

echo '</td><td>';
echo $array->usuario == $usuario ? '<strong>'.$usuario.'</strong>' : $usuario;
echo '</td><td>';

Don’t forget to use htmlentities($usuario) in the echo, where special characters such as < > & etc in the name.

  • Hello Bacco. How would this use of htmlentities, just by this code before the $user each time it is entered into the code ? Grateful.

  • @Leomanalvesmoitinho only when sending to screen. Example in the case above: $array->usuario == $usuario ? '<strong>'.htmlentities( $usuario ).'</strong>' : htmlentities( $usuario ); - Note that we only put in what is written on the screen. In comparison and other parts of the code no. htmlentities() is only to generate the correct HTML.

1


if($array->usuario == $usuario){
   echo '<strong>'.$usuario.'</strong>';
}else
   echo $usuario;
}
  • Raylan, thank you for your support. I had already tried this way and it didn’t work. There was no attempt to correct syntax. In your code just opened the key after Else. Thank you. You can test the code and/or address flaws: http://festivaldeaplicativos.com/churralcool/mostrapontosposi.php?usuario=Nina

Browser other questions tagged

You are not signed in. Login or sign up in order to post.