I have 3 tables in my MySql database.
-------------
| countries |
-------------
| iso |
| name |
-------------
--------------------
| documents |
--------------------
| id |
| country_iso |
| publication_date |
--------------------
---------------
| files |
---------------
| document_id |
| name |
---------------
Could you suggest a mysql query and php function to print this to a table as below.
Where d = documents->files->name, each year can have many document for a particular country.
-----------------------------------------
| | 2009 | 2008 | 2007 | 2006 |
-----------------------------------------
| country a | d d | | d | d |
| country b | | d | d | |
| country c | d | d d | | d |
| country d | d | | d d | |
-----------------------------------------
Thanks in advance
If there can be only one file per each document, you should combine the documents and files table so that the documents table has a filename field.
This example assumes one-to-one relation between files and documents. Instead of creating exotic SQL-queries, it's simpler to just select plain data from DB and transform it using PHP.
SELECT
countries.name country,
documents.name document,
files.name filename,
DATE_FORMAT(publication_date, '%Y')
FROM
documents
LEFT JOIN
countries
ON
documents.country_iso = countries.iso
LEFT JOIN
files
ON
documents.id = files.document_id
That will result in a followin table from the database:
country | document | filename | year
----------------------------------------------------
Germany | doc_1 | doc_1.pdf | 2009
Germany | doc_2 | doc_2.pdf | 2007
Germany | doc_3 | doc_3.pdf | 2007
Norway | doc_6 | doc_6.pdf | 2008
...
Next that table has to be transformed with PHP to proper form. The proper form in this case would be that all documents are indexed by year which are in turn indexed by customer:
$q = mysql_query("SELECT ...");
$result = array();
$years = array();
while($row = mysql_fetch_assoc($q)) {
$result[$row['country']][$row['year']][] = array('document' => $row['document'], 'filename' => $row['filename']);
$years[] = $row['year'];
}
// Max and min years are used to print the table header
$max_year = max($years);
$min_year = min($years);
Now, if you would like to find Germany's documents for year 2009, you could just use $result['Germany'][2009];. But you can use a nifty looping to print the table automatically:
<table>
<tr>
<th>Country</th>
<?php for($y = $max_year; $y >= $min_year; $y--): ?>
<th><?php echo $y; ?></th>
<?php endfor; ?>
</tr>
<?php foreach($result as $country => $years): ?>
<tr>
<td><?php echo $country; ?></td>
<?php for($y = $max_year; $y >= $min_year; $y--): ?>
<td>
<?php if(isset($years[$y])): foreach($years[$y] as $document): ?>
<a href="<?php echo $document['filename']; ?>">
<?php echo $document['name']; ?>
</a>
<?php endforeach; endif; ?>
</td>
<?php endfor; ?>
</tr>
<?php endforeach; ?>
</table>
There you go. Note that I haven't tested this, there might be parse errors and some other illogicalities but I guess you get the idea from this.
Related
I have two tables:
Clubs
|---------------------|------------------|
| id | name |
|---------------------|------------------|
| 1 | Arsenal |
|---------------------|------------------|
| 2 | Chelsea |
|---------------------|------------------|
| 3 | Fulham |
|---------------------|------------------|
| 4 | Leeds |
|---------------------|------------------|
Matches
|---------------------|------------------|------------------|
| id | home | away |
|---------------------|------------------|------------------|
| 1 | 1 | 3 |
|---------------------|------------------|------------------|
| 2 | 2 | 4 |
|---------------------|------------------|------------------|
Explanation: the numbers in the columns "home" and "away" in the Matches table linking to the the id in de Clubs table.
Now I want to echo (with php) the following:
|---------------------|------------------|
| home | away |
|---------------------|------------------|
| Arsenal | Fulham |
|---------------------|------------------|
| Chelsea | Leeds |
|---------------------|------------------|
Explanation: when clicking on the teamname I want to open the club.php page with the teaminfo.
I've tried the following:
<?php
$sql = "SELECT * FROM matches JOIN clubs ON matches.home AND matches.away = clubs.id"
$rs_result = $conn->query($sql);
?>
<div style="overflow-x:auto">
<table>
<tr><th><strong>Home</strong></th><th><strong>Away</strong></th><tr>
<?php
while($row = $rs_result->fetch_assoc()) {
?>
<tr>
<td><? echo $row['home']; ?></td>
<td><? echo $row['away']; ?></td>
</tr>
<?php } ?>
</table></div>
It is echoing the following:
|---------------------|------------------|
| home | away |
|---------------------|------------------|
| 1 | 3 |
|---------------------|------------------|
| 2 | 4 |
|---------------------|------------------|
My question: How can I show the clubnames in the table but passing the club id in the "a href" link?
I've tried $row[1] and so on but it doesnt work. Which SQL statement do I need? JOIN? INNER JOIN? LEFT JOIN?
Many thanks in advance!
I believe this should work - I made an adjustment to your query.
You can use left joins when you are interested in the left side of the query (matches) and do not care if the clubs have not been populated. F ex if you have a home - away of 1 and 5. We know that 1 is Arsenal but there is no entry for 5 in clubs.
Left join will display it, inner join will not.
<?php
$sql = "SELECT home,away,homeclub.name as homename,awayclub.name as awayname FROM matches
JOIN clubs as homeclub ON matches.home = homeclub.id
JOIN clubs as awayclub ON matches.away = awayclub.id"
$rs_result = $conn->query($sql);
?>
<div style="overflow-x:auto">
<table>
<tr><th><strong>Home</strong></th><th><strong>Away</strong></th><tr>
<?php
while($row = $rs_result->fetch_assoc()) {
?>
<tr>
<td><? echo $row['homename']; ?></td>
<td><? echo $row['awayname']; ?></td>
</tr>
<?php } ?>
</table>
</div>
I want to make sub group in group by (SQL).
in my case, I want to make classified detail in group detail which it come from two different column, for more detail, check this out :
I've sql table like this :
+----+-------+--------+-------+
| No | data1 | detail | price |
+----+-------+--------+-------+
| 1 | ID1 | Food | $ 100 |
| 2 | ID1 | Drink | $ 25 |
| 3 | ID2 | Drink | $ 25 |
| 4 | ID1 | Snack | $ 50 |
| 5 | ID2 | Snack | $ 50 |
+----+-------+--------+-------+
I want to make result in my php page like this :
+----+-------+--------+-------+
| No | detail_lunch | price |
+----+-------+--------+-------+
| 1 | Food | |
| | - ID1 | $ 100 |
| 2 | Snack | |
| | - ID1 | $ 25 |
| | - ID2 | $ 25 |
| 3 | Drink | |
| | - ID1 | $ 10 |
| | - ID2 | $ 10 |
+----+-------+--------+-------+
I already tried with GROUP CONCAT as below :
Controller :
$d['data'] = $this->db->query("select detail_item, GROUP_CONCAT (detail) detail_lunch
from table ORDER BY detail_lunch ASC");
Views :
<?php
foreach($data->result_array() as $d)
{
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $d['detail_lunch']; ?></td>
<td><?php echo $d['price']; ?></td>
</tr>
<?php
$no++;
}
?>
My code above not working,
is there any suggestion to solve my problem?
thanks...
GROUP BY is not needed since you're not using an aggregate function (combining two rows of values into a single value), you are attempting to create a pivot table, (joining two columns under a related column as multiple rows). You only need to ORDER BY detail ASC, data1 ASC, iterate over the resultset to display the detail when it changes, otherwise display the data1 and price
Example: https://3v4l.org/7tVDS
Query:
$data = $this->db->query('SELECT data1, detail, price
FROM table ORDER BY detail ASC, data1 ASC');
View:
<?php
//...
$no = 1;
$currentDetail = null;
foreach ($data->result_array() as $d) {
if ($d['detail'] !== $currentDetail) { ?>
<!-- Only Show the Detail when changed -->
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $d['detail']; ?></td>
<td> </td>
</tr>
<?php } ?>
<!-- Always show the data1/price on subsequent row -->
<tr>
<td> </td>
<td>- <?php echo $d['data1']; ?></td>
<td><?php echo $d['price']; ?></td>
</tr>
<?php
$currentDetail = $d['detail'];
} ?>
Result:
| 1 | Food |
| - ID1 | $ 100 |
| 2 | Drink |
| - ID1 | $ 25 |
| - ID2 | $ 25 |
| 3 | Snack |
| - ID1 | $ 50 |
| - ID2 | $ 50 |
Alternatively you can manipulate how the data is displayed in your view, such as using an unordered list ul in the detail table column td along with price. By using PHP to organize the values how you would like them to be displayed in your view, in an organized associative array.
Example: https://3v4l.org/8sIrR
$dataArray = [];
foreach ($data as $d) {
if (!array_key_exists($d['detail'], $dataArray)) {
$dataArray[$d['detail']] = [];
}
$dataArray[$d['detail']][] = $d;
}
//...
$no = 1;
foreach ($dataArray as $detail => $d) { ?>
<tr>
<td><?php echo $no++; ?></td>
<td>
<?php echo $detail; ?>
<ul>
<?php foreach ($d as $v) { ?>
<li><?php echo $v['data1']; ?> <?php echo $v['price']; ?></li>
<?php } ?>
</ul>
</td>
</tr>
<?php } ?>
Query is the problem .
Select detail_lunch from table group by details.
Then you can use if statement on that result.
I will update my answer after I get to work on my computer.
My database design : http://i.stack.imgur.com/3Rm2e.png
I am trying to use below code :
$myselect = mysql_query("
SELECT
reason.ReasonName,
tblexam.EXAMDATE,
tblexam.EXAMTIME,
prefix.PREFIXABB,
studentmaster.STUDENTNAME,
studentmaster.STUDENTSURNAME,
studentmaster.STUDENTCODE,
program.PROGRAMNAME,
course.COURSENAMEENG
FROM
reason,
formlate,
tblexam,
studentmaster,
prefix,
program,
course
WHERE
reason.ReasonID = formlate.ReasonID AND
formlate.CLASSID = tblexam.CLASSID AND
formlate.COURSEID = formlate.COURSEID AND
formlate.ROOMID = formlate.ROOMID AND
prefix.PREFIXID = studentmaster.PREFIXID AND
formlate.STUDENTID = studentmaster.STUDENTID AND
program.PROGRAMID = studentmaster.PROGRAMID AND
course.COURSEID = tblexam.COURSEID
GROUP BY
reason.ReasonID,studentmaster.STUDENTID
ORDER BY
reason.ReasonID ASC");
<body>
<?php $i =1;
while($rowfet = mysql_fetch_array($myselect)){
?>
<h2><?php echo $rowfet['ReasonName']; ?></h2>
<h3><?php echo DateThai($rowfet['EXAMDATE']) . ' ' . MorningEvening($rowfet['EXAMTIME']); ?></h3>
<table width="100%" border="1">
<thead>
<tr>
<th>No.</th>
<th>studentid </th>
<th>Fullname </th>
<th>Program </th>
<th>Coursename </th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><?php echo $i; ?></td>
<td><?php echo $rowfet['STUDENTCODE']; ?></td>
<td><?php echo $rowfet['PREFIXABB'] . $rowfet['STUDENTNAME'] . ' '.$rowfet['STUDENTSURNAME']; ?></td>
<td><?php echo $rowfet['PROGRAMNAME']; ?></td>
<td><?php echo $rowfet['COURSENAMEENG']; ?></td>
</tr>
</tbody>
</table>
<?php $i++; } ?>
but it shows the result like :
Reason1
10 Jan 2015 (Morning)
| No | studentid | Fullname | Program | Coursename |
-------------------------------------------------------
| 1 | 111111 | John | ProgA | Math |
---------------------------------------------------------
Reason1
10 Jan 2015 (Morning)
| No | studentid | Fullname | Program | Coursename |
-------------------------------------------------------
| 2 | 2222222 | Jane | ProgB | Math |
-----------------------------------
Reason2
10 Jan 2015 (Evening)
| No | studentid | Fullname | Program | Coursename |
-------------------------------------------------------
| 3 | 3333333 | Hawk | ProgB | Math |
-----------------------------------
I want the result to show like :
Reason1
10 Jan 2015 (Morning)
| No | studentid | Fullname | Program | Coursename |
-------------------------------------------------------
| 1 | 111111 | John | ProgA | Math |
---------------------------------------------------------
| 2 | 2222222 | Jane | ProgB | Math |
-----------------------------------
Reason2
10 Jan 2015 (Evening)
| No | studentid | Fullname | Program | Coursename |
-------------------------------------------------------
| 1 | 3333333 | Hawk | ProgB | Math |
---------------------------------------------------------
I want to group table by
Reasonname
Examdate
Examtime
Please help, I'm a newbie.
I've create database in SQLFiddle Here this please try and help:
SQLFiddle
considered that you have a sql result that looks like this, ordered by reason, date and time :
reason.reasonName | examDate | examTime | prefix | studentName | studentSurname | studentCode | ProgramName | courseName
with a few records, like :
1 | someDate1 | someTime1 | A | James | Jim | 123 | PA | PACourse
1 | someDate1 | someTime1 | A | Tommy | Tim | 222 | PC | PCCourse
1 | someDate2 | someTime2 | B | James | Jim | 123 | PB | PBCourse
2 | someDate1 | someTime1 | C | Jamie | Jam | 444 | PD | PDCourse
2 | someDate1 | someTime1 | E | Willy | Wil | 555 | PE | PECourse
2 | someDate2 | someTime2 | F | Cathy | Cat | 777 | PF | PFCourse
2 | someDate2 | someTime2 | G | David | Dav | 844 | PL | PLCourse
2 | someDate3 | someTime3 | L | James | Jim | 123 | PM | PMCourse
3 | someDate4 | someTime4 | W | Ibear | Ibe | 999 | PX | PXCourse
Here, you get this kind of data, you have,
for reason 1 :
2 students with exam at the same day/time (time1)
1 student with axam at another time (time2)
for reason 2 :
2 students with exam at the same day/time (time1)
2 students with exam at the same day/time (time2)
1 student with exam at another time (time3)
for reason 3 :
1 student with exam at time 4
SO, you want to display :
2 tables for reason 1 (time1 / time2)
3 tables for reason 2 (time1 / time2 / time3)
1 table for reason 3
CONSIDERED that the table, when you get it from your SQL is REALLY oredered like what I wrote above, you have to make your checks on fetch :
<?php
//YOU WILL HAVE TO CHECK THESE VARS TO MAKE IT CLEAR, IF SAME TABLE OR NOT
$i = false;
//I use $i as a table detector, false at start, once a table has been opened it is turned as true
$reason = "";
//I will keep the latest reasonName in this
$examDate = "";
//I will keep the latest examDate in this
$examTime = "";
//I will keep the latest examTime in this
$yourDisplay = "";
//In yourDisplay, i keep the text i have to display (tables, data...)
$closeTable = "</tbody></table>";
//This is the code you need to close a table.
while($rowfet = mysql_fetch_array($myselect)){
if(($rowfet['ReasonName']!=$reason)||(DateThai($rowfet['EXAMDATE'])!=$examDate)||(MorningEvening($rowfet['EXAMTIME'])!=$examTime)){
//YOU GET HERE IF YOU HAVE DIFFERENT REASON/DATE/TIME, SO YOU HAVE TO MAKE A NEW TABLE
$reason = $rowfet['ReasonName'];
$examDate = DateThai($rowfet['EXAMDATE']);
$examTime = MorningEvening($rowfet['EXAMTIME']);
//NOW CHECK IF YOU ALREADY HAVE WRITTEN A TABLE BEFORE
if($i == true){
//You already have written a table before
$yourDisplay.=$closeTable;
}else{
//This is the first table you write
$i=true;
}
$yourDisplay.=" <h2>".$reason."</h2>
<h3>".DateThai($examDate)." ".MorningEvening($examTime)."</h3>
<table width='100%' border='1'>
<thead>
<tr>
<th>No.</th>
<th>studentid</th>
<th>fullname</th>
<th>Program</th>
<th>Coursename</th>
</tr>
</thead>
<tbody>
<tr>
<td>".$rowfet['STUDENTCODE']."</td>
<td>".$rowfet['PREFIXABB']." ".$rowfet['STUDENTNAME']." ".$rowfet['STUDENTSURNAME']."</td>
<td>".$rowfet['PROGRAMNAME']."</td>
<td>".$rowfet['COURSENAMEENG']."</td>
</tr>";
}else{
//YOU ARE ALREADY IN A TABLE! WITH SAME REASON/TIME/DATE
//YOUR RECORD JUST NEEDS TO BE ADDED IN A NEW LINE
$yourDisplay.="<tr>
<td>".$rowfet['STUDENTCODE']."</td>
<td>".$rowfet['PREFIXABB']." ".$rowfet['STUDENTNAME']." ".$rowfet['STUDENTSURNAME']."</td>
<td>".$rowfet['PROGRAMNAME']."</td>
<td>".$rowfet['COURSENAMEENG']."</td>
</tr>";
}
}
//ONCE OUT, CLOSE THE LAST TABLE AND DISPLAY YOUR DATA
if($i==1){
$yourDisplay.=$closeTable;
}
echo $yourDisplay;
?>
In My Opinion, this kind of approach should solve your problem IF THE SQL QUERY returns the rows as explained above. (seems like it is the case)
EDIT :
You asked to "not display" the reasonname if it was the same, so just look at the line you see this :
$yourDisplay.=" <h2>".$reason."</h2>
<h3>".DateThai($examDate)." ................ code continue
and replace it with this :
if($rowfet['ReasonName']!=$reason){
$yourDisplay.="<h2>".$reason."</h2>";
}
$yourDisplay.="<h3>".DateThai($examDate)." ".MorningEvening($examTime)."</h3>
<table width='100%' border='1'>
//.... continue the code
You're looping through all your rows and creating a table for each of the rows.
What you want to do is to create a new table only if a sequence of a new ReasonName starts.
$myselect = mysql_query("SELECT reason.ReasonName,tblexam.EXAMDATE,tblexam.EXAMTIME,prefix.PREFIXABB,studentmaster.STUDENTNAME,studentmaster.STUDENTSURNAME,studentmaster.STUDENTCODE,program.PROGRAMNAME,course.COURSENAMEENG FROM reason, formlate, tblexam, studentmaster,prefix, program, course WHERE reason.ReasonID = formlate.ReasonID AND formlate.CLASSID = tblexam.CLASSID AND formlate.COURSEID = formlate.COURSEID AND formlate.ROOMID = formlate.ROOMID AND prefix.PREFIXID = studentmaster.PREFIXID AND formlate.STUDENTID = studentmaster.STUDENTID AND program.PROGRAMID = studentmaster.PROGRAMID AND course.COURSEID = tblexam.COURSEID GROUP BY reason.ReasonID,studentmaster.STUDENTID ORDER BY reason.ReasonID ASC");
<body>
<?php $i =1;
while($rowfet = mysql_fetch_array($myselect)){
?>
<h2><?php echo $rowfet['ReasonName']; ?></h2>
<h3><?php echo DateThai($rowfet['EXAMDATE']) . ' ' . MorningEvening($rowfet['EXAMTIME']); ?></h3>
<table width="100%" border="1">
<thead>
<tr>
<th>No.</th>
<th>studentid </th>
<th>Fullname </th>
<th>Program </th>
<th>Coursename </th>
</tr>
</thead>
<tbody>
<?php echo "You should create a loop here to loop through your ReasonNames"; ?>
<tr>
<td align="center"><?php echo $i; ?></td>
<td><?php echo $rowfet['STUDENTCODE']; ?></td>
<td><?php echo $rowfet['PREFIXABB'] . $rowfet['STUDENTNAME'] . ' '.$rowfet['STUDENTSURNAME']; ?></td>
<td><?php echo $rowfet['PROGRAMNAME']; ?></td>
<td><?php echo $rowfet['COURSENAMEENG']; ?></td>
</tr>
<?php echo "Your loop should end here"; ?>
</tbody>
</table>
<?php $i++; } ?>
I have a table that contains Aspiring team for particular positions and various vote casted.
The data is below
Teamtable
| no | team | position | votes Cast |
| 1 | A | President | 2 |
| 3 | B | President | 1 |
| 4 | C | Secretary | 2 |
| 6 | D | Secretary | 1 |
I want to be able to get this in an html format using php and mysql just as below
EXPECTED VIEW IN THE HTML AND PHP FORMAT
PRESIDENT
Team | Total Votes | Percentage |
A | 2 | 66.67 |
B | 1 | 33.33 |
SECRETARY
Team | Total Votes | Percentage |
C | 2 | 66.67 |
D | 1 | 33.33 |
This is what i have tried so far
//QUERY
$SQL=SELECT
`team`,
`position`,
`votesCast`
FROM
Teamtable
$Results=$db->query($SQL);
$data=array();
while($row=mysqli_fetch_assoc($Results)){
$team=$row['team'];
$position=$row['position'];
$totalVote=$row['votesCast'];
$data[$position][]=$position;
$data1[$position][]=$team;
$data2[$position][]=$totalVote;
}
foreach($data as $position =>$electionResults){
$teams=$data1[$position];
$totalVotes=$data2[$position];
foreach($teams as $re => $teas){
$votes=$totalVotes[$re];
echo "
<table>
<tr>
<td>$teas</td>
<td>$votes</td>
</tr>
</table>";
}
}
I have to tried up to this point, any help is appreciated.
This could be very helpful for you
while($row = mysqli_fetch_assoc($result)) {
$data[$row['position']]['total'][]=$row['votes'];
$data[$row['position']][]=$row;
}
foreach($data as $k=>$v){
echo '<p>'.$k.'</p>';
echo '<table border="1">';
$total_votes=array_sum($v['total']);
foreach($v as $kk=>$vv){
if($kk!=='total'){
$percentage=round($vv['votes']/$total_votes*100,2);
echo '<tr><td>'.$vv['tean'].'</td><td>'.$vv['votes'].'</td><td>'.$percentage.'%</td></tr>';
}
}
echo '</table>';
}
You wan't to have the table on the outside of the foreach.
echo "<table>";
foreach($teams as $re => $teas){
$votes=$totalVotes[$re];
echo "<tr>
<td>$teas</td>
<td>$votes</td>
</tr>";
}
echo "</table>";
I have a table chart. Lets say 5 by 5. I run a loop such as
<table>
<tbody>
<?php for ($i = 0; $i < 5; $i += 5) {
echo "<tr>
<td>one box</td>
<td>one two</td>
<td>one three</td>
<td>one four</td>
<tr>";
}?>
</tbody>
</table>
It creates a table such as
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
Now I have mysql data I load for my purposes and I need it to put the data in respectively so the table looks like
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | Res 1| | | |
-------------------------------------
| | | | Res 3| |
-------------------------------------
| |Res 4 | | | Res 2|
-------------------------------------
How would I do this? I have 50 results and need to fill the results into the correct column and row. I need to do some sort of if(results[0-50]['id'] == rowcolumnid) echo the results for the correct table while doing the for loop.
Edit: Here is my full code.
<table id="schedule">
<tbody>
<?php
$time = mktime(0, 0, 0, 1, 1);
for ($i = 28800; $i < 62200; $i += 1800) { ?>
<tr id="row<?php echo $i; ?>">
<td id="hour">
<?php
printf('%1$s',date('g:i a', $time + $i));
?>
</td>
<td id="sunday"></td>
<td id="monday"></td>
<td id="tuesday"></td>
<td id="thursday"></td>
<td id="friday"></td>
<td id="saturday"></td>
</tr>
<?php } ?>
</tbody>
</table>
My mysql results are in datetime format that I'm going to use to propogate the table.
Results:
ID| EVENT NAME | DATEOFEVENT
1 | event name | 2012-11-20 12:00:00
2 | event name | 2012-11-21 13:30:00
3 | event name | 2012-11-22 13:00:00
4 | event name | 2012-11-23 11:00:00
5 | event name | 2012-11-24 08:00:00
etc.
I can do a strtotime of the dates and a date command to match.
If you first fetch the data (or if it is sorted if you first fetch the first data), then you can just iterate over the data when you match the hour/time that you iterate over to draw the table.
As an example, I've chosen to display only one column that represents 5 hours (1-5) of which some can be matched. Those that are matched, are stored in an array and made available as an iterator (ArrayIterator):
$data = [3,5];
$datas = new ArrayIterator($data);
$datas->rewind();
Matched hours are represented with 1, unmatched ones with 0:
echo "+---+---+\n";
foreach(range(1, 5) as $hour)
{
if ($hasData = ($datas->valid() and $datas->current() === $hour)) {
$datas->next();
}
$hasData = (int) $hasData;
echo "| $hour | $hasData |\n";
echo "+---+---+\n";
};
Output:
+---+---+
| 1 | 0 |
+---+---+
| 2 | 0 |
+---+---+
| 3 | 1 |
+---+---+
| 4 | 0 |
+---+---+
| 5 | 1 |
+---+---+
This works perfectly if the data from the data is available as an iterator (often the case, for mysql_* you need to write you one) and if it is sorted.
Even this is only a single list here, for a table this works actually equally because a table is just a different form of representing the data.