Dynamic table with PHP

Asked

Viewed 1,270 times

-3

I am creating a dynamic table using PHP.

The problem and the following created a field to modify the values of the variable $conteudo table body however can’t change more than one table field someone there can help me to solve this problem?

PS. to pass the field to be modified and as follows (field 1,field 2,field 3)

<?php

/*
 * CABEÇALHO DA TABELA
 */
$campos = explode(',','id,nome,idade,sexo');
$conteudo = explode(',','1,funano,21,1');


/*
 * MODIFICADOR DE CAMPOS
 */
$modificador = explode(',','21');



echo '<table border="1px" width="100%">';
echo '<tr>';

foreach($campos as $x){
            echo '<th>'.$x.'</th>';
}
echo '</tr>';



/**
 * CONTEUDO DA TABELA
 */
echo '<tr>';
foreach($conteudo as $b){
    /*
     * COMPARANDO O MODIFICADOR COM O CAMPO DA TABELA
     */
    foreach($modificador as $m) {


        if ($b == $m) {
            echo '<th>' . 'campo' . '</th>';
        } else {
            echo '<td>'.$b.'</td>';
        }
    }
}
echo '</tr>';
echo '</table>';

The problem happens when I try to add another field to modify as for example:

$modificador = explode(',','21,funano');

  • explains more what you tried ... from what I see in the comments who did knows well what has there

  • runs the script you will understand better

  • explains better what you need and what you tried to make clear

  • You can only change one field because it only uses one field in the modifier, it works right here: http://phpfiddle.org/main/code/n7ri-zas8

  • Jorge B tries to add another field $modifier = explodes(',','21,so-and-so'); and see what will happen to the table

  • Then you compare "21" from $m with "21" of $b and when this happens it replaces "21" with "field".

  • perfect now to scold and try to compare more than one table field when I try that the table gets all bugged

  • This is because you always print either field or value whenever you compare.

  • And how to solve this jam?

Show 5 more comments

1 answer

3


The problem is you’re printing the field value or the modifier every time you check all the $m and ends up printing more than once per field.

You can check when there’s match of $m with $b if it’s already been printed:

foreach($conteudo as $b){
    /*
     * COMPARANDO O MODIFICADOR COM O CAMPO DA TABELA
     */
    $isEqual = false;
    foreach($modificador as $m) {

        if ($b == $m) {
            echo '<th>' . 'campo' . '</th>';
            $isEqual = true; // se forem iguais imprime campo
        }
    }
    if(!$isEqual) echo '<td>'.$b.'</td>';//senão imprime o valor de $b
}

phpfiddle

Or you can use the function in_array PHP comparing whether an element is in array:

foreach($conteudo as $b){
    /*
     * COMPARANDO O MODIFICADOR COM O CAMPO DA TABELA
     */
    if (in_array($b, $modificador))
        echo '<th>' . 'campo' . '</th>';
    else
        echo '<td>'.$b.'</td>';
}
  • I managed to solve all problems with helping thanks to everyone who made your time available

Browser other questions tagged

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