I am trying to sort articles by “Today”, “This Week”, “This Month” , “All Time”. The thing is that my query sums up votes from one table and gets articles from another table, this make query really complicated to me, And I am not sure if I am doing it right.
Anyway, I have a page like http://web.com/top.php?time=“some time here” And I use the following code to select articles basing on what time user chooses instead of “some time here”
$time = preg_replace('#[^a-z]#i', '', $_GET["time"]);
if (!$time) { //Today
$date = "WHERE TO_DAYS(NOW()) - TO_DAYS(stories.st_date) = 1";
}
else if ($time == "Week") { //This Week
$date = "WHERE TO_DAYS(NOW()) - TO_DAYS(stories.st_date) <= 7";
}
else if ($time == "Month") { //This Month
$date = "WHERE TO_DAYS(NOW()) - TO_DAYS(stories.st_date) <= 31";
}
else if ($time == "All") { //All Time
$date = "";
}
else {
header("Location: http://www.web.com/");
exit;
}
$sql = ("SELECT stories.*, SUM(votes.vote_value) as 'total_votes' FROM stories JOIN votes ON stories.id = votes.item_name ".$date." GROUP BY stories.id ORDER BY total_votes DESC LIMIT 10") or die (mysql_error("There was an error in connection"));
None of this seems to work ;D I don’t see why, In my opinion it should, but than again I am relatively new to php and mysql. So could anyone suggest what the problem is here?
My Database Structure to give you better understanding of the system.
Stories table

Votes table

try
or similar, think about what you want to achieve and the logic then rewrite your sql query
in phpmyadmin till you get the right output before putting it into php.