john010117 Posted April 10, 2007 Share Posted April 10, 2007 Ok, I have a problem. I have a code (mostly built from the ground-up by myself with a little help from others) to have a simple news system. The two main variables I have problems with are "Date" and "id". I so far have the script set up so that when a user types in "index.php?Date=whatever", it'll show all the news for that date. If a user types in "index.php?id=whatever", it only shows that specific news post. So far so good. Let me show you a part of the script first. $Date variable script: if($_GET['date']) { // Get date from _GET param or current date if not available $_GET['date'] = @trim(stripslashes($_GET['date'])); $date = ($_GET['date'] && !empty($_GET['date'])) ? date('Y-m-d', strtotime(trim($_GET['date']))) : date('Y-m-d'); $result = mysql_query('SELECT * FROM news WHERE Date = "' . $date . '" ORDER BY Time'); $curtime = time(); if ($result && mysql_num_rows($result)) { $numrows = mysql_num_rows($result); $rowcount = 1; while ($row = mysql_fetch_assoc($result)) { print "<b>{$row['Title']}</b><br /> {$row['News']}<br /> <span style=\"font-size:10px\"><b>Posted by:</b> {$row['Posted_by']} ({$row['Date']} {$row['Time']}) <a href=\"http://www.allaroundhalo.org/index.php?id={$row['id']}\">Permalink</a>)</span><br /><br />"; } print "<br />"; ++$rowcount; } } I need to have the script check today's date and get news for today if the "?date=" part isn't typed in the URL (ex: index.php). BUT I can't allow today's news to show when a user is requesting a specific date or id. Any suggestions? Any help will be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/46509-solved-variablesdate-problem/ Share on other sites More sharing options...
Glyde Posted April 10, 2007 Share Posted April 10, 2007 The easiest way: $date = isset($_GET['date']) && !empty($_GET['date']) ? date('Y-m-d', strtotime(trim($_GET['date']))) : date('Y-m-d'); // Query to get news from that date Quote Link to comment https://forums.phpfreaks.com/topic/46509-solved-variablesdate-problem/#findComment-226327 Share on other sites More sharing options...
john010117 Posted April 10, 2007 Author Share Posted April 10, 2007 That doesn't work. The script has to get today's date and post today's news if and only when the user has just typed "index.php" in. And it also shouldn't show today's news on any other date or id. Quote Link to comment https://forums.phpfreaks.com/topic/46509-solved-variablesdate-problem/#findComment-226332 Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 Try this: if($_GET['date']) { // Get date from _GET param or current date if not available $_GET['date'] = @trim(stripslashes($_GET['date'])); $date = ($_GET['date'] && !empty($_GET['date'])) ? date('Y-m-d', strtotime(trim($_GET['date']))) : date('Y-m-d'); $result = mysql_query('SELECT * FROM news WHERE Date = "' . $date . '" ORDER BY Time'); $curtime = time(); if ($result && mysql_num_rows($result)) { $numrows = mysql_num_rows($result); $rowcount = 1; while ($row = mysql_fetch_assoc($result)) { print "<b>{$row['Title']}</b><br /> {$row['News']}<br /> <span style=\"font-size:10px\"><b>Posted by:</b> {$row['Posted_by']} ({$row['Date']} {$row['Time']}) <a href=\"http://www.allaroundhalo.org/index.php?id={$row['id']}\">Permalink</a>)</span><br /><br />"; } print "<br />"; ++$rowcount; } } else { // IF no date selected, show todays date. $now = date("Y-m-d"); $result = mysql_query('SELECT * FROM news WHERE Date = "' . $now . '" ORDER BY Time'); $curtime = time(); if ($result && mysql_num_rows($result)) { $numrows = mysql_num_rows($result); $rowcount = 1; while ($row = mysql_fetch_assoc($result)) { print "<b>{$row['Title']}</b><br /> {$row['News']}<br /> <span style=\"font-size:10px\"><b>Posted by:</b> {$row['Posted_by']} ({$row['Date']} {$row['Time']}) <a href=\"http://www.allaroundhalo.org/index.php?id={$row['id']}\">Permalink</a>)</span><br /><br />"; } print "<br />"; ++$rowcount; } } Quote Link to comment https://forums.phpfreaks.com/topic/46509-solved-variablesdate-problem/#findComment-226335 Share on other sites More sharing options...
Glyde Posted April 10, 2007 Share Posted April 10, 2007 if (isset($_GET['id']) && is_numeric($_GET['id'])) { // Display news for the id } elseif (isset($_GET['date']) && !empty($_GET['date'])) { // Display news for the specified date } else { // Display news for today } Quote Link to comment https://forums.phpfreaks.com/topic/46509-solved-variablesdate-problem/#findComment-226336 Share on other sites More sharing options...
john010117 Posted April 10, 2007 Author Share Posted April 10, 2007 Thank you, Glyde. Your script worked. JSHINER, I appreciate it, but your way of coding it made it show that it showed today's news while the user requested a specific id/date. Thanks anyway. SOLVED! Quote Link to comment https://forums.phpfreaks.com/topic/46509-solved-variablesdate-problem/#findComment-226350 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.