PHP class for HTML table: strange attribute assignment
P粉384679266
P粉384679266 2023-09-06 00:38:07
0
1
474
<p>In the class that builds the HTML table, I have this method that renders the table. Everything works fine except for the assignment of HTML attributes under certain conditions (indentation, closing tags, data representation, etc.). When I set the cell data, I call setData() to receive the three parameters and use it like this. Notice how I set the third parameter (cell property): </p> <p><strong>Method definition: </strong></p> <pre class="brush:php;toolbar:false;">public function setData( array $data, array $row_attributes = [], array $cell_attributes = [] ): bool { // the code }</pre> <p><strong>Call: </strong></p> <pre class="brush:php;toolbar:false;">$table->setData( $pagination->resultset, [], // row attributes array( // cell attributes array(), // row 1 (index 0) array( // row2 (index 1) ["id"=>"R2C1id"], // row 2, cell 1 ["id"=>"R2C2id", "onclick"=>"R2C2_onclick();"], // row 2, cell 2 ), array( // row 3 [], [], ["id"=>"R3C3id", "selected"=>"selected"], // row 3, cell 3 [], [], [], [] ) ) );</pre> <p>In this example, the table has seven columns. </p> <p>Here you will see the HTML output. Note the cell properties of the second and third rows: </p> <pre class="brush:php;toolbar:false;"><div class='table-body'> <div class='table-row'> <div class='table-row-cell'>1</div> <div class='table-row-cell'>Consumidor Final</div> <div class='table-row-cell'>Consumidor Final</div> <div class='table-row-cell'></div> <div class='table-row-cell'>1</div> <div class='table-row-cell'></div> <div class='table-row-cell'></div> </div> <div class='table-row'> <div class='table-row-cell' id='R2C1id'>2</div> <div class='table-row-cell' id='R2C2id' onclick='R2C2_onclick();'>Prueba SRL</div> <div class='table-row-cell' 0='Array' 1='Array'>Tu Prueba</div> <div class='table-row-cell' 0='Array' 1='Array'>12345678901</div> <div class='table-row-cell' 0='Array' 1='Array'>1</div> <div class='table-row-cell' 0='Array' 1='Array'></div> <div class='table-row-cell' 0='Array' 1='Array'></div> </div> <div class='table-row'> <div class='table-row-cell'>3</div> <div class='table-row-cell'>Otra Prueba SA</div> <div class='table-row-cell' id='R3C3id' selected='selected'>Prueba 2</div> <div class='table-row-cell'>12345678902</div> <div class='table-row-cell'>1</div> <div class='table-row-cell'></div> <div class='table-row-cell'></div> </div> </div></pre> <p>This is the code I use to do all this. I'll show you the code snippet for cell rendering and how to handle properties. </p> <p><strong>Cell rendering: </strong></p> <pre class="brush:php;toolbar:false;">// process cells $row_counter = 0; foreach ($this->data as $data) { // row $row_build = ""; $cell_counter = 0; foreach ($data as $cell_data) { // cell if ($cell_counter < $col_count) { $row_build .= $this->getHtmlDiv( $html_cell_class, $cell_data ?? "", $this->getHtmlAttributes("cell", $row_counter, $cell_counter), 3 ); } $cell_counter ; } // $cell_counter ; // complete empty cells to preserve row:hover on full row while ($cell_counter < $col_count) { $row_build .= $this->getHtmlDiv( $html_cell_class, "", $this->getHtmlAttributes("cell", $row_counter, $cell_counter), 3 ); $cell_counter ; } $body_build .= $this->getHtmlDiv( $html_row_class, $row_build, $this->getHtmlAttributes("row", $row_counter, 0), 2, true ); $row_counter ; }</pre> <p><strong>Attribute methods:</strong></p> <pre class="brush:php;toolbar:false;">private function getHtmlAttributes(string $section, int $row, int $column): array { if (count($this->html_attributes[$section]) > 0) { if (array_key_exists($row, $this->html_attributes[$section])) { if ($section === "cell") { if (array_key_exists($column, $this->html_attributes[$section][$row])) { return $this->html_attributes[$section][$row][$column]; } } return $this->html_attributes[$section][$row]; } } return []; }</pre> <p>What’s wrong? Thanks. </p>
P粉384679266
P粉384679266

reply all(1)
P粉349222772

Well, when I posted the question and selected the code, the answer appeared. In method getHtmlAttributes(), else condition is missing.

private function getHtmlAttributes(string $section, int $row, int $column): array
{

    if (count($this->html_attributes[$section]) > 0) {
        if (array_key_exists($row, $this->html_attributes[$section])) {
            if ($section === "cell") {
                if (array_key_exists($column, $this->html_attributes[$section][$row])) {
                    return $this->html_attributes[$section][$row][$column];
                } else { // <-- this 
                    return [];
                }
            }
            return $this->html_attributes[$section][$row];
        }
    }
    return [];
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template