Jump to content

Echo <h1></h1> when date changes


MargateSteve

Recommended Posts

Sorry for the topic title not explaining much, but I did not now how to word it!

 

I was previous given some code at http://www.phpfreaks.com/forums/php-coding-help/streamlining-dreamweaver-generated-php-code/msg1459677/#msg1459677 that created a header every time a crtieria changed, in this case a players position.

 

I have tried to tailor this to another page but have got it wrong somewhere. In this instance I was a change of date to be the determining factor. For example, with what I have tried so far, the output is

2011-04-02

Lowestoft Town v Tonbridge Angels

 

2011-04-02

Tooting & Mitcham United v Maidstone United

 

2011-04-02

Wealdstone v Concord Rangers

 

2011-04-09

Hastings United v Margate

 

2011-04-09

AFC Hornchurch v Carshalton Athletic

 

2011-04-09

Billericay Town v Aveley

 

when I actually want

 

2011-04-02

Lowestoft Town v Tonbridge Angels

Tooting & Mitcham United v Maidstone United

Wealdstone v Concord Rangers

 

2011-04-09

AFC Hornchurch v Carshalton Athletic

Billericay Town v Aveley

 

I have tried to adjust the code to reflect the different query etc but something is missing.

 

Thinking further ahead, there will also be a second criteria, in that games in different competitions could be played on the same day so I would also want to group the output by the competition ('comp' in the table) to get

 

2011-04-02

League

Lowestoft Town v Tonbridge Angels

Tooting & Mitcham United v Maidstone United

Cup

Wealdstone v Concord Rangers

 

2011-04-09

League

AFC Hornchurch v Carshalton Athletic

Cup

Billericay Town v Aveley

 

I would imagine that would just mean drilling down one more level in the code once I get this bit right.

 

The tables, with a sample of data are

CREATE TABLE `all_games` (
  `all_games_id` int(11) NOT NULL auto_increment,
  `date` date default NULL,
  `time` time default NULL,
  `comp` int(11) NOT NULL default '1',
  `round` tinyint(4) default NULL,
  `replay` char(1) default NULL,
  `home_team` int(11) NOT NULL default '1',
  `away_team` int(11) NOT NULL default '1',
  `home_goals` int(11) default NULL,
  `away_goals` int(11) default NULL,
  `home_ht` int(11) default NULL,
  `away_ht` int(11) default NULL,
  `attendance` int(11) default NULL,
  PRIMARY KEY  (`all_games_id`)
) TYPE=MyISAM AUTO_INCREMENT=732 ;


