Jump to content

Dynamic Links


john010117

Recommended Posts

On my index page, I have a news section. No php needed for that. But I want the website to automatically archive old news when the next day comes. So, if I type in index.php, it should automatically get the news for today (but I'm typing up the news). Sorry if this doesn't make sense. I want to have something like this (see the date at the top and the arrows?). How do you do that?

Link to comment
Share on other sites

If all of your news items are in a database, you can use PHP to determine today's date and a SQL query to only grab the headlines for news items that are from the current day (or after some arbitrary hour like today at 12AM).

SELECT newsID, headline FROM news WHERE date > '2007-01-08 00:00:00'

(query may be different depending on your database)

Link to comment
Share on other sites

If all of your news items are in a database, you can use PHP to determine today's date and a SQL query to only grab the headlines for news items that are from the current day (or after some arbitrary hour like today at 12AM).
SELECT newsID, headline FROM news WHERE date > '2007-01-08 00:00:00'

(query may be different depending on your database)

Well, so far I have this code.
<?php// Connection information$db_host = "localhost"; $db_user = "**"; $db_pass = "**"; $db_name = "**"; // Connect to server$dbac = mysql_connect($db_host,$db_user,$db_pass); // Select databasemysql_select_db ($db_name) or die ("Cannot connect to database"); // Change is here added [ = '{$_GET['date']}' ] to end. Which get ?date=____ from url.$result = mysql_query("SELECT * News FROM news WHERE Date = '{$_GET['date']}' ORDER BY Time");if ($result && mysql_num_rows($result)) {	  $numrows = mysql_num_rows($result);	  $rowcount = 1;		 	  while ($row = mysql_fetch_assoc($result)) {					 while(list($var, $val) = each($row)) {			print "<B>$var</B>: $val<br />";		 }   		 print "<br />";		 ++$rowcount;	  }   }?>

I store the news by date, so whenever I type index.php?date=2007-01-06, it'll give me all the news for ONLY that day. Now I'm trying to figure out how to get today's news when I just type in index.php. Any ideas?

Link to comment
Share on other sites

You can implement something like this:

if ( !isset ( $_GET [ 'date_thingy'] ) ) // If ?date_thingy is not set{	// Make the query without WHERE clausule}else{	// Make query with WHERE clausule}

But.. isn't it a better idea if you make this with 24hrs ago. 'Cause when it's 00:01hrs, there is no news, that kinda looks weird.

Link to comment
Share on other sites

Or, check to see if the date is set in the URL (like the example from Kiwi). If it is set, then store that value in some $date variable. If it is not set, then set $date to today's date.Then use that $date in your WHERE clause.

Link to comment
Share on other sites

Check this reference:http://www.php.net/manual/en/ref.datetime.phpScroll down to see the list of date/time functions. You can use the time function to return the current timestamp, the date function will also default to the current time, and mktime will give you the timestamp for a date you specify.The only caveat is that it uses the server time. If your server is hosted on Christmas Island, then the time returned by these functions will be in their time zone, unless you specify otherwise.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...