Jump to content

DISPLAYING HEADERS CORRECTLY


shaddf

Recommended Posts

i have this table:

+-----------+-----------+------+------------+-----------------------+-------+
| HomeScore | AwayScore | g_id | mo_th      | g_date                | venue |
+-----------+-----------+------+------------+-----------------------+-------+
|         0 |         0 |   27 | July  2017 | Thu 13th of Jul 04.00 | H     |
|         0 |         0 |   26 | July  2017 | Sun 9th of Jul 04.00  | A     |
|         1 |         0 |   23 | June  2017 | Sun 18th of Jun 04.30 | H     |
|         1 |         1 |   19 | June  2017 | Sun 4th of Jun 04.30  | A     |
|         0 |         1 |   17 | May  2017  | Thu 25th of May 04.30 | H     |
|         1 |         0 |   15 | May  2017  | Sun 21st of May 04.30 | A     |
|         1 |         1 |   11 | May  2017  | Sun 14th of May 04.30 | H     |
+-----------+-----------+------+------------+-----------------------+-------+
7 rows in set, 2 warnings (0.04 sec)


I would like to loop through the records and display a heading of the mo_th field once before listing all the
corresponding homescore ,away score and venue below it.Like so:
MAY 2017
-HOMESCORE:0, AWAYSCORE:1
.
.
JUNE 2017
-HOMESCORE:1,AWAYSCORE:0
.
.

HOW CAN I DO THIS USING PHP

Link to comment
Share on other sites

1. Stop storing the dates as strings. It's horrible. I want to gouge my eyes out. Use actual DATETIMEs.

 

+-----------+-----------+------+---------------------+-------+
| HomeScore | AwayScore | g_id | g_date              | venue |
+-----------+-----------+------+---------------------+-------+
|         0 |         0 |   27 | 2017-07-13 16:00:00 | H     |
|         0 |         0 |   26 | 2017-07-09 16:00:00 | A     |
|         1 |         0 |   23 | 2017-06-18 16:30:00 | H     |
|         1 |         1 |   19 | 2017-06-04 16:30:00 | A     |
|         0 |         1 |   17 | 2017-05-25 16:30:00 | H     |
|         1 |         0 |   15 | 2017-05-21 16:30:00 | A     |
|         1 |         1 |   11 | 2017-05-14 16:30:00 | H     |
+-----------+-----------+------+---------------------+-------+
2. Make a query to return all the records you want sorted by g_date.

3. Determine the month label in your code (DateTime or strtotime + date) or in the query (DATE_FORMAT).

4. In the code, keep track of the previous month label.

$prevmonth = "";
foreach ($rows as $row) {
	$thismonth = /* month label from $row */;
5. If the label changed then output it and update.

if ($thismonth != $prevmonth) {
	echo "<h2>{$thismonth}</h2>\n";
	$prevmonth = $thismonth;
}
If you're using more HTML markup than that and can't figure out how to make it work, post your code and the output you're trying to get.

moth.jpg

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.