This is a general question and does not have any relation to specific programming code. However, this sounds simple, but I can not manage to figure it out.
Giving the following DB structure:
id | name | type
1 | testA | A
2 | testA | A
3 | testA | A
4 | testB | B
5 | testB | B
from this i want to generate table that would look like this:
HTML TABLE:
Col A Col B
testA testB
testA testB
testA
so the html code would looks like:
<table>
<tr>
<th>Col A</th>
<th>Col B</th>
</tr>
<tr>
<td>testA</td>
<td>testB</td>
<tr>
<tr>
<td>testA</td>
<td>testB</td>
<tr>
<tr>
<td>testA</td>
<td></td>
<tr>
<table>
Although, if you have some solution using inline colspan
and rowspan
it is also welcome.
I could not find an elegant solution.
My code so far is:
// Not Working:
$elements = array('doc'=>array(),'pub'=>array());
$db = new SQLite3('sqlite3.db');
$results = $db->query('SELECT * FROM l ORDER BY type;');
while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
array_push($elements[$row['type']], $row);
}
$fi = array();
for ($i=0;$i<count($elements['pub'])+count($elements['doc']);$i++){
if ($i%2==0){
array_push($fi,$elements['pub'][floor($i/2)]);
}else{
array_push($fi,$elements['doc'][floor($i/2)]);
}
}
var_dump($fi);
exit();
// This is not working because most of the times
// the count of elements from type A
// is NOT equal to elements of type B.
There is no connection between the A/B values in the same row, so we have to invent one. This can be done by using the rowids of newly inserted rows (the SELECT just does a full outer join):
CREATE TEMPORARY TABLE tempA AS
SELECT name FROM l WHERE type = 'A';
CREATE TEMPORARY TABLE tempB AS
SELECT name FROM l WHERE type = 'B';
SELECT tempA.name, tempB.name
FROM tempA
LEFT JOIN tempB USING (rowid)
UNION ALL
SELECT tempA.name, tempB.name
FROM tempB
LEFT JOIN tempA USING (rowid)
WHERE tempA.name IS NULL;
DROP TABLE tempA;
DROP TABLE tempB;