Jump to content

Recommended Posts

Greetings PHP Gurus,

 

I'm not much of a "Programmer", I'm actually more of a "Code Tweaker", and I need help with an IF Statement. All I need this code to do is look in a text file and if the date in that file is past, then go to another file (or maybe even somewhere in the same file as the rest of the code is in) and display whatever line in it that has the next date in the future. For example, let's say a band has scheduled a tour. You make a file like this to list the event dates:

 

2-5-2007 = Las Vegas, Nevada

2-7-2007 = Los Angeles, California

2-9-2007 = San Francisco, California

2-11-2007 = Eugene, Oregon

2-13-2007 = Seattle, Washington

2-15-2007 = Missoula, Montana

 

And let's say it's February 10th, 2007. That would mean the last_date.txt file would contain the date 2-7-2007 in it. Thus the next date would be 2-11-2007, and the program should display Eugene, Oregon, because that is the city listed for that date. Then on the band's website they would have a section of the site for tour information, and at the top of the list of shows they moght have something like "The next show is ____________.", with the blank being whatever the output of this file is.

 

This is what I have so far. It's all known working code that I have played around with for the last year or so to do various things. Where you see the line "This is where I get stuck... What do I put here?", that's where I need help.

 

<?
//cfg_file to save last displayed event date.
$file='last_date.txt';

//get last event date.
$last_date=file_get_contents($file);

//check if today is greater than last_date.
$today=date("n-j-Y");
$output = "$today";

if($today>$last_date){
          
This is where I get stuck... What do I put here?

// Now that the last date event has passed, add the date to the last_date file.
if ($fp = fopen("$file", "w")) {
fputs($fp, $output, strlen($today));
fclose ($fp);
}
}

?>

 

Does anyone have any ideas on how I could do this? Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/36536-solved-need-if-statement-help/
Share on other sites

I am always open to suggestions... Otherwise I will never learn enough to be a Programmer, and will be a Code Tweaker forever. I am trying to learn to figure these things out for myself, and if I was closed minded it probably wouldn't ever happen. So by all means, make all the suggestion you like.  ;D

ok, well this code should work.  I've included some comments and hopefully this will help by example.

 

I've changed two main things...

 

1) The format of the file, not it's in YYYY-MM-DD format as that's the ISO 8601 standard, and easy to manipulate in PHP

2) We're only going to us one file.

 

<?php

/*
* Open the file that contains tour dates, the format should be as below
*
* YYYY-MM-DD|Location of gig
* 2007-06-14|London, England
**/
$raw_data = file('tourdates.txt');

/*
* Loop through each line, split into date and location and then place in
* a new array keyed by unix timestamp
**/
foreach ($raw_data as $line){
   list($date, $location) = explode('|', $line);
   $gigs[strtotime($date)] = $location;
}

// Place the gigs in date order
ksort($gigs);

// Get todays date and work out the net gig from it
$today = strtotime('today');
foreach ($gigs as $d => $l){
   if ($d > $today){
      $next = $d;
      break;
   }
}

// Echo when the next gig is, using the date() funtion to format to your liking
echo "The next gig is " . date("l jS F", $next). " in " . $gigs[$next];

?>

 

Regards

Huggie

I figured it out... I was using a " when I needed a '... I hate those things. They always give me grief.

 

It's all basically the same as how you originally wrote it, I just changed one line...

 

echo "<img src='banners/{$gigs[$next]}'>";

Then I made a bunch of images for it and dropped them in the ../banners folder. And it works like a charm. Thank you again. thumbsup.gif

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.