Reply
Select rows by time interval
Old 08-22-2006, 06:00 PM Select rows by time interval
Ultra Talker

Posts: 270
Im making a FORM for my PHP website.... i want the things in the form to be visible in a preset time interval.

In the form users can chose how many days they want their post to be visible.... i also have current date in a set varible.

So i think i have the most important things.... curent date and number of days someone want their post to be visible.

How would the select look like for something like this in MySQL ... ?


Hope someone has an answer
tomcat_fo is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 08-22-2006, 11:59 PM Re: Select rows by time interval
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,337
Name: Keith Marshall
Location: West Hartford, CT
I think all you need is one field in the db that is filled with an expiration date. So you can use (just an example) the following to insert your new form post.

PHP Code:
// Insert into table_name auto increment for the id and the current time multiplied by the number of days user wants it to expire
$sql 'INSERT INTO table_name (id, expire) VALUES("", "' . ( time() + ((int)$_POST['expire'] * 86400) ) . '"'

Then when you are returning the posts that not yet have expired, just use this for example:

PHP Code:
// Select all from table_name where the exipation date is before current date
$sql 'SELECT * FROM table_name WHERE expire <= NOW()'
__________________

<mgraphic /> - I don't have a solution but I admire the problem.

Last edited by mgraphic : 08-23-2006 at 12:00 AM.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-23-2006, 10:34 AM Re: Select rows by time interval
Ultra Talker

Posts: 270
In your example... is $_POST['expire'] a date or a number of days ?


In my form i want users to be able to let their post be visible for MAX 30 days, so i have made a <select> wich contain numbers 1-30
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-23-2006, 11:30 AM Re: Select rows by time interval
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,337
Name: Keith Marshall
Location: West Hartford, CT
Yes, its a whole number of days from now set to expire because it's being multiplied by 86400 (number of seconds in one day) added to the current time (also in seconds from the Epoch).

I messed up in the insert command, because you would need to format the time in a sql date format.

PHP Code:
// Insert into table_name auto increment for the id and the current time multiplied by the number of days user wants it to expire
$sql 'INSERT INTO table_name (id, expire) VALUES("", "' date('Y-m-d', ( time() + ((int)$_POST['expire'] * 86400) )) . '"'
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-23-2006, 01:43 PM Re: Select rows by time interval
Ultra Talker

Posts: 270
Thanks a lot, i belive that must work.... just don't have time to try it right now - have got a lot of other loose ends in my website - had set the deadline by the end of this month (my own website, but want other things running next month, so hope to stick to the plan)
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-25-2006, 04:39 PM Re: Select rows by time interval
Ultra Talker

Posts: 270
Now i'v looked at it againg, and i get a message saying:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /usr/home/web/web128130/kikarin/lysing1.php on line 136

my insert looks like this:
mysql_db_query("$datab", "INSERT INTO lysingar (navn, budstad, bygd, tlf, mail, dagar, pris, km, year, merki, motor, vekt, litur, brennievni, yvirskrift, tekstur, mynd, date, lysingbolkid, ip) VALUES ('$navn', '$budstad', '$bygd', '$tel', '$tmail', "' . date('Y-m-d', ( time() + ((int)$_POST['dagar'] * 86400) )) . '", '$pris', '$km', '$year', '$merki', '$motor', '$vekt', '$farva', '$brennievni', '$yvirskrift', '$stuttlysing', '".$fil."', '$cur_date', '$bolkid', '$ip')") or die('Ikki funni&eth;3');;
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-26-2006, 06:56 AM Re: Select rows by time interval
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,831
Location: Blackpool. UK
it looks like your double & single quotes are the wrong way around here;

PHP Code:
tmail', "' date 
and here
PHP Code:
)) . ", 
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-27-2006, 12:02 PM Re: Select rows by time interval
Ultra Talker

Posts: 270
Now it inserts.... thanks !

BUT it insert the numer 2006 in the atribut no matter if i set the days(dagar) to 1 or 30 in my form...... and i belive 2006 must be the year 2006 that somehow ends up there.

The atribut type is set to integer.

Last edited by tomcat_fo : 08-28-2006 at 09:02 AM.
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-28-2006, 09:02 AM Re: Select rows by time interval
Ultra Talker

Posts: 270
Hope someone knows what the problem is
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Old 08-28-2006, 05:56 PM Re: Select rows by time interval
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,337
Name: Keith Marshall
Location: West Hartford, CT
Make sure the mysql field type is set to date instead of int.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-28-2006, 06:30 PM Re: Select rows by time interval
Ultra Talker

Posts: 270
Thanks that works perfectly now...
tomcat_fo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Select rows by time interval
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16164 seconds with 12 queries