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
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 am working on a social network website project. I have created database and everything.
The posts table has a preference column which stores the preference value according to the likes and comments that a post gets from the users and also the time at which the post is created.
To retrieve posts for a user's home page from the posts table, I am running a query using joins which sorts using preference column .
Now, suppose I retrieve 10 posts for a user to be shown on the posts table and user scrolls down and one more request is made from the user to retrieve next 10 posts to the server.
If in between of those requests few other users creates a new post or preference value of posts in the database changes in the between, and now if I the second request is run on the server, all the posts will be resorted for the second request (i.e. to show next 10 posts) but since the database is updated , this means in the second request there will be many chances that few of earlier 10 posts are retrieved along in the second request.
I want to know how to avoid these duplicate requests.
How facebook or any other social network solves this problem at the backend when their database is dynamic.
I would rather avoid such unreliable way of sorting at all.
As a user, I'd rather quit that service. Frankly, I hate such too smart a service which decides which posts I have to see and which not. And even dynamically ordered on top of that.
Make it ordered by date, by tags of interest, by something sensible, reliable and constant.
In your script store a record of the rows id returned.
For example, using a basic limit and just storing the latest id when the first select is done, and using the page number to determine the limit of records to return.
SELECT id, somefield
FROM SomeTable
WHERE id < $SOMESTOREDVALUE
LIMIT $PAGENUMBERTIMESTEN, 10
or storing the latest id after each page is returned (which you will need to store each time this is run)
SELECT id, somefield
FROM SomeTable
WHERE id < $SOMESTOREDVALUE
LIMIT 0, 10
If you store the time & date when the user first makes a request in a session, you could use that to filter the posts table.
So your SQL for the second page of results would be along the lines of
SELECT <some fields> FROM <sometables>
WHERE DatePosted <= $timefirstseen LIMIT 10, 10
Where $timefirstseen was loaded from the session variable. This will restrict your results to only posts that existed when the users visit started.
You would of course need to include a feature to allow the user to clear the session or do that automatically when they revisit their homepage to make sure they got to see the new posts eventually!
I have searched everywhere but could not get anything. The scenario is that when I run a select statement from MySQL/PHP, I want to use Next & Previous buttons to navigate backwards and forward through the results.
Does anyone know how I can accomplish this?
Here's how I run the query: select everything from members where hair = black;
This returns 8 results, and I have a page like so details.php?id=3 which takes id and display the details.
I want to be able to keep clicking the next button and move to another id from the result.
How do I accomplish this?
Thank you for your assistance.
If you mean to display 1 product details per page with Next buttons, then
1) Get the total rows from table
2) Find total number of pages like
$totalPages = $totalRows; //since 1 record per page
3) Loop thru $totalPages value to generate next links
4) Get the id of product from page url $_GET
5) Query sql using te product id obtained frm GET
Well thats the basics, hope your get it
Assuming you have all ids in the $allIds var, try something like this:
<?php
$id = (int) $_GET['id'];
$key = array_search($id, $allIds);
$nextId = $allIds[$key+1];
?>
Well, you have 2 different scopes here to address, first, when you query you are in PHP/Server side mode. PHP gets some data, processes it, shows some of it to the knowledge i have of your problem and then sends it to the browser.
Next, you have the client scope, where the client has the HTML rendered to his screen and has the possibility to click NEXT and PREVIOUS. When he does that, he issues a request to the server to get the NEXT or PREVIOUS record based on the current one.
You have 2 scenarios to work this problem out:
1) Load the whole data into memory and scan it to find your current position, see if there is a NEXT and PREVIOUS element and respond to the request of the user. If the user asked for the NEXT, easy, just move one more record. If the user asked for PREVIOUS item, then when scanning using a WHILE or FOR/FOREACH, always keep in memory the "lastitem" you saw, you can use this "lastitem" as your PREVIOUS item.
2) In the event you have many (i mean like more than 1000) items, you need to use a little subquery magic and work this out using SQL.
select everything from members where hair = black AND id = XYZ ORDER BY id LIMIT 2;
This will return you the CURRENT AND NEXT elements. Then call the same query again but this time, order it DESC so that your 2 items are the CURRENT AND PREVIOUS.
select everything from members where hair = black AND id = XYZ ORDER BY id DESC LIMIT 2;
Note that you can order this the way you want, the goal is to get a static result, something that always gets out the same way so that you can reverse it and get the items around the selected one.
I hope this helps you
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 :)