INSERT INTO `all_games` VALUES (700, '2011-04-23', '00:00:00', 1, NULL, '', 11, 20, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (701, '2011-04-23', '00:00:00', 1, NULL, '', 12, 13, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (702, '2011-04-23', '00:00:00', 1, NULL, '', 15, 22, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (703, '2011-04-23', '00:00:00', 1, NULL, '', 21, 7, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (704, '2011-04-25', '00:00:00', 1, NULL, '', 1, 14, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (705, '2011-04-25', '00:00:00', 1, NULL, '', 4, 6, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (706, '2011-04-25', '00:00:00', 1, NULL, '', 8, 15, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (707, '2011-04-25', '00:00:00', 1, NULL, '', 13, 2, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (708, '2011-04-25', '00:00:00', 1, NULL, '', 16, 21, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (709, '2011-04-25', '00:00:00', 1, NULL, '', 17, 3, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (710, '2011-04-25', '00:00:00', 1, NULL, '', 18, 9, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (711, '2011-04-25', '00:00:00', 1, NULL, '', 19, 5, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (712, '2011-04-25', '00:00:00', 1, NULL, '', 20, 12, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (713, '2011-04-25', '00:00:00', 1, NULL, '', 22, 11, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (714, '2011-04-30', '00:00:00', 1, NULL, '', 14, 18, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (715, '2011-04-30', '00:00:00', 1, NULL, '', 2, 19, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (716, '2011-04-30', '00:00:00', 1, NULL, '', 3, 22, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (717, '2011-04-30', '00:00:00', 1, NULL, '', 5, 13, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (718, '2011-04-30', '00:00:00', 1, NULL, '', 6, 8, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (719, '2011-04-30', '00:00:00', 1, NULL, '', 9, 17, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (720, '2011-04-30', '00:00:00', 1, NULL, '', 11, 7, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (721, '2011-04-30', '00:00:00', 1, NULL, '', 12, 1, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (722, '2011-04-30', '00:00:00', 1, NULL, '', 15, 20, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `all_games` VALUES (723, '2011-04-30', '00:00:00', 1, NULL, '', 21, 4, NULL, NULL, NULL, NULL, NULL);



CREATE TABLE `competitions` (
  `comp_id` int(11) NOT NULL auto_increment,
  `comp_name` varchar(45) default NULL,
  `comp_short_name` varchar(4) default NULL,
  `logo` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`comp_id`)
) TYPE=MyISAM AUTO_INCREMENT=8 ;


INSERT INTO `competitions` VALUES (1, 'Isthmian League Premier Division', 'ILP', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/tRymanLeague.png');
INSERT INTO `competitions` VALUES (2, 'FA Cup', 'FAC', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/facup.png');
INSERT INTO `competitions` VALUES (3, 'FA Trophy', 'FAT', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/fattophy.png');
INSERT INTO `competitions` VALUES (4, 'Kent Senior Cup', 'KSC', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/kentseniorcup.png');
INSERT INTO `competitions` VALUES (5, 'Isthmian League Cup', 'ILC', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/isthmianleaguecup.png');
INSERT INTO `competitions` VALUES (6, 'Friendly', 'FR', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/friendly.png');
INSERT INTO `competitions` VALUES (7, 'Kent Messenger Cup', 'KMC', '');



CREATE TABLE `teams` (
  `team_id` int(11) NOT NULL auto_increment,
  `team_name` varchar(45) default NULL,
  `short_name` varchar(5) default NULL,
  `badge` varchar(255) default NULL,
  `year_formed` int(11) default NULL,
  `nickname` varchar(255) default NULL,
  `previous_names` varchar(255) default NULL,
  `website` varchar(255) default NULL,
  `twitter` varchar(255) default NULL,
  `facebook` varchar(255) default NULL,
  `telephone_number` varchar(255) default NULL,
  `chairman` varchar(255) default NULL,
  `manager` varchar(255) default NULL,
  `home_colours` varchar(255) default NULL,
  `away_colours` varchar(255) default NULL,
  `stadium` int(11) default NULL,
  `2009/10` varchar(255) default NULL,
  PRIMARY KEY  (`team_id`)
) TYPE=MyISAM AUTO_INCREMENT=74 ;


INSERT INTO `teams` VALUES (1, 'Aveley', 'Ave', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/aveley.png', 1927, 'The Millers', 'None', 'http://www.aveleyfc.net/', NULL, NULL, '01708865940', 'Graham Gennings', 'Alan Kimble', 'Black & Blue/Blue/Blue', 'Black & White/White/White', 3, 'Isthmian League Premier Division, 3rd');
INSERT INTO `teams` VALUES (2, 'Billericay Town', 'Bill', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/billericaytown.png', 1880, 'Ricay, The Blues', 'None', 'http://www.billericaytownfc.co.uk/', 'http://twitter.com/BTFC', 'http://www.facebook.com/billericaytownfc', '01277652188', 'Steve Kent', 'Craig Edwards', 'Blue/White/Blue', 'White/Black/White', 2, 'Isthmian Premier, 13th');
INSERT INTO `teams` VALUES (3, 'Bury Town', 'Bur', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/burytown.png', 1872, 'The Blues', 'Bury St Edmunds, Bury United', 'http://www.burytownfc.co.uk', NULL, NULL, '01277999999', 'Russell Ward', 'Richard Wilkins', 'Blue & Red/Blue/Blue', 'White/Black/White', 10, 'Southern League Division One Midlands, 1st');
INSERT INTO `teams` VALUES (4, 'Canvey Island', 'Can', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/canveyisland.png', 1926, 'The Gulls', 'None', 'http://www.canveyislandfc.com', 'http://twitter.com/CIFC', NULL, NULL, 'George Frost', 'John Batch', 'Yellow/Blue/White', 'White/White/White', 11, 'Isthmian League Premier Division, 16th');
INSERT INTO `teams` VALUES (5, 'Carshalton Athletic', 'Car', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/carshaltonathletic.png', 1905, 'The Robins', 'Mill Lane Misson', 'http://www.carshaltonathletic.co.uk/', 'http://twitter.com/carshaltonath', NULL, '02086428658', 'Harry Driver', 'Mark Butler', 'Red/Red/Red', 'Maroon/White/Maroon', 9, 'Isthmian League Premier Division, 17th');
INSERT INTO `teams` VALUES (6, 'Concord Rangers', 'Con', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/concordrangers.png', 1967, 'Beach Boys', 'None', 'http://www.concordrangers.co.uk/', 'http://twitter.com/ConcordRangers', NULL, '01268691780', 'Antony Smith', 'Danny Scopes / Danny Cowley', 'Yellow/Blue/Blue', 'TBA', 12, NULL);
INSERT INTO `teams` VALUES (7, 'Cray Wanderers', 'Cray', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/craywanderers.png', 1860, 'The Wands', 'None', 'http://www.craywands.co.uk', 'http://twitter.com/craywanderers', NULL, '02084605291', 'Gary Hillman', 'Ian Jenkins', 'Amber/Black/Amber', 'Blue/White/White', 13, 'Isthmian League Premier Division, 15th');
INSERT INTO `teams` VALUES (8, 'Croydon Athletic', 'Croy', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/croydonathletic.png', 1986, 'The Rams', 'Wandsworth & Norwood', 'http://croydonathletic.net', NULL, NULL, '0208664834', 'TBA', 'Dave Garland & Bob Langford', 'Maroon & White/Maroon/Maroon', 'Yellow/Blue/Blue', 14, 'Isthmian League Division One South, 1st');
INSERT INTO `teams` VALUES (9, 'Folkestone Invicta', 'Fol', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/folkestoneinvicta.png', 1936, 'Invicta, The Seasiders', 'None', 'http://www.folkestoneinvicta.co.uk/', 'http://twitter.com/FIFC', NULL, '01303257461', 'TBA', 'Neil Cugley', 'Amber/Black/Amber', 'Red/White/Red', 15, 'Isthmian League Division One South, 2nd');
INSERT INTO `teams` VALUES (10, 'Grays Athletic', 'Gra', 'N/A', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `teams` VALUES (11, 'Harrow Borough', 'Har', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/harrowborough.png', 1933, 'The Boro, The Reds', 'Roxoinian, Harrow Town', 'http://www.harrowboro.com/', 'http://twitter.com/harrowboro_fc', NULL, '08706091959', 'Peter Rogers', 'David Howell', 'Red/Red/Red', 'White/White/Black', 16, 'Isthmian League Premier Division, 14th');
INSERT INTO `teams` VALUES (12, 'Hastings United', 'Has', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/hastingsunited.png', 1894, 'The Arrows, The U''s', 'Hastings Town, Hastings & St. Leonards', 'http://www.hastingsunitedfc.co.uk/', 'http://twitter.com/hastingsufc', NULL, '01424444635', 'David Walter', 'Tony Dolby', 'Maroon/Maroon/Maroon', 'White/White/White', 17, 'Isthmian League Premier Division, 7th');
INSERT INTO `teams` VALUES (13, 'Hendon', 'Hen', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/hendon.png', 1908, 'The Greens, The Dons', 'Christ Church Hampstead, Hampstead Town, Hampstead, Golders Green', 'http://www.hendonfc.net/', 'http://twitter.com/HendonFC', NULL, '02089083553', 'TBA', 'Gary McCann', 'Green/Green/Green', 'Blue/Blue/Blue', 18, 'Isthmian League Premier Division, 10th');
INSERT INTO `teams` VALUES (14, 'AFC Hornchurch', 'AFC', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/afchornchurch.png', 1923, 'The Urchins', 'Upminster Wanderers, Hornchurch & Upminster', 'http://www.afchornchurch.com/', 'http://twitter.com/AFCHornchurch', 'http://www.facebook.com/AFCHornchurch', '01708220080', 'TBA', 'Colin McBride', 'Red & White/Black/Red', 'Blue & White/Blue/Blue', 1, 'Isthmian League Premier Division, 9th');
INSERT INTO `teams` VALUES (15, 'Horsham', 'Hor', 'http://www.margate-fc.com/edit/news/data/upimages/images/Badges/horsham.png', 1871, 'The Hornets', 'None', 'http://www.hornetsreview.co.uk/', NULL, NULL, '01403266888', 'Kevin Borrett', 'John Maggs', 'Yellow & Green/Yellow/Yellow', 'Blue/Black/White', 19, 'Isthmian League Premier Division,11th');

The query is

$all_games = mysql_query("SELECT *, HT.team_name as HomeTeam, VT.team_name as AwayTeam, COMP.comp_short_name as CP
FROM all_games
JOIN teams as HT ON all_games.home_team = HT.team_id
JOIN teams as VT ON all_games.away_team = VT.team_id 
JOIN competitions as COMP ON all_games.comp = COMP.comp_id  ORDER BY date ASC 
");
if (!$all_games) {
    die("Query to show fields from table failed");

And the code that echoes the right values, but does not seperate the the dates is

if($all_games)
{
if(mysql_num_rows($all_games)) 
{
    $lastdate = 0;
    while ($row_all_games = mysql_fetch_assoc($all_games))
{

// when the date changes output a new heading
            if($lastdate = $row_all_games['date'])
            {
                echo "<h1>".strtoupper($row_all_games['date'])."</h1>";
                $lastdate = $row_all_games['date'];
               
            }
    echo $row_all_games['HomeTeam']." v ".$row_all_games['AwayTeam'];
        }
    }

  else
    {
        echo "No results!";
    }
}

 

I must admit that I have tried a few variations but the nearest I have got is for the page to echo "No results!"

 

I may be a fraction off getting it right or a million miles away so if anyone can give any pointers I will be very grateful.

 

Thanks in advance

Steve

Link to comment
https://forums.phpfreaks.com/topic/225091-echo-when-date-changes/
Share on other sites

Not sure I completely get what you're saying here, but this will get today's date:

 

$today = date("Y-m-d");

 

as 2011-01-20

 

Then compare your stored date (i think its $row_all_games['date']) to today's date and if they are equal do whatever it is that is date dependent.

 

Make sense? Ill try to poke through your code and see if I can understand it better, I just glanced at it quickly. Hope thats what you're trying to do..  :D

if($lastdate = $row_all_games['date'])

 

^^^ You buggered up that line by changing the original != comparison operator to an = assignment operator. You are assigning $row_all_games['date'] to the variable $lastdate, not comparing the two.

if($lastdate = $row_all_games['date'])

 

^^^ You buggered up that line by changing the original != comparison operator to an = assignment operator. You are assigning $row_all_games['date'] to the variable $lastdate, not comparing the two.

 

Good catch

if($lastdate = $row_all_games['date'])

 

^^^ You buggered up that line by changing the original != comparison operator to an = assignment operator. You are assigning $row_all_games['date'] to the variable $lastdate, not comparing the two.

 

I knew that it would be something to do with my stupid chunky fingers!! I looked and looked but could not find the error!

 

Thanks as always

Steve

Apologies for unsolving this but I thought I would be able to solve the next bit on my own, but couldn't and thought it would be rude to start another thread.

 

Everything worked fine until I tried to add the extra separation by competition that I mentioned.

 

I have added a 'comp_id ASC' ORDER BY to the query and have amended the code as such

if ($all_games)
{
if(mysql_num_rows($all_games)) 
{
    $lastdate = 0;
            while ($row_all_games = mysql_fetch_assoc($all_games))
            {

// when the date changes output a new heading
            if($lastdate != $row_all_games['date'])
            {
                echo "<br/><b>".strtoupper($row_all_games['date'])."</b><br/>";
                 $lastdate = $row_all_games['date'];
               
            };


// when the competitione changes output a new heading			
$lastcomp = 0;
    if($lastcomp != $row_all_games['comp'])
            {
                echo "<b>".$row_all_games['comp_name']."</b><br/>";
                 $lastcomp = $row_all_games['comp'];
               
            };

    echo $row_all_games['HomeTeam']." v ".$row_all_games['AwayTeam']."<br/>";
        }
    }

  else
    {
        echo "No results!";
    }
}

 

Once again I really thought that I had got it dead right but it shows the competition above every game (http://www.margate-fc.com/content/test/stats/games2.php?date1=2011-04-02&date2=2011-04-25&teams=&submit=Filter).

 

I have run through the processing theory in my head, checked for typos, have double-checked placement of curly brackets and semi-colons and moved things around to see what the problem might be. I have even tried to enclose the whole new competitions bit and tried to combine it with the original as an if/else but cannot get anything to work.

 

What have I done wrong this time?!?!?!?

 

Thanks as always

Steve

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.