Change the 1 of a MAX CASE to an image in php


Let's see if I can explain friends, I have this query that I have managed to execute correctly thanks to you, I am working on postgreSQL and php, and so my table is built to show on the page in php

<div id="tabs-8" class='panel'>
                PERROS, CERDOS, VACAS, LOROS
                from (
                select distinct row_number() OVER (order by e.FOLIO_FAMILIA, 
                e.ID_MUNICIPIO, e.ID_LOCALIDAD) as No, 
                MAX( case when id_caso=1 then 1 else 0 end) as PERROS,
                MAX( case when id_caso=2 then 1 else 0 end) as CERDOS,
                MAX( case when id_caso=3 then 1 else 0 end) as VACAS,
                MAX( case when id_caso=4 then 1 else 0 end) as LOROS
                from encuesta e , bienes b, zona z
                where e.FOLIO_FAMILIA = b.FOLIO_FAMILIA 
                and e.FOLIO_FAMILIA = z.FOLIO    
                and e.ID_MUNICIPIO = '12-15'
                and id_caso in (1,2,3,4) and z.id_zona='12-15-1'        
                group by e.FOLIO_FAMILIA, e.ID_MUNICIPIO, e.ID_LOCALIDAD
                order by e.FOLIO_FAMILIA, e.ID_MUNICIPIO, e.ID_LOCALIDAD) as t1;
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
            $rows = pg_num_rows ($result);
            $i = pg_num_fields($result);

            echo "<table class='table table-fixed' border=1> <tr>";
            for($j=0; $j<$i; $j++){
                    $fieldname=pg_field_name($result, $j);
                    echo "<th>".strtoupper($fieldname)."</th>";
            echo "</tr>";

            while ($line = pg_fetch_array($result)){
                echo "<tr>";
                    for($j=0; $j<$i; $j++){
                        if ($line[$j] <> "") {
                            echo "<td align='left'>$line[$j]</td>";
                            echo"<td align='left'>&nbsp;</td>"; 
                        echo "</tr>";   
                echo "</table>";

The main function of this table is that the user can see a general analysis regarding the animals, the current result is displayed like this on my page

What I want to achieve is to substitute that one (1) for an image or icon, that is, to have an appearance more or less like this

How can I achieve that, or have some idea of what kind of php code is used to achieve it,


asked by IndiraRivas 07.03.2018 в 21:13

1 answer


If it's like I guess, that you fill your table in while , you could easily do what you want using a ternary Operator.

Let's first look at the code:

if ($line[$j] <> "") {
                            $html=($column==1) ? '<img src="/punto_azul.png">' : $column;
                            echo "<td align='left'>$html</td>";

Now we explain it:

  • $column=$line[$j]; stores each value of the loop for later evaluation.
  • This is the ternary operator: ($column==1) ? '<img src="punto_azul.png">' : $column; What it does is evaluate if $column is equal to 1 . If it is, it will store a valid path of the image in the variable $html . Very important: In the example I have assumed that you have an image called punto_azul.png and that it is in the same folder as the present PHP file. If not, after src you must put the correct path of the image to work .
  • Finally, with echo "<td align='left'>$html</td>" we print the value resulting from each evaluation, which will always be found in $html .

P.D. : To present the blue dot, other techniques can be used. For example if in your project you have libraries like FontAwesome you can take advantage of them for this kind of thing. That way you avoid having to manipulate images. In that case, the line of the image would look like this:

$html=($column==1) ? '<i class="fa fa-circle" aria-hidden="true"></i>' : $column;

This would print a circle of fontawesome . You could easily change the size, or color, using CSS.

answered by 08.03.2018 / 01:03