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.. :)
Related
I am new to php, mostly learning from examples, and having a little bit of trouble.
I already searched for this, but couldnt find exactly what i was searching for, or at least couldnt understand it.
I am trying to create somekind of a "news" system. I have a database with 3 columns:
tid - auto-incremented, the id # for the news i post
created_by - who created it...obviously
text - the news themselves
So, is there a way that every time i post some new article it gets a new unique page, based on its id?
In example - mysite.com/news.php?id=3 will show the article with "tid"=3 (from the sql table).
Lets say news.php is my main file and i want its content to change everytime when the ?id in the url changes.
Thanks and wish you a successful week ahead.
<?php
$currentID = $_GET['current_id']; // get current id out of the link
// add some logic to validate it is a valid id to prevent sql injection
build the query which is get next post that is above
current id and limit it to 1 post only
"SELECT * FROM news where id > $currentID limit 1";
back link would turn the greater than to a less than. you would need to have checks to ensure you cant go below 0 and cant go max id number. A quick count of records would do it. or catch not found records to go back to the start
I've recently developed a CMS from scratch using PHP and MySQL running through Apache/phpmyadmin on a localhost.
Everything has gone very smoothly so far, visitors may view articles and navigate through the page successfully. Admins (created by an Admin on the CMS or in the MySQL schema) can log in, view articles, edit articles, view and edit users and delete articles.
My problem is that when articles are deleted, a next/previous button at the bottom (of any existing articles) with a function of
<?php if ($id != 1) : ?>
Previous Article
<?php endif; ?>
which looks for a previously existing article with x-id, however since this article was deleted it no longer exists. Is there any way to remove the article id as the article is deleted, thus freeing up that article id again?
I can manually reset the id to 0,1,2,3... etc. in the schema on phpmyadmin, but I'd like to apply some logic in order to make this system work flawlessly.
Many thanks for you help, if I get time I'll upload the CMS to my web server so that it can be viewed freely.
~Myles
EDIT: a video can be viewed here: http://youtu.be/F8LU1jei3E4
You can do this by querying based on id.
Previous article:
SELECT * FROM articles WHERE id < ? ORDER BY id DESC LIMIT 1
Next article:
SELECT * FROM articles WHERE id > ? ORDER BY id LIMIT 1
So for your pagination it is always better to play with SQL's LIMIT.
Instead of using your id for a query like that:
'SELECT * FROM articles WHERE article_id=' . $id
(it is not a safe query, it's just to show you)
So instead of this query, yours should look like this one:
'SELECT * FROM articles LIMIT ' . $id . ', 1;'
This will not return you the article with the id $id, but the $id'th article.
Hope it's clear for you.
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
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 :)