Jump to content

[SOLVED] Display next 5 dates, greater than or equal to current date


selliott

Recommended Posts

Hello,

    I need some help adjusting my code to only display dates greater than or equal to the current date. And for it to limit the results to 5.  Please help!

 

<?php
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'On');
include('admin/functions.php');

$ConnString="DRIVER={SQL Server};SERVER=;DATABASE=";
$DB_User="";
$DB_Passwd="";
$QueryString="SELECT ID, Date, Title, Details, Results, Car FROM Schedule ORDER BY Date";
$Connect = odbc_connect( $ConnString, $DB_User, $DB_Passwd );  
$Result = odbc_exec( $Connect, $QueryString ); 

$nl = "\r\n"; 

    echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" . $nl; 
  echo "<ROOT>" . $nl; 

while($r = odbc_fetch_array( $Result ) )
{
        
    echo '<news ID="' . $r['ID'] . '" Title="' . char_replace($r['Title']) . '" Date="' . $r['Date'] . '" Car="' . $r['Car'] . '">' . $nl; 
    echo '<body><![CDATA[' . stripslashes2($r['Results']) . ']]></body>' . $nl; 
    echo '</news>' . $nl; 
}

echo "</ROOT>" . $nl;

odbc_free_result( $Result );
odbc_close( $Connect );
?>

Link to comment
Share on other sites

Thanks for the quick reply.  This won't limit the results to only dates that are greater than or equal to the current date though, will it?  Won't it just display all dates in the db?

 

no it will limit the result to 5 LIMIT 0,5

 

it will also select the 5 newest dates

Link to comment
Share on other sites

Assuming you're using the 'date' (or similar) data type for your "date" field, SQL will be able to order the dates in correct order as it can read the format. 'varchar' (or similar) may have mixed results though.. My brain isn't working this morning, I'm starting to think instead of "ASC" it may be "DESC", as you want the newest / highest first?? Then as burnside pointed out, "LIMIT 5" / "LIMIT 0, 5" will limit the records returned to 5.

 

Play around with it a bit..

 

Adam

Link to comment
Share on other sites

oops, I didn't mean to say "Won't it just display all dates".  I meant the 1st 5 in the db, regardless of the current date.

 

I had a little trouble getting this to display at first, then read around and it looks like I had to change it to read:

 

SELECT TOP 5 ID, Date, ...etc.

 

Since I'm on an SQL server.  I kept getting an error whenever I tried putting the LIMIT in there.  Using SELECT TOP 5 is only showing me the first 5 dates in the database though.  So, since the first date is 11/20/08, it's showing first....but I don't want it to show at all, since that date would be past.  So if the site was visited today, I'd want it to only display the next 5 dates that are of 11/15/08 or greater.

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.