Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7004363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:11:24+00:00 2026-05-27T21:11:24+00:00

I am trying to sort articles by Today, This Week, This Month , All

  • 0

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

enter image description here


Votes table

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:11:24+00:00Added an answer on May 27, 2026 at 9:11 pm

    try

        SELECT stories.*, SUM(votes.vote_value) as 'total_votes' 
        FROM stories JOIN votes ON stories.id = votes.item_name 
        WHERE votes.item_name=$date 
        GROUP BY stories.id ORDER BY total_votes DESC LIMIT 10
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to sort an array in PHP that is being populated from a CSV.
Trying to sort this array of objects according to (1) depth and (2) weight,
I'm trying to sort a multidimensional array in objective-c i know that i can
I've started a new job this week and am trying to get my head
I made a search.php that finds articles on my site. What I was trying
Trying to sort out the correct syntax for this UPDATE : UPDATE `foo` SET
I trying to sort this HashMap based on date in keys My Hash map:
I am trying to sort a List like this: public void Example() { string
I am trying to sort query result into an array. I have following query.
I was trying to sort some xml with jquery, but this code doesn't affect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.