How to close a td with a tr after the loop and continue printing the rest of the code in PHP

Asked

Viewed 135 times

2

all right? I have a small problem taking the data from a database and trying to turn it into a table type, I happen to be looking for the data I need to mount a part of the table in a query and the rest of the information in another query. ex, I have to search the sites through a GROUP BY in a query to show only the result of a site, and through another query, the data I need, it happens that for the table to stay the way I need, I need that at a given time it closes with a , that is, if I have 10 data coming, after this 10th data, I must put one , I did it through an if condition in the code, but when I continue the code, it loses my condition and the entire layout breaks, as I want the table to be:

        SITE
     1  2  3  4
 A   V  V  V  V
 B   V  V  V  V
 C   V  V  V  V
 D   V  V  V  V

The structure of the table would be like this:

 <table>
    <tr>
        <th colspan='4'>SITE</th>
    </tr>
    <tr>
        <td></td>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>
        <td>6</td>
        <td>7</td>
    </tr>
    <tr>
        <td>A</td>
        <td>B</td>
        <td>C</td>
        <td>D</td>
        <td>V</td>
        <td>V</td>
        <td>V</td>
    </tr>
</table>

My PHP code is as follows:

require_once("db.php");
$sql = $pdo->query("SELECT site FROM tabelactr GROUP BY site");
$resultsql1 = $sql->fetchAll();
$i=0;
foreach ($resultsql1 as $key => $value1) {
    $conteudo= "<table class='tg'><tr><th class='tg-0pky' colspan='4'>" . $value1['site'] . "</th></tr><tr><td class='tg-uys7'></td>";
    $sql2 = $pdo->query("SELECT pos,tamanhoCriativo,ctr FROM tabelactr WHERE site = '" . $value1['site'] . "' GROUP BY pos,tamanhoCriativo");
    $cont = $sql2->rowCount();
    foreach ($sql2 as $key2 => $value2) {
        $conteudo.= "<td class='tg-uys7'>" . $value2['tamanhoCriativo'] . "</td>";
        $i++;
        if ($i % $cont == 0) {
            echo "</tr>";
        }
        $conteudo.="<tr><td class='tg-0pky'>" . $value2['pos'] . "</td><td class='tg-xldj'>" . $value2['ctr'] . "</td></tr>"; //aqui está a linha onde está dando erro.
    }
    $conteudo.="</table>";
    print($conteudo);
}

The problem is to continue the code after the if where I write the tr closure, because it groups all the values I have, and it doesn’t stay the way I want it above, does anyone have any idea how I can get around this problem?

Thank you!

1 answer

0

Change to that, I changed inside the second foreach

require_once("db.php");
    $sql = $pdo->query("SELECT site FROM tabelactr GROUP BY site");
    $resultsql1 = $sql->fetchAll();
    $i=0;
    $conteudo = "<table class='tg'>";
    foreach ($resultsql1 as $key => $value1) {
        $conteudo .= "
                <tr>
                    <th class='tg-0pky' colspan='4'>" . $value1['site'] . "</th>
                </tr>";
        $sql2 = $pdo->query("SELECT pos,tamanhoCriativo,ctr FROM tabelactr WHERE site = '" . $value1['site'] . "' GROUP BY pos,tamanhoCriativo");
        $cont = $sql2->rowCount();
        foreach ($sql2 as $key2 => $value2) {
            $conteudo.= "<td class='tg-uys7'>" . $value2['tamanhoCriativo'] . "</td>";
            $i++;
            if ($i % $cont == 0) {
                echo "</tr>";
                echo "<tr>";
            }
            $conteudo.="<td class='tg-0pky'>" . $value2['pos'] . "</td><td class='tg-xldj'>" . $value2['ctr'] . "</td>"; //aqui está a linha onde está dando erro.

        }
        $conteudo.="</table>";
        print($conteudo);
    }
    $conteudo .= "</table>";

Browser other questions tagged

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