I have a page full of user-submitted updates and stuff. Basically the basic stuff. I am taking it out of a database. I have this so far:

$getnews = mysql_query("SELECT * FROM news ORDER BY $order LIMIT 50");

and $order is determined by a drop down menu on the top of the page. That drop down menu includes an option to view most "liked" (determined by a column of the database table that shows how many people have liked the post.

I do not want it to be the top 50 most liked of all time, though (which is what it's currently doing), I want the ability ot have the most liked OF the top 50 (like, take the last 50 postings, sort by most liked), but when I try to put LIMIT 50 before ORDER BY, it gives me an error. Any help?

Recommended Answers

All 6 Replies

You cannot put it before the order. Not quite sure what result you are after, but it sounds like you need a sub-query.

A sub-query, eh? I've never heard of those. (but I am new to this, so that might be why)

I basically just want to take the 50 most recent news updates (I have an id field that's AI) and sort them by most liked (rather than having the 50 most liked posted news of all time)

Something like:

SELECT * FROM (
  SELECT * FROM table ORDER BY `date_column` DESC LIMIT 50
) ORDER BY `liked_column`

When I put

$getnews = mysql_query("SELECT * FROM (SELECT * FROM news ORDER BY id DESC LIMIT 50) ORDER BY rating");

I get
"Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/atalking/public_html/news.php on line 79"

Try it in phpmyadmin, the query returns an error. I think it needs to be:

$getnews = mysql_query("SELECT * FROM (SELECT * FROM news ORDER BY id DESC LIMIT 50) AS temp_table ORDER BY rating");

I didn't try it in phpmyadmin and your line of code (put directly into my ftp client) worked perfectly (except I had to add "DESC"). Thanks a bunch!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.