I have a data table which contains hundreds of records and each record has a location id.
What I am trying to do is echo out all the records with a line break when the location id changes.
The SQL statement is:
SELECT LocationID, deviceName, companyname, record_ID,location, location,orderby FROM FIDS_Hardware WHERE deviceTypeCode = 5 AND monitor = 1 ORDER BY hotelID ASC, orderby ASC
The code I am using to produce the table may look strange but it is how I want it to work.
$Limit2 = 5;
$Count2 = 0;
echo "<table border='0' width='100%' cellspacing='2'>";
while ($row_ConfMon = mysqli_fetch_assoc($ConfMon)) {
$locationid = $row_ConfMon['hotelID'];
$RoomNo2 = $row_ConfMon['deviceName'];
$companyname = $row_ConfMon['companyname'];
$recordID = $row_ConfMon['record_ID'];
$location = $row_ConfMon['location'];
$DownDate = date("d-m-y", $row_ConfMon['uptime']);
$DownTime = date("H:i", $row_ConfMon['uptime']);
if($row_ConfMon['NoDevice'] == 1) {
if($Count2 < $Limit2) {
if($Count2 == 0) {
echo "<tr>";
}
echo "<a href="confmon.php" target="_self"><td class="roomviewpurple" valign="top"><div class="coltitle"><a href="confmon_hist.php?recordID=$recordID" target="_self">$companyname</a></div> <div class="confmontime2">$location<br />$RoomNo2<br /></div><span class="confmontime">$DownDate<br /><div class="confmontime2">$DownTime</div><br /></span></td></a>";
} else {
$Count2 = 0;
echo "</tr><tr><a href="confmon.php" target="_self"><td class="roomviewpurple" valign="top"><div class="coltitle"><a href="confmon_hist.php?recordID=$recordID" target="_self">$companyname</a></div><div class="confmontime2">$location<br />$RoomNo2<br /></div><span class="confmontime">$DownDate<br /><div class="confmontime2">$DownTime</div><br /></span></td></a>";
}
$Count2++;
} elseif($row_ConfMon['MobileDisplay'] == 1 && $row_ConfMon['messageCount'] == 2) {
if($Count2 < $Limit2) {
if($Count2 == 0) {
echo "<tr>";
}
echo "<a href="confmon.php" target="_self"><td class="roomviewamber" valign="top"><div class="coltitle"><a href="confmon_hist.php?recordID=$recordID" target="_self">$companyname</a></div> <div class="confmontime2">$location<br />$RoomNo2<br /></div><span class="confmontime">$DownDate<br /><div class="confmontime2">$DownTime</div><br /></span></td></a>";
} else {
$Count2 = 0;
echo "</tr><tr><a href="confmon.php" target="_self"><td class="roomviewamber" valign="top"><div class="coltitle"><a href="confmon_hist.php?recordID=$recordID" target="_self">$companyname</a></div><div class="confmontime2">$location<br />$RoomNo2<br /></div><span class="confmontime">$DownDate<br /><div class="confmontime2">$DownTime</div><br /></span></td></a>";
}
$Count2++;
} elseif($row_ConfMon['messageCount'] == 2) {
if($Count2 < $Limit2) {
if($Count2 == 0) {
echo "<tr>";
}
echo "<a href="confmon.php" target="_self"><td class="roomviewred" valign="top"><div class="coltitle"><a href="confmon_hist.php?recordID=$recordID" target="_self">$companyname</a></div> <div class="confmontime2">$location<br />$RoomNo2<br /></div><span class="confmontime">$DownDate<br /><div class="confmontime2">$DownTime</div><br /></span></td></a>";
} else {
$Count2 = 0;
echo "</tr><tr><a href="confmon.php" target="_self"><td class="roomviewred" valign="top"><div class="coltitle"><a href="confmon_hist.php?recordID=$recordID" target="_self">$companyname</a></div><div class="confmontime2">$location<br />$RoomNo2<br /></div><span class="confmontime">$DownDate<br /><div class="confmontime2">$DownTime</div><br /></span></td></a>";
}
$Count2++;
} else {
if($Count2 < $Limit2) {
if($Count2 == 0) {
echo "<tr>";
}
echo "<td class="roomviewgreen" valign="top"><div class="coltitle"><a href="confmon_hist.php?recordID=$recordID" target="_self">$companyname</a></div><div class="confmontime2">$location<br />$RoomNo2<br /></div><span class="confmontime">$DownDate<br /><div class="confmontime2">$DownTime</div><br /></span></td>";
} else {
$Count2 = 0;
echo "</tr><tr><td class="roomviewgreen" valign="top"><div class="coltitle"><a href="confmon_hist.php?recordID=$recordID" target="_self">$companyname</a></div><div class="confmontime2">$location<br />$RoomNo2<br /></div><span class="confmontime">$DownDate<br /><div class="confmontime2">$DownTime</div><br /></span></td></a>";
}
$Count2++;
}
}
echo "</tr></table>";
I currently echo all the records into a table but I would like to start a new line in the table when the location id changes, like.
This is what the output currently looks like.
This is what I would like it to look like.
Is this possible?
2
Answers
You probably want to do the space insertion in your PHP code — via a
foreach
loop, for example — rather than in the SQL. Is it possible to do this with SQL? Yes, but for something presentation-related like this, I would suggest just cycling through the rows and adding the empty<tr>
tags on the PHP end. It will almost certainly be a lot cleaner, and it keeps the correct behaviors on the correct layers of the application.It should just be a case of looping through the data, and keeping track of what Location ID was used in the previous row, so you can compare it to the current one, and then decide whether to add an extra line or not.
P.S. There’s also a lot of repeated code in your original, so I took the liberty of removing the repetition – changing the logic around where you create new rows to just do that once, and also using variables to represent what changes in the different
echo
statements (which is only a class name, and the addition of an extra table row) so you only need to state most of the HTML once, and lastly refactoring it to get rid of all the escape characters which were cluttering it up too.Oh an your HTML table was technically malformed because you didn’t put any
<td>
cells into the row.For that reason, let’s take this in stages. This first version is equivalent to producing the output that you have now:
Live demo: https://3v4l.org/HQqL3
Now this next one should do the job for starting a new row when the location field has changed. As you can see, I’ve added a variable to keep track of the location ID from the previous row, so we can compare it, and also changed the point where it decides whether to end the row and reset the column count – it’s delayed until we’re processing the start of the next row, so there’s the opportunity to compare the location IDs. As per your comment below, it also adds another row for spacing, when changing location ID:
Live demo: https://3v4l.org/KRiU1