I designed a blog and its retrieving data from database dynamically.Now i want that the blog that has most comments is displayed on the top. So anyone who can help me with this.I am posting the snapshots my page where the popular blogs is to be displayed.
Area where the popular blog are displayed.
http://s1272.photobucket.com/user/Ahad_Murtaza/media/Untitled_zps329dc86b.png.html
Before asking try to do something or try to google it...
Your Answer --
You can use pagination for display the data...
Or you can do some query work --
SELECT SUM(something) AS fieldname
FROM tablename
ORDER BY fieldname
By taking that SUM of comments we can display.
Hope this will help !!
AS i seen your images in link a query should be like this to display as per your output
Please set column name as per your need to display from table here is just query
select
Comment.blog_id,
count(Comment.commentId) as number_of_comments,blog.title
from
Comment
LEFT JOIN blog
ON Comment.blog_id=blog.blog_id
group by
Comment.blog_id,
order by
number_of_comments desc
limit 10;
let me know if i can help you more
Related
Seems to be a simple question for someone who knows well SQL, but not for me.
Anyhow, here is a sample table:
Primary (and only) key is on id. So basically the scenario is as fallows. User may add images. Newly added images are inserted with comune_number value 0 avoiding duplicates (on file name = image via PHP). Images inserted can be assigned to a category from another table. The same image can be assigned to many categories, for each is inserted a new row with the category id (comune_number). Relation between the two tables is on comune_number.
I would like to show the images, with a checkbox checked for which is assigned already.
My question is simple. How to include all images once, but where the same image is already assigned, include that comune_number instead of 0. I don't care about the order. The result I would like to achieve is something like this:
I'm aware of GROUP BY, so if I try
mysql_query("SELECT * FROM banner WHERE `comune_number` = 0");
or
mysql_query("SELECT * FROM banner GROUP BY `image`");
I end up with the same (not wanted) result.
Most likely I have to combine two queries in one, but I can't figure out which and how.
Note1: I have tried many combinations in phpMyAdmin, based on my (little) knowledge and on what I found with Google (including Stackoverflow), but none of them resulted as shown.
Note2: MySQL version 5.1
Either MYSQL only or combined with PHP solutions are welcome.
EDIT
I need to keep the comune_number. So when I show the images, I need to know the id.
My queries must rely on comune_number, so when I need comune_number = 10, the result should be as on second image above.
EDIT2
It seems I wasn't made myself clear. So what I want, when user is watching category of id 10, show him all the images once, and mark the ones which were assigned to that category.
Another example is here. So if user is watching category (comune_number) of id=9, show every picture once, and mark the two assigned to it.
Based on your SQLFiddle and all the comments here is the updated query:
SELECT r.*
FROM (SELECT b.*
FROM banner b
WHERE b.comune_number = 9
UNION ALL
SELECT b2.*
FROM banner b2
WHERE b2.comune_number = 0
GROUP BY b2.image) r
GROUP BY r.image;
Live DEMO.
select * from
(SELECT * FROM banner order by
FIELD(comune_number,10,0))
abc group by `image`;
fiddle
Updated:
Query with filter condition:
select * from
(SELECT * FROM banner
where comune_number in (10,0) order by
FIELD(comune_number,10,0))
abc group by `image`;
an important tip. When you use GROUP BY all the field you put in your field list, must be in GRUOP BY clause or must be into an aggragate function. MySql has a strange behaviour, don't sign the error but if you try in another DBMS your query:
SELECT * FROM banner GROUP BY image
You have an error.
MySql applies for fields not present in group by an implicit limit 1.
Solution about your issue:
SELECT * FROM banner b1
WHERE b1.comune_number <= 'YOUR_CATEGORY'
AND NOT EXISTS
(SELECT 'X' FROM banner b2
where b2.image = b1.image
and b2.comune_number > b1.comune_number
AND b2.comune_number <= 'YOUR_CATEGORY')
EDIT
I've changed query, now I put a condition about input category. Where you find YOUR_CATEGORY put the value of category you see (i.e. 9).
I am not sure whether this is the exactly you need, but I think it can be helpful.Try this:
Select if(comune_number = 10, comune_number,0), id, image from table
You may change comune_number as per your convenience. Further nesting in IF condition is also possible.
I have mysql_query problem in php. Of course I have visitors in my website and they read football news there. The problem is that I try to sort news by views DESCending, but it doesn't work for new uploaded news. Somehow it appears at the end of the page. I've figured out one more thing that when views of the news is 9 it appears at the beginning of the page, but if it is more than 9 it appears at the end of the page. I'd appreciate any kind of help. Here is mysql_query.
mysql_query("SELECT * FROM news ORDER BY views DESC");
Oh and by the way, my website is http://www.bbcsport-football.com/football
there you can find Sorting options, New and Most Viewed. Click most viewed and you'll see it.
You need to convert the views column to a number
mysql_query("SELECT * FROM news ORDER BY cast(views as INTEGER) DESC")
Or even better change the table definition so views is a INTEGER
You're sorting the news by the count of "views", if I'm right, views is the count of users that already have read the page right? Try sorting it by the publication date instead, like:
"SELECT * FROM news ORDER BY pub_date DESC"
your code seems to be o.k to me... just check this query in your phpmyadmin database and check results what you get manually.
i see your URL and it looks o.k as per order by view. but one thing i notice is your pagination.
when i click pagination (page 2) from most viewed. it will automatically redirected to the first page of new. hope this little testing will help you in your website.. :)
Im having trouble with an mySQL Query .What i need is a query that will display on the same page where im viewing a post a list of links to posts in the same category on the side of the page.Im almost finished a music site similar to youtube where users can listen to a track and see a list of tracks beside the post that are in the same category.
Any Information Ive found on this is just related to Wordpress pluggins which im not using.
My Database Tables are
Table_categories
cat_id
cat_name
Table - Mixes
mix_id
mix_subject
mix_description
mix_date
mix_cat -- mix_cat is the foreign key for categories.cat_id
The cat_id of Categories is linked to the foreign key mix_cat in table Mixes.I Know it has something to do with ?id= number of the id in one of the table,s
Cheers guys Thanks for your help
Trevor
You could try something like
Select * from Table_Mixes where id={insert id here, don`t forget to force the int type to avoid SQL Injection}
Get the row and now you have a $mix object with a mix_cat property (or $mix['mix_cat'], if you get it as an array).
Now you just
select * from Table_Mixes where mix_cat={insert $mix->mix_cat here} order_by {whatever} limit {how many links?}
and build the links.
Does anyone know how to display 3 previous posts on article page? Let's say I'm in article post page and would like to display previous 3 posts images (basing on the post I'm currently reading) below it's content, but not next posts images. I was trying to do this with setting up the offset in the db query but with no luck. Can someone please help me with this one? I would greatly appreciate any help or at least pointing me in the right direction.
That shouldn't be too hard, if you use an id field with auto increment values, you could do something like this:
SELECT id, name FROM articles WHERE id < {current_id} ORDER BY id DESC LIMIT 3;
Obviously, replace {current_id} with the id of the article you're currently reading.
After displaying the specific post do a new WP_Query to retrieve the 3 posts previous to the publication date of the displayed post. This documentation page describes how to query for posts with a relative date (e.g. the example Return posts from the last 30 days). Base the query on the publication time of the displayed post.
The example includes a way to supply a WHERE clause to the query, with add_filter(). I'm not sure, but I think you need to call remove_filter after doing the query, or that filter might be applied to other queries also.
I am trying to build a website with mysql and php. This is the first site I have attempted so I want to write a little plan and get some feedback.
The site allows users to add some text in a text field as a “comment”. Once the comment has been entered into the site it is added to the database where it can be voted for by other users.
When a new comment has been added to the database it needs to create a new page, e.g. www.xxxxx.com/commentname or www.xxxxxx.com/?id=99981.
There will be a list of "Comments" in the database along with the number of votes for each comment.
The home page will have two functions.
1) Allow users to add a "comment"
2) Display two tables, each with 20 rows containing most "popular comments" and "recent comments"
Each comment will generate its one page where the comment will be displayed. Here users can read the comment and Vote for the comment if they wish.
Please help me out by explaining how to do the following.
-Generate a new page whenever a comment is added to the database
-Add a vote to the vote count in the comment database.
-Display the top 20 most popular comments as per number of votes.
-Generate a new page whenever a comment is added to the database
You only need a comment.php file with a MySQL query getting the given comment out of the database. I would recommend to use the comments primary key to get the comment. Using rewrites you can have a URL like this: www.xxxx.com/comment/1. If you need the redirect for a specific link structure ask again.
-Add a vote to the vote count in the comment database.
Just add a column to your table holding the votes. If you have logged in users and you want then to check their votes, create a new table for the votes and another table for the many to many realtion.
-Display the top 20 most popular comments as per number of votes.
This is simply done by sorting in the MySQL queries and selecting only 20 results:
// For the recent 20 comments
SELECT * FROM comments ORDER BY id DESC LIMIT 0,20
// For the 20 most popular comments
SELECT * FROM comments ORDER BY votes DESC LIMIT 0,20
Any further questions?
This is a pretty broad question, i don't think we'll be able to help you fully here at stack without making a full blown php blog tutorial!
I'll try and point you in the right direction however. Firstly i'd say take a look at wordpress, even though i presume you want to make your own custom one, wordpress would be a good starting point for code inspiration? (Just a thought)
The way i'd generate a new page, would be to make a php page, say comments.php, which using the $_GET variable, gets the related record in the database and displays it.
Adding a vote up or down is as simple as adding form to the page with two submit buttons, one with a value of 1 one with a value of -1, upon submit it sends its value to the database, and takes the existing vote value say 25 and adds its value so, if u up voted 25 + 1 = 26 if you downvoted 25 + -1 = 24.
Displaying the 20 most popular comments is just a case of using some SQL sorting, something like this would work
SELECT * FROM comments ORDER BY votes DESC LIMIT 0, 20
That statement selects all the columns from the comments table, sorts it by the votes column decending, so highest value first, and then limits the number of records it fetches by 20, from there its a case of looping through each record and displaying it how you wish.
I hope this atleast gets you started on the right path :)