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.
Related
I am building a site where I'm constantly adding new links. The links are organized in a mysql database with id being the main index starting at 1. I only want to display the top five links from the database on my site so, in my php code I assign the database to an array so I can only call the top 5 links to the site. My problem is that every time I add new links to the database I have to change the id numbers manually. I want the newest link to start at id number 1 and the one below that to automatically become id number 2 and so on. Is there a way to do that or is there a better way to organize my database?
dont make any changes to the id(if you're auto incrementing),just modify your Mysql Query to retrieve data from your table by ID in descending order.
SELECT * FROM your_table ORDER BY id DESC LIMIT 5
I'm trying to create a filter to show certain records that are considered 'trending'. So the idea is to select records that are voted heavily upon but not list them in descending order from most voted to least voted. This is so a user can browse and have a chance to see all links, not just the ones that are at the top. What do you recommend would be the best way to do this? I'm lost as to how I would create a random assortment of trending links, but not have them repeat as a user goes from page to page. Any suggestions? Let me know if any of this is unclear, thanks!
This response assumes you are tracking up votes in a child table on a per row basis for each vote, rather than just +1'ing a counter on the specific item.
Determine the time frame you care about the trending topics. Maybe 1 hour would be good?
Then run a query to determine which item has the highest number of votes in the last hour. Order by this count and you will have a continually updating most upvoted list of items.
SELECT items.name, item_votes.item_count FROM items
INNER JOIN
(
SELECT item_id, COUNT(item_id) AS item_count
FROM item_votes
WHERE date >= dateAdd(hh, -1, getDate()) AND
## only count upvotes, not downvotes
item_vote > 0
group by item_id
) AS item_votes ON item_votes.item_id = items.item_id
ORDER BY item_votes.item_count DESC
You're mentioning that you don't want to repeat items over several pages which means that you can't get random ordering per request. You'll instead need to retrieve the items, order them, and persist them in either a server-wide or session-specific cache.
A server-wide cache would need to be updated every once in a while, a time interval you'll need to define. Users switching page when this update occurs will see their items scrambled.
A session-specific cache would maintain the items as long as the user browses your website, which means that the items would be outdated if your users never leave. Once again, you'll need to determine a time interval to enforce updates.
I'm thinking that you need a versioned list. You could do the server-wide cache solution, and give it an version (date, integer, anything). You need pass this version around when browsing the latest trends, and the user will keep viewing the same list. Clicking on the Trends menu link will send them to the browsing pages without version information, which should grab the latest from your cache. You then keep this as long as the user is browsing these pages.
I can't get into sql statements, not because they are hard, but we don't know your database structure. Do you keep track of individual votes in a separate table? Are they just aggregated into a single column?
Maybe create a new column to record how many views it has? Then update it every time someone views it and order by threads with the largest number of views.
I have single table where i put all the content. Each row is one post. I want to be able to manipulate order of posts so that i can push some of those to the top or bottom...
Also, each post belongs to certain category. (i believe that that additional parameter makes difference when ordering posts within certain category)
Please, give me some hints/code patterns how would you do it?
p.s. i don't want to add new tables, make more relations, i want it as simple as possible...
I would personally accomplish this by adding a sort_index field to the table with a default value of something like 100. You can then push posts down by decreasing their sort order, or push them to the top by increasing it. By starting with a specific number, you are establishing a baseline for default sorting of posts.
SELECT * FROM `posts` ORDER BY sort_index DESC, date_posted DESC
This would use ensure posts with the same sort index get a secondary sorting based on the date they were posted.
It also allows you to then easily automate making the posts fall down in the rankings over time with a small script setup as a cron to decrease the sort index of posts every so often until they hit the default value.
Let's say you have a table with the following columns:
id
post
category
You can add a new integer column called rank. Using the new rank column you can order your posts like this:
SELECT `post` from tblPosts order by category, rank;
Now you can order your posts by assigning a rank to them. In the above example smaller numbers will be listed at the top and larger ones at the bottom.
How would I go about selecting one entry at a time while prioritizing on newest ones? So, in the end, the newly added entries are seen before the old one. The problem is new entries are added from time to time, and I want them to be shown first and I do not want to show already seen entries.
OK, so let's attack this issue two ways:
To show newly added entries first, you'd do something like:
SELECT * FROM customers ORDER BY customer_id DESC;
And if you want to leave out items that have already been viewed, you'll have to flag them in the DB somehow. Maybe add a did_view integer column with a default value of 0 and set it to 1 every time the newest results are pulled:
UPDATE customers SET did_view = 1;
Then, on the next page load, you'll pull the new entries:
SELECT * FROM customers WHERE did_view = 0 ORDER BY customer_id DESC;
I have a friend who runs an online auction website. He currently has a featured items section on the homepage that he wants to have cycle an item every X amount of minute. The site runs off a MySQL database which I haven't actually seen yet.
The current code that he is using is a big, long messy Javascript code that is causing all kinds of errors.
The items would have to cycle in order and when they get to the end, go back and repeat again.
What would be the best approach to take to this using PHP?
EDIT: I mean from a backend SQL perspective. Not the UI.
Thanks
Ben
Assuming you have a separate table for the featured items (probably has an item ID referencing the main items table and maybe other info)... In this table, add a last_featured column to represent the time the item was last shown. From there, you can manipulate your queries to get a rotating list of featured items.
It might look something like this (as a weird pseudocode mix between PHP & MYSQL):
// select the most recent item in the list, considered the current item
$item = SELECT * FROM featured_items ORDER BY last_featured DESC LIMIT 1;
if ($item['last_featured'] > X_minutes_ago) {
// select the oldest item in the list, based on when it was last featured
$item = SELECT * FROM featured_items ORDER BY last_featured ASC LIMIT 1;
// update the selected item so it shows as the current item next request
UPDATE featured_items SET last_featured=NOW() WHERE item_id = $item['item_id'];
}
Note that this requires 3 call to the database... There may be a more efficient way to accomplish this.