how can I replace values brought in a query with php?

2
<php 

$connect =mysqli_connect("localhost"  ,"root" , "" , "helpdesk");
$query ="SELECT status, count(*) as number FROM ticket  GROUP BY status  ";
$results = mysqli_query($connect, $query);
$result = mysqli_query($connect,$sql);



while ($mostrar =mysqli_fetch_array($result)) {
  # code...
?>

  <tr><td><?php echo $mostrar['total'] ?></td><td><?php echo $mostrar['status'] ?></td></tr>
</tbody>
<?php }?>

I have this code that returns me well the account of the values that I have in a table and the status, however I want to change by php the fields obtained where the status is shown by others

I want to do something like

if ($mostrar['status']=1){  $mostrar['status']= "pendiente por revisar";}

but when I do, all fields fields are changed to that value

What am I doing wrong?

    
asked by Juan Ortiz 11.11.2018 в 15:19
source

2 answers

3

You are misusing the comparison operator; Remember that if you use:

  • a single sign of equal = then assign a value
  • double sign of equal == you compare a value
  • Triple equal sign === to compare if the data you are comparing are the same and similar
  • Then this line should look like this

    if ($mostrar['status']== 1)
    {  
      $mostrar['status']= "pendiente por revisar";
    }
    
        
    answered by 11.11.2018 / 15:21
    source
    1

    In this case you can avoid the if , using a ternary operator.

    <td>
        <?php echo ($mostrar['status']==1) ? "Pendiente por revisar" : $mostrar['status']; ?>
    </td>
    

    And as a suggestion, I would avoid the constant mixing of PHP / HTML code, using a concatenation variable. The result will be a less confusing code and much easier to maintain or port to another party (imagine for example a scenario where you have to return the table to a View).

    <?php 
    
        $connect =mysqli_connect("localhost"  ,"root" , "" , "helpdesk");
        $query ="SELECT status, count(*) as total FROM ticket GROUP BY status  ";
        $result = mysqli_query($connect, $query);
    
        /*Verificar si hubo resultados antes de intentar crear la tabla*/
        $html="<table>";
        $html.="<tbody>"; //Esto debería ir fuera del while
        while ($mostrar = mysqli_fetch_assoc($result)) {
            $total=$mostrar['total'];
            $status=($mostrar['status']==1) ? "Pendiente por revisar" : $mostrar['status'];
            $html.="<tr><td>$total</td><td>$status</td></tr>";
        }
        $html.="</tbody>";
        $html.="</table>";
        echo $html;
    ?>
    
      

    NOTE ON OPTIMIZATION :

         

    I used mysqli_fetch_assoc instead of mysqli_fetch_array , because the latter creates you two types of results for each column   (one as an associative array and the other as a numerical array). Since only   you are interested in values in the form of an associative array, use the   functions / methods more appropriate to what you need. You will be driving   a less heavy result, instead of 4 data per row you will manage 2. If   the query produces 1000 rows, we talk about you will be downloading to the 2000 data server less. It is always important to consider the    optimization of the code.

        
    answered by 11.11.2018 в 20:30