Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13874

Re: How to loop through the dates?

From Norman Peelman <npeelman@cfl.rr.com>
Newsgroups comp.lang.php
Subject Re: How to loop through the dates?
Date 2014-06-15 20:59 -0400
Organization A noiseless patient Spider
Message-ID <lnlfif$fcd$1@dont-email.me> (permalink)
References <ywx2wjj7uwar$.105qt7ftnicew$.dlg@40tude.net>

Show all headers | View raw


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?-

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

How to loop through the dates? richard <noreply@example.com> - 2014-06-15 12:11 -0400
  Re: How to loop through the dates? Luuk <luuk@invalid.lan> - 2014-06-15 18:38 +0200
    Re: How to loop through the dates? Denis McMahon <denismfmcmahon@gmail.com> - 2014-06-15 17:43 +0000
      Re: How to loop through the dates? Luuk <luuk@invalid.lan> - 2014-06-15 20:03 +0200
  Re: How to loop through the dates? Denis McMahon <denismfmcmahon@gmail.com> - 2014-06-15 17:40 +0000
    Re: How to loop through the dates? richard <noreply@example.com> - 2014-06-15 13:59 -0400
      Re: How to loop through the dates? Luuk <luuk@invalid.lan> - 2014-06-15 20:27 +0200
      Re: How to loop through the dates? Denis McMahon <denismfmcmahon@gmail.com> - 2014-06-15 19:14 +0000
      Re: How to loop through the dates? Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-06-15 21:39 +0000
      Re: How to loop through the dates? Arno Welzel <usenet@arnowelzel.de> - 2014-06-17 08:12 +0200
    Re: How to loop through the dates? Luuk <luuk@invalid.lan> - 2014-06-15 20:01 +0200
      Re: How to loop through the dates? Denis McMahon <denismfmcmahon@gmail.com> - 2014-06-15 19:07 +0000
        Re: How to loop through the dates? Luuk <luuk@invalid.lan> - 2014-06-15 21:58 +0200
          Re: How to loop through the dates? Denis McMahon <denismfmcmahon@gmail.com> - 2014-06-16 00:55 +0000
        Re: How to loop through the dates? richard <noreply@example.com> - 2014-06-16 13:02 -0400
          Re: How to loop through the dates? Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-06-16 17:57 +0000
    Re: How to loop through the dates? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-06-15 19:14 -0400
      Re: How to loop through the dates? richard <noreply@example.com> - 2014-06-16 12:26 -0400
        Re: How to loop through the dates? Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-06-16 16:39 +0000
        Re: How to loop through the dates? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-06-16 14:14 -0400
        Re: How to loop through the dates? Arno Welzel <usenet@arnowelzel.de> - 2014-06-17 08:18 +0200
  Re: How to loop through the dates? Tim Streater <timstreater@greenbee.net> - 2014-06-15 20:30 +0100
    Re: How to loop through the dates? Luuk <luuk@invalid.lan> - 2014-06-15 22:01 +0200
      Re: How to loop through the dates? Tim Streater <timstreater@greenbee.net> - 2014-06-15 21:34 +0100
        Re: How to loop through the dates? Luuk <luuk@invalid.lan> - 2014-06-15 23:32 +0200
        Re: How to loop through the dates? richard <noreply@example.com> - 2014-06-16 12:19 -0400
          Re: How to loop through the dates? Norman Peelman <npeelman@cfl.rr.com> - 2014-06-16 20:44 -0400
  Re: How to loop through the dates? Norman Peelman <npeelman@cfl.rr.com> - 2014-06-15 20:59 -0400

csiph-web