Re: How to loop through the dates? [message #186136 is a reply to message #186117] |
Mon, 16 June 2014 00:59 |
Norman Peelman
Messages: 126 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 06/15/2014 12:11 PM, richard wrote:
> http://mroldies.net/radio/tracker2.php
>
>
> I've got my report generator almost completed.
> I only need to know how I can loop through the dates.
> In BASIC, this is no big deal.
>
> As a synopsis,
> The table "tracker" is consulted for data.
> Data is ordered by date.
> Data is now transferred to the array $master.
> From $master, data is outputted as desired.
> In this case, each song played on a given date is listed.
>
>
>
>
> $query = "SELECT * FROM tracker ORDER BY date";
> $result = mysqli_query($con, $query);
> @$num_results = mysqli_num_rows($result);
>
> /*Loop through each row and display records */
> for($i=0; $i<$num_results; $i++) {
> $row = mysqli_fetch_assoc($result);
>
> $master[$i]['date']=$row['date'];
> $master[$i]['songID']=$row['songID'];
> $master[$i]['track']=$row['track'];
> $master[$i]['artist']=$row['artist'];
> $master[$i]['hits']=$row['hits'];
> }
>
> for($i=0;$i<$num_results;$i++){
>
> print $master[$i]['date'];
> echo "<br>";
> }
> echo "<table border=\"1\">";
>
> $newdate=$master[0]['date'];
>
> $ct=0;
> print "<tr><td>".$master[$ct]['date']."</td></tr>";
> while ($ct<$num_results){
>
> if ($master[$ct]['date']==$newdate) {
> print "<tr><td>".$master[$ct]['track']."</td></tr>";
> }
>
> $ct++;
> }
>
> echo "</table>";
>
>
> mysqli_close($con);
>
Since you don't appear to be doing anything with $master, why not
just output you table directly?
$query = "SELECT `date`, songID, track, artist, hits FROM tracker
ORDER BY `date` ASC";
$result = mysqli_query($con, $query);
if (!$result)
{
echo "The query failed with the following MySQL error:<br>";
echo mysqli_errno($con) . ": " . mysqli_error($con);
mysqli_close($con);
exit;
} else {
$num_results = mysqli_num_rows($result);
if ($num_results == NULL)
{
echo "No records to display!";
exit;
}
}
/*Loop through each row and display records */
$last_date = "nomatch";
$tmp = 0;
for($i=1; $i<$num_results; $i++)
{
$row = mysqli_fetch_assoc($result);
if ($row['date'] <> $last_date)
{ // ---------- new table ----------
if ($tmp == 1)
{ // ---------- end last table ----------
echo "</table>\n\r";
echo "<br>\n\r";
$tmp = 0;
}
if ($tmp == 0)
{ // ---------- start new table ----------
echo "<table border='1'>\n\r";
echo " <th colspan='3'>" . $row['date'] . "</th>\n\r";
$last_date = $row['date'];
$tmp = 1;
}
}
if ($row['date'] == $last_date)
{ // ---------- cell data ----------
echo " <tr>\n\r";
echo " <td>" . $row['songID'] . "</td>\n\r";
echo " <td>" . $row['track'] . "</td>\n\r";
echo " <td>" . $row['artist'] . "</td>\n\r";
echo " <td>" . $row['hits'] . "</td>\n\r";
echo " </tr>\n\r";
}
}
echo "</table>\n\r";
echo "<br>\n\r";
mysqli_close($con);
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
|
|
|