How to change the field color to the repeated elements shown in a table

1

I need help to change the color of the rows of a table on the page only as long as the policy number and name are the same. The table already has a style but it is only necessary to change the color of the client and policy field if the item is repeated two or more times. I've tried putting conditional but it's not there yet. Thanks

<?php
require_once('class/Consultas.php');
$Reportes = new Reportes();

$dato = $_POST['dato'];
$array = $Reportes->getReportes($dato['telefono'], $dato['cliente'], $dato['promotoria'],$dato['FechaI'],$dato['FechaF']);

if(count($array)>0){    

$tabla = '<div class="col-lg-12">
        <img id="export" name="export" src="imagenes/logo_excel.png" style="cursor:pointer"/></td>
    </div>
    <div class="col-lg-12">
    <div class="table-responsive" style="height:600px;">
      <table class="table table-bordered table-condensed" id="Exportar_a_Excel">
        <thead>
            <tr bgcolor="gray" style="color:white;">
                <th>#</th>
                <th>ID</th>
                <th>FECHA</th>
                <th>PROMOTORIA</th>
                <th>AGENTE</th> 
                <th>POLIZA</th> 
                <th>NOMBRE</th>
                <th>PRIMA</th>
                <th>TELEFONO1</th>
                <th>TELEFONO2</th>
                <th>ENCUESTADO</th> 
                <th>CODIFICACION</th>
                <th>INTENTOS</th>   
                <th>COMENTARIOS</th>
                <th>AGENDADO</th>
            </tr>
        </thead>
        <tbody>';


   $contador = 0;
   $lista_colores=array('#F0F0F0','#99CC66','#FF5011');            

   $num_colores=2;
   $indice=0;

   foreach($array as $row){

        $arrayCodf = $Reportes->getCodificacion($row['telefono1'], $row['telefono2'], $dato['codificacion'],$row["fecha"] );

        if($arrayCodf[0]['ea'] == "")
            $codif = "NO CODIFICADO";
        else
            $codif = $arrayCodf[0]['ea'];

        if($arrayCodf[0]['intentos'] == "")
            $intentos = "0";
        else
            $intentos = $arrayCodf[0]['intentos'];

        if($row['encuestado'] == "")
            $encuestado = "NO";
        else
            $encuestado = $row['encuestado'];

        if($codif != "NO CODIFICADO" || $dato['codificacion'] == "0" || $dato['codificacion'] == "TODOS"){
            $color=$lista_colores[$indice % $num_colores];
            $indice = $indice + 1;
            $tabla.= "<tr bgcolor='".$color."'>";

                if($_SESSION['encuesta'])
                    $tabla.= '<td><a href="#" id="ShowEncuesta" data-id="'.$row['iddirectorio'].'" data-nombre="'.$row['nombre'].'">'.++$contador.'</a></td>';

                else
                    $tabla.= '<td>'.++$contador.'</td>';

                $tabla.= '<td><a href="#" id="ShowDirectorio" data-id="'.$row['iddirectorio'].'">'.$row['id'].'</a></td>';
                $tabla.= '<td>'.$row['fecha'].'</td>';
                $tabla.= '<td>'.$row['promotoria'].'</td>';
                $tabla.= '<td>'.$row['agente'].'</td>';
                $tabla.= '<td>'.$row['poliza'].'</td>';
                $tabla.= '<td>'.$row['nombre'].'</td>';
                $tabla.= '<td>'.$row['prima'].'</td>';
                $tabla.= '<td>'.$row['telefono1'].'</td>';
                $tabla.= '<td>'.$row['telefono2'].'</td>';
                $tabla.= '<td>'.$encuestado.'</td>';
                $tabla.= '<td>'.$codif.'</td>';
                $tabla.= '<td>'.$intentos.'</td>';
                $tabla.= '<td>'.$row['comentarios'].'</td>';
                $tabla.= '<td>'.$row['agendado'].'</td>';
            $tabla.= '</tr>';
        }
   }

   /*
   $dato['cliente'] = $dato['cliente'];

   while ($damefila=mysql_fetch_object($array)){ 
echo "<tr "; 
if ($dato['cliente']>=2) 
    echo "bgcolor=#354648"; //si el resto de la división es 0 pongo un color 
else 
    echo "bgcolor=#ddddff"; //si el resto de la división NO es 0 pongo otro color 
echo ">"; 
   }
   */

   $tabla.= '<tr bgcolor="gray" style="color:white;">
            <td colspan="15" style="font-weight:bold;"  align="center" >Total Reportes : '.$contador.'</td>
          </tr>';
   $tabla.= '</tbody>
      </table>
      </div>
      </div>';

   }else{

    $tabla.= '<div class="col-lg-12">
            <div class="alert alert-danger text-center">
                SIN RESULTADOS PARA MOSTRAR
            </div>
          </div>';

   }

echo $tabla;

?>
    
asked by EviAr 04.01.2017 в 23:38
source

3 answers

0

I do not know if this can help, add this before your first foreach

$duplicados_color = array();
foreach ($array as $row) {
    if(!isset($duplicados_color[$row['poliza'].$row['nombre']])){
        $duplicados_color[$row['poliza'].$row['nombre']] = '#'.random_color();
    }
}

and where you get the colors

$color = $duplicados_color[$row['poliza'].$row['nombre']];

clear and these functions

function random_color_part() {
    return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
}

function random_color() {
    return random_color_part() . random_color_part() . random_color_part();
}
    
answered by 05.01.2017 в 00:10
0

You can have a counter or a boolean that increases or is true at the moment you ask if the policies coincide, then before drawing the table you make the row have a background. I hope it helps you.

    
answered by 05.01.2017 в 01:53
0

I'll give you an example:

var array=[false,true,false,true];

window.onload = function() {

var tabla=document.getElementById('tabla');
var tab=document.createElement('table');


for (var i = 0; i < array.length; i++) {
var tr=document.createElement('tr');
	var td=document.createElement('td');
	if(array[i]==true){
		var texto =document.createTextNode("yes");
		td.className="yes";
	}else{
		var texto =document.createTextNode("no");
		td.className="no";
	}
	td.appendChild(texto);
	tr.appendChild(td);
	tab.appendChild(tr);
tabla.appendChild(tab);
};


};
.yes{
	background-color: red;
}
<div id='tabla'></div>

according to the example the following array: var array=[false,true,false,true]; represents the data of your database, of course only the column on which it will depend if it has to be painted or not (be repeated in your case), and making use of css by the name of the class that we insert td.className="yes"; and td.className="no"; we painted it red repeated (in your case) like this:

.yes{
    background-color: red;
}
    
answered by 05.01.2017 в 03:28