Click to See Complete Forum and Search --> : Query::Displaying Only past 7 days and am I using stripslashes right?


Markbad311
01-09-2006, 05:55 PM
<?php
$db_name = "markbad_markbadsql";
$table_name = "event";
$connection = mysql_connect ("localhost", "markbad_drpl1", "n4x4q37IhCez")
or die ('I cannot connect to the database because: ' . mysql_error());
$db = mysql_select_db ($db_name, $connection)
or die (mysql_error());
//sort by date.. only monday thru friday
$sql = "SELECT *, curdate() AS c, dayofweek(curdate()) AS d FROM $table_name WHERE date BETWEEN date_sub(c INTERVAL (d -1) DAYS) AND date_add(c INTERVAL (7-c) DAYS)";
$result = mysql_query($sql,$connection)
or die (mysql_error());
//While loop will create an array called $row
while ($row = mysql_fetch_array($result)) {
// get individual elements from events
$date = stripslashes($row['date']);
$bar = stripslashes($row['bar']);
$updated = stripslashes($row['updated']);
$details = stripslashes($row['details']);
$map_url = stripslashes($row['map_url']);
$display_block .= "
\t<h4>$bar <span class=\"date\"> $updated</span></h4>
\t<p>
\t\t&details<br />
\t\t<span class=\"date\">$map_url</span>
\t</p>";
}
?>

//bunch of html

<? echo "$display_block"; ?>




I get a funky error from mysql
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTERVAL (d -1) DAYS) AND date_add(c INTERVAL (7-c) DAYS)' at line 1


Please Help. What I want to do with the query is display all of the events within listed from "ONLY" the past seven days. not any before that


Also am I using stripslashes right?

Thanks!

NogDog
01-09-2006, 07:03 PM
Well, for last 7 days, I think you might want something like:

$query = "SELECT * FROM `$table_name` WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 DAY) and DATE_ADD(CURDATE(), INTERVAL 1 DAY)";

Markbad311
01-09-2006, 07:29 PM
so what about the stripslashes? is that correct? One more thing is that syntax for selecting the last 7 days, or past 7 and future 7?

NogDog
01-09-2006, 07:47 PM
The sample code I provided was intended to provide 7 days of data, with the latest day being today. If you want 7 days up to but not including today, then change it to:

$query = "SELECT * FROM `$table_name` WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) and CURDATE()";

I see nothing wrong with the stripslashes() syntax, though I'm not sure there's any reason to use it here.

Markbad311
01-09-2006, 08:39 PM
Ok One more thing other then that I have ran into allready. Some of my data runs off my page. this is probally a bad thing so I don't want to change the page rather, I would like to have it go through the table and display only so many rows at a time then have some kind of variable to trigger the next sets and keep going till the end. I reall don't want it to leave the one php page. Other then that everything works accordingly

chazzy
01-09-2006, 09:04 PM
you can use the MySQL limit function:
LIMIT 0,10
starting with the 0th row, shows the next 10 rows.
LIMIT 10,10
start with the 10th row, shows the next 10 rows
etc.
you can use a get parameter to determine how many rows to offset in your query.