Ax Slinger Posted January 31, 2007 Share Posted January 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/36536-solved-need-if-statement-help/ Share on other sites More sharing options...
HuggieBear Posted January 31, 2007 Share Posted January 31, 2007 Does it have to be done this way or are you open to suggestion? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/36536-solved-need-if-statement-help/#findComment-173979 Share on other sites More sharing options...
Ax Slinger Posted January 31, 2007 Author Share Posted January 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/36536-solved-need-if-statement-help/#findComment-173984 Share on other sites More sharing options...
HuggieBear Posted January 31, 2007 Share Posted January 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/36536-solved-need-if-statement-help/#findComment-174002 Share on other sites More sharing options...
Ax Slinger Posted February 1, 2007 Author Share Posted February 1, 2007 Wow... Thanks to you I may have some hair left after all. I've pulled a good bit it out this week. Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/36536-solved-need-if-statement-help/#findComment-174185 Share on other sites More sharing options...
Ax Slinger Posted February 5, 2007 Author Share Posted February 5, 2007 Hey HuggieBear, I really appreciate this code. It works perfectly... But I do have one more question for ya. Is it possible to make it display an image instead of a text message? Quote Link to comment https://forums.phpfreaks.com/topic/36536-solved-need-if-statement-help/#findComment-177808 Share on other sites More sharing options...
HuggieBear Posted February 6, 2007 Share Posted February 6, 2007 Yes, that's possible. You could add the path of the image to the file too and use it like that. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/36536-solved-need-if-statement-help/#findComment-178061 Share on other sites More sharing options...
Ax Slinger Posted February 7, 2007 Author Share Posted February 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/36536-solved-need-if-statement-help/#findComment-178850 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.