search sql query result in php array

1

I find the following problem: I have an array called $data2 and when making a query sql the result of it can be searched in the array, I'm working with checkbox so if the result of the query sql is in the array $data2 this prints a checked . This is the code I'm working on:

while($row=mysqli_fetch_array($query)){
 // CADENA DE TEXTO A COMPARAR
 $nombre_ids = $row[2].','.$row[1].','.$row[0];
 // CICLO APRA RECORRER EL ARRAY  
 for($i=0;$i<count($data2);$i++){ 
  // COMPARAR SI EXISTE LA CADENA SQL EN EL ARRAY  
  if($data2[$i]==$nombre_ids){
   $mensaje .= '            
   <tr style="background-color:#FF7575;">
    <td>
     <input type="checkbox" class="check" name="'.$row[0].'" id="'.$nombre_ids.'" checked>  
     <label for="'.$nombre_ids.'">'.$row[2].'</label>   
    </td>
   </tr>';
  } else{
   $mensaje .= ' 
   <tr>
    <td>
     <input type="checkbox" class="check" name="'.$row[0].'" id="'.$nombre_ids.'">  
     <label for="'.$nombre_ids.'">'.$row[2].'</label>   
    </td>
   </tr>'; 
  } 
 }
}

The problem is that I duplicate the query data depending on how much data I have in the array $data2

    
asked by Agus Olivera 25.01.2018 в 00:16
source

2 answers

0

You can still simplify the code, saving yourself by at least six lines. Since the chain you concatenate in $mensaje is almost the same, you can assign to variables the values that would be different within the if , and then do the concatenation only once, when leaving the if :

while($row=mysqli_fetch_array($query)){
    $nombre_ids = $row[2].','.$row[1].','.$row[0];

    /*
        *Declaramos las variables vacías
        *sólo adquieren datos si se cumple el if
    */
    $cssFondo='';
    $attrChecked='';

    if (in_array($nombre_ids, $data2)) {
         $cssFondo='style="background-color:#FF7575;"';
         $attrChecked='checked';
     }

    /*Concatenamos usando las variables*/
    $mensaje.='<tr '.$cssFondo.'>'.
                '<td>'.
                    '<input type="checkbox" class="check" name="'.$row[0].'" id="'.$nombre_ids.'" '.$attrChecked.'>'.  
                    '<label for="'.$nombre_ids.'">'.$row[2].'</label>'.   
                '</td>';
}     
    
answered by 25.01.2018 / 01:58
source
1

Well, it turns out that there is a function in PHP that is called in_array to verify if a text string exists in an array. So I solved it in the following way:

while($row=mysqli_fetch_array($query)){
 $nombre_ids = $row[2].','.$row[1].','.$row[0];
 // ESTA FUNCIÓN ES LA QUE BUSCA LA CADENA DE LA CONSULTA SQL EN EL ARRAY   
 if (in_array($nombre_ids, $data2)) {
  $mensaje .= '            
  <tr style="background-color:#FF7575;">
   <td>
    <input type="checkbox" class="check" name="'.$row[0].'" id="'.$nombre_ids.'" checked>  
    <label for="'.$nombre_ids.'">'.$row[2].'</label>   
   </td>';
 } else{
  $mensaje .= ' 
  <tr>
   <td>
    <input type="checkbox" class="check" name="'.$row[0].'" id="'.$nombre_ids.'">  
    <label for="'.$nombre_ids.'">'.$row[2].'</label>   
   </td>'; 
 }
}     
    
answered by 25.01.2018 в 00:47