Page 1 of 1

PHP function to only view info added after certain date

Posted: Sat May 02, 2009 6:03 pm
by leifsod
Hello all!

This is my first post here, and I'm brand new at PHP/MySQL, so please bare with me :)

I have a database with four columns: id, header, text and date, which I display with a while loop. Now, what I need to do is some kind of (additional?) loop to make only posts where the date variable is > today's date show. So, something like while ($date > $now), echo $row... how do I proceed?

Thanks in advance for any help!

// Leif

Re: PHP function to only view info added after certain date

Posted: Sat May 02, 2009 7:35 pm
by requinix
Just change the query:

Code: Select all

SELECT * FROM `table` WHERE "some date" >= `date`
That'll only return rows that are newer than (or as recent as) "some date". The string can be replaced with NOW() if you're looking at dates starting with today.

Re: PHP function to only view info added after certain date

Posted: Sun May 03, 2009 12:52 pm
by leifsod
Thanks for your quick reply, tasairis!

I actually managed to fix it (before having seen your suggestion) by adding an if- loop after the while-loop telling it only to display rows if the variable "date" is > the current date :) . I will definitely use your tip somewhere else, though!

// Leif

Re: PHP function to only view info added after certain date

Posted: Sun May 03, 2009 1:45 pm
by ldougherty
Just an FYI that is bad coding practice that you should avoid. If you are only going to use the information where "date" is > "current date" then you should use the method supplied by tasairis. The way you are doing so you are pulling extra information on a query that you do not need and then having nested while/if/etc etc is just extra CPU and memory your server will use.

Re: PHP function to only view info added after certain date

Posted: Sun May 03, 2009 4:24 pm
by leifsod
Really? Had no idea! The only programming I've done is the MATLAB assignments at Uni, and there, noone really cared if we followed any practice or not...:P

I will definitely try the other way to make it as good as possible! Thanks a bunch!

Best Regards // Leif