I just created a pagination for my webpage which fetches data from my mysql database. My query has a WHERE clause where it accepts a php variable and when that is used as a keyword. Here's an extract:
SELECT name, category_id, subcategory, image, price, stock FROM home_products WHERE name LIKE '%$search%'
UNION
SELECT name, category_id, subcategory, image, price, stock FROM kitchen_products WHERE name LIKE '%$search%'
The links to the pages (e.g. Page 1, Page 2 etc) show on the page but whenever I click on them the page shows me an error that the $search variable (the keyword in the where clause) cannot be found. I have tried adding the "search" variable at the end of the links in the pagination but it doesn't really do anything that helps (The list of number of pages in the pagination result increases which just causes me more problems). Here's an extract of the code that outputs the pagination:
echo "<a href='pagination.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='results.php?search='".$_GET['search']."'&page=".$i."'>".$i."</a> ";
};
echo "<a href='results.php?page=$total_pages'>".'>|'."</a> "; // Goto last page `
Am I doing something wrong? Thank you in advance
Things you can check here:
your first page link goes to pagination.php, although others go to results.php. Looks like that should not be this way
your first and last pages don't have the search variable appended to them like the pages in the loop do
if any of your pages generated from the loop say that "search" cannot be found, then maybe you're mixing submission methods (eg. you are expecting a $_POST['search'], but pass the 'search' parameter in the $_GET)
Related
I am relatively new to PHP and MySQL. I have a list of information I want to retrieve from the database and display on my html page. I want on one page to display the unordered list and then on the header of my html page to have links named like A | B | C.....and when I click on any of the alphabet letter to sort the first column of the unsorted list from the database according to the letter of the alphabet clicked.
Here is my code so far...
My code so far
This is how I want my page to look like...
How I want my page to look like
I have googled about pagination using php and the results are helpful but not showing how to create pages for sorting a list based on a certain criteria, like alphabetically
One way to do is, keep links on Alphabet letter as
All |
A |
B
Now in your sort.php get the value sorted by query
First get sorting letter from url using $_GET variable
<?php
if(isset($_GET['let']))
$let = $_GET['let'];
else
$let='';
$query = "SELECT supplier, contact, telephone, email FROM suppliers WHERE supplier LIKE '$let%'";
// other codes
?>
You can do this with SQL queries alone:
$letter = "A";
$query = "SELECT supplier, contact, telephone, email FROM suppliers WHERE supplier LIKE '$letter%'";
For your "ALL" page, just use a different SQL SELECT query omitting the LIKE from the query above
I want to make a PHP page that will changing it's content based on some sort of an ID.
The idea is: the index page will have 4 squares, the content of those 4 squares will be the top 4 records in the database, they obviously have their own IDs in the DB.
What I want to happen is when I click on one of the squares it will pass the ID to another PHP page that will get all the details about it in the page.
To be more clear:
Lets say it is a cars website, the 4 squares would be an image of top 4 cars in the database with the IDs 1,2,3 and 4 respectively, when I click on the car's image (lets say 1) I will be directed to a PHP page called CarInfo.php
what i want to happen here is for the ID of the square to be passed to this page (maybe page will appear something like carInfo.php?id=1) and the page will load all the information from the database where the ID will match the recieved ID (in this case the record with ID = 1).
The problem is I don't even know how to start doing it... How can I pass the ID? How can the other page receive it? And can I use a variable to pass it to so I can use it in the query carteria? If so how?
Note: there will be 1 PHP page that displays the information ONLY (aside from index), all content will be dynamic.
very easy. exactly as you have said. you will use address with parameter, like
carInfo.php?id=1
and on carinfo page you can use php get variable http://php.net/manual/en/reserved.variables.get.php
$_GET["id"]
it will give you selected id, and you can then use is in database
In the first page you have to do something like this for each car:
Click Here
Click Here
Click Here
Click Here
Then, in show.php you need to write a code like this to recive information form the provided ID if the database you're using is MySQL:
<?php
$db = mysqli_connect('localhost','USERNAME', 'PASSWORD','DATABASE');
$result = mysqli_query($db, "SELECT * FROM YOUR-TABLE WHERE id = '{$_GET["id"]}'");
while($row = mysqli_fetch_assoc($result)){
echo "Color : {$row['color']}";
// ...
}
?>
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
I have a page that lists all the products. It's a long list so I gave the users paging ablility, and I give the user the standard page list on the bottom of the page so they can go to the next page or any page. I calculate the page based on the total number of products and the number of rows displayed on one page. No issues.
$sql = "SELECT * from products limit " . $page . ', ' . $rows_per_page
I now added search ability so the user can start the list with a product starting with a given letter, i.e. start with product starting with "s" or "sneaker".
I'm having problems combining the search and paging. How do I figure out what page within the full list, their search would end up in so I can give them the ability to go to the next page or a previous page.
Offering next page only is easy because I can start a new paging list starting with the search product:
$sql = "SELECT * from products WHERE product_name >= '". $search ."' limit " . $page . ', ' . $rows_per_page
but that will not let the user go backwards.
You have a couple of options.
1) You can do it the way you suggested directly if your search form is using GET, and have a variable ($page) with the start-record on it. For example, a link to www.mydomain.com/search.php?s=$search&p=$page+10 on the "next" text. This would allow you to go to any page instantaneously via the URL, but wouldn't be smart if you have sensitive information in there. Also, you'd have to do a separate call to the server for each page.
2) You can do it the way you suggested indirectly with SESSION variables. You can store $search and $page, and have your next/previous buttons be POST form submissions. Format your MySQL query limits ahead or backwards of $page depending on the variable submitted. But again, this requires a new server request for every page.
3) You can get the entire list of results at once and use Javascript to selectively show some of them. For example, you can print the first ten results in a div with display: block and the others in other divs with display: hidden. Then your buttons can have an onclick event which shows the appropriate div of results.
I found some information is the MySQL manual http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html written in 2007 by Ian Storms, which seems to work. I don't know about the efficiency or the internal performance cos, nor do I really understand the sql
set #row=-1;
select foo.Row,foo.product_name
from (select #row:= #row+1 AS Row, product_name from products order by product_name )
AS foo where foo.product_name='skyy';
I've seen alot of tutorials on search with php and mysql, but im having trouble with generating a link with the search result. For example say i have an item called "item1" in my db and the user searhes for item , item1 should be returned as a link so the user can click the link to get more information about that product. Does anyone have any scripts , or snippets of code for how to acheive this?Thanks
You probably need to create an item page say item.php that accepts an id which will then search teh database for that item and display the item information.
Your search results will then have to display the name of the item in a link that also includes the id of the item.
<?php echo $itemname"?>
This would of course be in a loop that goes through the list of items one at a time.
When the user clicks the link it will take them to item.php and send id as a parameter.
that's a simply example with no check and no control by datatype or query results. just start from it and do what you need...
<?php
//your db connection
//col1 is where the id is saved
//col2 is the url
$qry="SELECT col1, col2 FROM table WHERE col1='".$_GET['var']."'";
$result=mysql_fetch_array(mysql_query($sql));
echo 'LINK';
?>
edit:
if you want a direct redirect do this instead of echoing
header('Location: '.$result['col2']);