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.
Related
I'm using MySQL and Php.
Here's the thing : I'm building a forum, with topics and posts.
Posts are displayed in a topic in different pages (let's say 10 posts/page).
My question is : if I ask to see a specific post in a topic, how can I know the page where the post is displayed ? (With the purpose to display the page required)
Should I calculate the number of pages then check each group of items of all possible pages or something like that ?
Thx for your help !
You could use Google Custom Search and let Google do all the hard work. I hear they're quite good for searching websites.
If you want something a little fancier you can pay for Google Site Search.
if your site is small and you want to try a really really simple solution why don't you explore MySQL Fulltext search?
That said the field, fields to search must be a Fulltext index. So you can grab a pair of text fields and some varchar fields and jointhem all in a Fulltex Index.
SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('database' WITH QUERY EXPANSION);
But go and have a better look at MySQL manual!
Ok, I was eating and then... THE IDEA.
I guess a simple way to doing this is to count the number of items before the needed one (with ID or, in a better way, with Timestamps cause posts are ordered with those), and then calculate how many pages it takes.
Meaning, for example, if pages display 10 items, we have 24 items before our targeted one : ceil(24/10) = 3 item will be in page 3 !
Very simple when we think of it.
#Get the page displaying the selected item
#Step 1 : calculate how many items are before the selected one
$nbr_of_items = SELECT COUNT(post_id) FROM posts WHERE post_topic_id = :topic_id AND post_timestamp < :timestamp_of_selected_item
#Step 2 : do simple maths
$calcul = ceil($nbr_of_items / $nbr_of_items_displayed_in_a_page);
For example, we have 24 items before the selected one and pages display 10 items :
$calcul = ceil(24 / 10) = 3
Of course it could be updated (what if an other post has the same timestamp, etc) but it's a good start.
here is my issue.
I am currently in a post (so in single.php). I got the only category that this post is in by using:
$category = get_the_category()[0];
Now, what I would like to be able to know is the current position of that post in that category and I need to be able to retrieve that position without searching through all pages of the category (for performances reasons).
E.g:
Let`s say my post is the 14th most recent post in its category, I want to get that value (14) so that I can then calculate on which category page it would be (knowing how many posts are displayed by page)
Thanks
If you know the number of posts/page ahead of time, you can just use SQL to do this.
Lets say you want to do 10 posts/page.
SELECT * FROM articles WHERE category_id = :categoryID LIMIT 0, 10;
That will give you the first 10 articles in a given category. If you see the articles for page two, you just modify the LIMIT
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
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.. :)
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 :)