How to retrieve date time from mysql in decending order? - php

I have made a simple blog and trying to retrieve the latest post to the top of the blog. the code i have typed already does the ordering for the specific day however when i try to add a another post the next day it goes to the bottom of the page and the ordering of the latest blog works on that specific day.
So what i want is ,my blog to compare all the dates and then post the latest one right at the top..
many thanks

You should use order by
SELECT * FROM table_name WHERE your_condition ORDER BY your_date_column_name
DESC

Related

SQL - Workaround for Removing Last Element and Updating New one

On my First SQL-Based Project.
I want a News pane on my website.
In that news pane. I want to show Latest 10 News of all time which I keep updating after every new change in website like a changelog.
What will be the workaround for SQL-PHP to Show only the Latest 10 News and How would I update the DataBase Table (Removing the Oldest, 10th Entry) and then Adding Latest(1st Entry) to the table + changing the IDs (1-10).
For this, You can Make a Table(e.g. News). With Column ID content date.
Simply Add the news and the do this.
SELECT * FROM News ORDER BY ID DESC LIMIT 10
then,
Take this into PHP ARRAY and then simply work there.
Rarely would you want to remove the old values from your database straight after adding a new entry, why not just order the database select results and limit the rows, far more flexible for future code changes.
SELECT * FROM News ORDER BY id DESC LIMIT 10;
Assuming that you have timestamp for your news, you might want to order by this database value instead. As this would allow you to 'bump' up news articles later on, for example if you want an edited article to be considered new you would just have to update the timestamp, and not change the row ID to a higher value.
SELECT * FROM News ORDER BY unix_time_stamp DESC LIMIT 10;
Finally if you wanted to clean up your news entries I would do that on a cleanup function separately, probably on some kind of hourly or daily cronjob.

Call the next and previous records in a MySQL query

How do I call the next and previous MySQL records, without referring to the ID?
In my table, I have the following records:
IDNO (Auto-increment, primary key)
Title
Slug (unique key)
Data
DateTime (Y-m-d H:i:s)
I have two pages - index.php and single.php. In index.php, my posts are called (10 at a time) and ordered by DateTime. Each entry links to single.php, where the single post is displayed, based on the URL variable slug and calling the appropriate record in my database. I would like, since the pages are making up a simple blog, to call the next post and the previous post, still in DateTime order (the next will link to the more recent post, the previous link to the less recent post). Any advice on how to do this? If it was based on IDNO, I could do it, but since it isn't and the posts are not naturally in DateTime order, but are ordered as such in my query, I am at a loss.
The query I use on each single.php page is:
SELECT * FROM site_posts WHERE slug = '".$_GET['SLUG']."'
I want the links to appear on this page, so I am only including that query.
For the next post
SELECT * FROM site_posts WHERE DateTime > $currentPostDateTime
ORDER BY DateTime ASC LIMIT 0,1
and
for the previous post
SELECT * FROM site_posts WHERE DateTime < $currentPostDateTime
ORDER BY DateTime DESC LIMIT 0,1

PHP mysql_query order by views trouble

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.. :)

How to display 3 previous posts in article page?

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.

php and mysql site design question

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 :)

Categories