Page 1 of 1

Every day a new Text (thought of a day)

Posted: Tue Apr 06, 2004 3:14 am
by caitanya
Hi

I am just updating/redesigning an old website. I made it into PHP and connected to the mySQL database. Now I would like to make a php script that shows for each day of the month another thought from the database.

I made a new table in the database with ID THOUGHT and DAY
The administration where I add and delete the thoughts is working nice but reading from the database is not working as i want. It shows me all the texts at once.

What i am looking for is a small php code that will read from the databese as follows: If today is a day 6. April 2004 it should show only the THOUGHT under DAY 6. Tomorow than it would be the day 7, and so on...

I just came from JAVA and ASP so I have some problems with PHP althaugh it is a great prog. language...

If you have something peace of code that i can use or some ideas then please let me know.

thnaks
caitanya

Posted: Tue Apr 06, 2004 3:21 am
by JayBird
if you could show the current code you are using, we can help you change it to suit your needs....hopefully.

Mark

Posted: Tue Apr 06, 2004 3:24 am
by twigletmac
You then need to add a WHERE clause to your SELECT statement so that you only retrieve the record you want, you can use two MySQL functions in the SQL, DAYOFMONTH and NOW() to get the current day's number:

Code: Select all

SELECT ID, thought FROM the_table WHERE day = DAYOFMONTH(NOW())
http://www.mysql.com/doc/en/Date_and_ti ... tions.html

Mac

Posted: Tue Apr 06, 2004 4:30 am
by caitanya
ok here is my old code... it show all the thoughts at once - one under another...

What is missing is some kind of check for the current day...


<?php

function showDaily ($thought, $dates) {
$db = new DB();
$db->query("SELECT * FROM ".DB_PREFIX."_daily" );

while ($thought = $db->next_record()) {
echo "<br><br>$thought[thought]<br>";

}
$db->close();
}

showDaily ($thought, $dates);

?>

thanks for help
caitanya

Posted: Tue Apr 06, 2004 4:34 am
by twigletmac
caitanya wrote:What is missing is some kind of check for the current day...
See my post above ;)

Mac

Posted: Tue Apr 06, 2004 5:13 am
by caitanya
ok i changed the code to this...

edited: please use the BBCode to format your posts. Easier to to read

Code: Select all

function showDaily ($thought, $dates) {
	$db = new DB();
	$db->query("SELECT ID, thought FROM ".DB_PREFIX."_daily" WHERE dates = DAYOFMONTH(NOW()));
	
	while ($thought = $db->next_record()) {
		echo "<br><br>$thought[thought]<br>";

  }
	$db->close();
}
showDaily ($thought, $dates);
and it gives me a parse error in the line 5 meaning:
$db->query("SELECT ID, thought FROM ".DB_PREFIX."_daily" WHERE dates = DAYOFMONTH(NOW()));

what is this DAYOFMONTH(NOW()) function? do i have to make a new table with this name or is it just a sql string...

shouldnt there be a function that first checks what date is today (maybe DAYOFMONTH(NOW())) and then checks which thought has this date in the table DATES.

So my Tables are now ID, THOUGHT, DATES

as i said i am quite bad in php so i need a help on this matter...

thanks
caitanya

Posted: Tue Apr 06, 2004 5:30 am
by twigletmac
caitanya wrote:and it gives me a parse error in the line 5 meaning:

Code: Select all

$db->query("SELECT ID, thought FROM ".DB_PREFIX."_daily" WHERE dates = DAYOFMONTH(NOW()));
You need to put the string in quotes otherwise PHP doesn't know what to do with it:

Code: Select all

$db->query("SELECT ID, thought FROM ".DB_PREFIX."_daily WHERE dates = DAYOFMONTH(NOW())");
caitanya wrote:what is this DAYOFMONTH(NOW()) function? do i have to make a new table with this name or is it just a sql string...
DAYOFMONTH() and NOW() are functions within MySQL - the link I posted in my first post has information on both.
caitanya wrote:shouldnt there be a function that first checks what date is today (maybe DAYOFMONTH(NOW())) and then checks which thought has this date in the table DATES. So my Tables are now ID, THOUGHT, DATES
Table or field? I think you mean field. Previously you said that there was a field called day containing day numbers and that's what my previous code is based on - if that is now a specific date (i.e you have 366 thoughts) AND stored in a MySQL DATE type column, then you can change the SQL to:

Code: Select all

SELECT ID, thought FROM the_table WHERE dates = NOW()
Mac

Posted: Tue Apr 06, 2004 5:59 am
by caitanya
ok thanks now it works... i hope. :D

which date does it take? mine or the one from server?

cause now it shows me correctly the daily thought nr. 6 for the 6.April 2004 and if i change the date on my machine it still shows the thought nr. 6...

anyway i'll see this tomorow...

thanks this forum is grat :D
caitanya

Posted: Tue Apr 06, 2004 6:10 am
by JayBird
Yes, it gets the server date.

Mark