Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: Creating big HTML-table efficiently
    primarykey
    data
    text
    <p>I'm making a HTML table based on certain information in my MySQL database. The tables I need from the database hold the data for rows (<em>rowid</em>, <em>rowname</em>), columns (<em>columnid</em>, <em>columnname</em>) and cell data. A table, that links IDs from rows and columns together (<em>cellid</em>, <em>rowid</em>, <em>columnid</em>, <em>somedata</em>). The table looks something like this:</p> <pre><code>__________| column1 | column2 | column3 | ... row1 | somedata | somedata | | ----------|----------|----------|----------|----- row2 | somedata | | somedata | ----------|----------|----------|----------|----- ... | | | | </code></pre> <p>My approach was to use several nested foreach loops, something like this:</p> <pre><code>$rows = new Rows(); //These objects are containers for objects $columns = new Columns(); //that hold some of the database data. $alldata = new Alldata(); //MySQL queries within these objects are irrelevant, I think. They already get the relevant data $count = count($alldata); echo "&lt;table&gt;"; echo "&lt;tr&gt;"; echo "&lt;td&gt;&amp;nbsp;&lt;/td&gt;"; foreach ($columns-&gt;getColumns() as $column) { echo "&lt;td&gt;".$column-&gt;getColumnname()."&lt;/td&gt;"; } echo "&lt;/tr&gt;"; foreach ($rows-&gt;getRows() as $row) { echo "&lt;tr&gt;"; echo "&lt;td&gt;".$row-&gt;getRowname()."&lt;/td&gt;"; foreach ($columns-&gt;getColumns() as $column) { $last = 1; foreach ($alldata-&gt;getAlldata() as $data) { if ($data-&gt;getCID() == $column-&gt;getID() &amp; $data-&gt;getRID() == $row-&gt;getID()) { //If the column has data the related to the row echo "&lt;td&gt;".$data-&gt;getSomedata()."&lt;/td&gt;"; break; } if ($last == $count) { //if loop couldn't find any entries, it prints an empty cell echo "&lt;td&gt;&amp;nbsp;&lt;td&gt;"; } $last++ } } echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; </code></pre> <p>Bruteforcing, obviously this is not very efficient method when there are several hundred rows of data on any table in the database and I don't like this. <strong>Any ideas how to make this more efficient? Is there better way of creating a table like I need?</strong></p> <p>EDIT:<br> Pondering about this problem for a week has finally given me some answers. It was so simple!<br> I did a slight modification to my <em>Column</em> object class. Previously, my code went through all possible entries on <em>celldata</em> table in the database. Now <em>Column</em> object gets an array of <em>celldata</em> where <em>columnid's</em> match and I can do the comparison using only that data.</p> <pre><code>$rows = new Rows(); $columns = new Columns(); echo "&lt;table&gt;"; echo "&lt;tr&gt;"; echo "&lt;td&gt;&amp;nbsp;&lt;/td&gt;"; foreach ($columns-&gt;getColumns() as $column) { echo "&lt;td&gt;".$column-&gt;getColumnname()."&lt;/td&gt;"; } echo "&lt;/tr&gt;"; foreach ($rows-&gt;getRows() as $row) { echo "&lt;tr&gt;"; echo "&lt;td&gt;".$row-&gt;getRowname()."&lt;/td&gt;"; foreach ($columns-&gt;getColumns() as $column) { $last = 1; $count = count($column-&gt;getAlldata()); foreach ($column-&gt;getAlldata() as $data) { if ($data-&gt;getCID() == $column-&gt;getID() &amp; $data-&gt;getRID() == $row-&gt;getID()) { echo "&lt;td&gt;".$data-&gt;getSomedata()."&lt;/td&gt;"; break; } if ($last == $count) { echo "&lt;td&gt;&amp;nbsp;&lt;td&gt;"; } $last++ } } echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; </code></pre> <p>Much faster, although still bruteforcing but there's less amount of data to go through. Of course, now the problem is that <em>Column</em> objects may do quite a lot of MySQL queries, depending on how many <em>Columns</em> there are.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload