Jump to content

[SOLVED] variables/date problem


john010117

Recommended Posts

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.

Link to comment
Share on other sites

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;
}

}

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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.