selliott Posted November 25, 2008 Share Posted November 25, 2008 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 ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/134173-solved-display-next-5-dates-greater-than-or-equal-to-current-date/ Share on other sites More sharing options...
Adam Posted November 25, 2008 Share Posted November 25, 2008 Perhaps: $QueryString="SELECT ID, Date, Title, Details, Results, Car FROM Schedule ORDER BY Date ASC LIMIT 5"; Quote Link to comment https://forums.phpfreaks.com/topic/134173-solved-display-next-5-dates-greater-than-or-equal-to-current-date/#findComment-698454 Share on other sites More sharing options...
selliott Posted November 25, 2008 Author Share Posted November 25, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/134173-solved-display-next-5-dates-greater-than-or-equal-to-current-date/#findComment-698459 Share on other sites More sharing options...
beansandsausages Posted November 25, 2008 Share Posted November 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/134173-solved-display-next-5-dates-greater-than-or-equal-to-current-date/#findComment-698493 Share on other sites More sharing options...
Adam Posted November 25, 2008 Share Posted November 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/134173-solved-display-next-5-dates-greater-than-or-equal-to-current-date/#findComment-698495 Share on other sites More sharing options...
selliott Posted November 25, 2008 Author Share Posted November 25, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134173-solved-display-next-5-dates-greater-than-or-equal-to-current-date/#findComment-698525 Share on other sites More sharing options...
beansandsausages Posted November 25, 2008 Share Posted November 25, 2008 hm.... try select from `foo` WHERE date => NOW() LIMIT 0,5 that should select the 5 dates = or greater that date un tested and might not work but thats general idea, Quote Link to comment https://forums.phpfreaks.com/topic/134173-solved-display-next-5-dates-greater-than-or-equal-to-current-date/#findComment-698533 Share on other sites More sharing options...
selliott Posted November 25, 2008 Author Share Posted November 25, 2008 Awesome, thanks for the help guys! I ended up having to use: SELECT TOP 5 ID, Date, Title, Car FROM Schedule WHERE Date >= GetDate() ORDER BY Date ASC "; Quote Link to comment https://forums.phpfreaks.com/topic/134173-solved-display-next-5-dates-greater-than-or-equal-to-current-date/#findComment-698542 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.