please i need your help greatly. I am creating a page that will have all the lists of the registered members using pagination, but when i click on any user it takes me to only the 'profile.php?user=3' instead of the user's id profile if you understand what i mean, please show me what i did wrong in my coding:
<html>
<head>
<title>MEMBERS</title>
<link rel="icon" href="guyt.gif" type="image/x-icon">
<link rel="stylesheet" type="text/css" media="all" href="style.css">
</head>
<body>
<?php include 'connect.php'; ?>
<?php include 'functions.php'; ?>
<?php include 'header.php'; ?>
<div id="sidebar">
<br>
<?php include 'lists.php'; ?>
</div>
</body>
</html>
here is the lists.php which contains the pagination:
<?php
$per_page = 5;
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
$mem_query = mysql_query("SELECT id FROM register");
while($run_mem = mysql_fetch_array($mem_query)){
$id = $run_mem['id'];
$first = getuser($id, 'first');
$last = getuser($id, 'last');
}
$sql = "SELECT * FROM register";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = ceil($num_rows / $per_page);
$sql .= " LIMIT $start, $per_page";
$result = mysql_query($sql);
While($row = mysql_fetch_array($result)){
echo "<div class='header'><table width='98%'><tr><td><div align='left'>" . $row['first'] . " " . $row['last'] . "</div></td><td><div align='right'><a href='profile.php?user=$id'>VISIT PROFILE</a></div></td></tr></table></div><br>";
}
$prev = $page - 1;
$next = $page + 1;
echo "<hr>";
if($prev > 0)
echo "<a href='?page=$prev' class='box'>Previous</a></font> ";
if($page < ceil($num_rows/$per_page))
echo " <a href='?page=$next' class='box'>Next</a></font>";
?>
Please look at your code critically,you are generating the id within the first loop and assigning the value in the second loop. That is your problem,because after the execution of first loop the variable id will be assigned to the last member id which is basically 3. It will also be nice to show the profile.php.
Solution
change your url to this
<a href='profile.php?user='.$row['id']>VISIT PROFILE</a>
Related
im doing pagination for my table where there are around 1500 rows.
$numperpage = 50;
$countsql = $connect->prepare("select COUNT(id) from prana");
$countsql->execute();
$row = $countsql->fetch();
$numrecords = $row[0];
$numlinks = ceil($numrecords/$numperpage);
$page = $_GET['start'];
if (!$page) $page = 0;
$start = $page * $numperpage;
echo "start is ".$start.'<br>';
if(isset($_POST["action"]))
{
$query = "
SELECT * FROM prana WHERE product_status = '1' limit $start,$numperpage";
echo $output;
for ($i=0;$i<$numlinks;$i++)
{
$y = $i+1;
echo ' '.$y.' ';
}
from <a href="index.php?start='.$i.'"> im getting value of start in my url but for $page = $_GET['start'] it is showing Undefined array key start . and start variable stays at 0.
THIS IS MY REFERENCE :- https://www.youtube.com/watch?v=TI78ax23qZg
please help.
I hope this is the thing you are looking for, here is an working example and in details explanation of this example,
I have attached the code and Link for better understanding, have a look
have a nice day
https://www.myprogrammingtutorials.com/create-pagination-with-php-and-mysql.html
<html>
<head>
<title>Pagination</title>
<!-- Bootstrap CDN -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$conn=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$total_pages_sql = "SELECT COUNT(*) FROM table";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
$sql = "SELECT * FROM table LIMIT $offset, $no_of_records_per_page";
$res_data = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($res_data)){
//here goes the data
}
mysqli_close($conn);
?>
<ul class="pagination">
<li>First</li>
<li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
Prev
</li>
<li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
Next
</li>
<li>Last</li>
</ul>
</body>
</html>
To get rid of the warning, change your code from:
$page = $_GET['start'];
if (!$page) $page = 0;
Into:
$page = isset($_REQUEST['start']) ? $_REQUEST['start'] + 0 : 0;
Where $_REQUEST['start'] is used instead of $_GET['start'] to ensure logic works, even if method was not GET.
And the "+ 0" is to ensure it's numeric.
Also, start param is not set until one of links is clicked.
so I'm having some issues with a pagination script I have been building The pagination works fine however when i load a new page using the next button, my search term $search_term = "%" . $_POST['searchBar'] . "%"; is lost. is there any way to avoid this? or set the search term as a set value?
the url for the next page is like so - http://examplewebsite.com/user/Courses/SearchResultsPage.php?pn=2
Any help with this would be greatly appreciated.
The rest of the pagination script is below;
<?php
$mysqli = new mysqli('localhost', 'user', 'password','db');
if ($mysqli->connect_errno)
{
die('Database connection failed');
}
//$m->set_charset('utf8');
//here are my main changes
//turn errors on to develop, back off when you go live
error_reporting(E_ALL);
ini_set('display_errors', 1);
$search_term = "%" . $_POST['searchBar'] . "%";
$search_param = $_SESSION['$search_term']=$search_term;
$stmt =$mysqli->prepare("SELECT title, summary, id FROM course WHERE title
LIKE ?");
$stmt->bind_param("s", $search_param); //learn this
$stmt->execute();
$result = $stmt->get_result();
//This gets the number of rows in a query result
$rows = $result->num_rows;
//number of results per page
$rows_per_page = 10;
//shows last page
$last_page = ceil($rows/$rows_per_page);
if($last_page < 1){
$last_page = 1;
}
if(isset($_GET['pn'])){
$page_number = preg_replace('#[^0-9]#', '', $_GET['pn']);
} else {
$page_number = 1;
}
//makes sure page number is between limits of $page_number
if($page_number < 1){
$page_number = 1;
} else if($page_number > $last_page){
$page_number = $last_page;
}
// sets the value of items to view
$limit = 'LIMIT ' .($page_number -1) * $rows_per_page .',' .$rows_per_page;
//displays to the user the total number of results and the page numbers
$total_number_of_results = "Search Results (<b>$rows</b>)";
$page_user_is_on = "Page <b>$page_number</b> of <b>$last_page</b>";
//query again only grabbing the set number of rows depending on page number
$stmt = $mysqli->prepare("SELECT title, summary, id FROM course WHERE title LIKE ? ".$limit);
$stmt->bind_param("s", $search_param);
$stmt->execute();
$result = $stmt->get_result();
$list = '';
while($row = $result->fetch_assoc()){
$title = $row['title'];
$id = $row['id'];
//I'm assuming you want each link to be different here...
$list.='<p>' . $title . '</p>';
}
mysqli_close($mysqli);
//set up pagination
$pagination_controls = '';
if($last_page != 1){
if($page_number > 1){
$previous = $page_number - 1;
$pagination_controls .='previous ';
for($i = $page_number - 4; $i < $page_number; $i++)
{
if($i > 0){
$pagination_controls .= ''.$i.' ';
}
}
}
$pagination_controls.=''.$page_number.' ';
//clickable links to the left
for($i = $page_number+1; $i <= $last_page; $i++)
{
$pagination_controls .= ''.$i.' ';
if($i >= $page_number+4){
break;
}
}
if($page_number != $last_page){
$next = $page_number + 1;
$pagination_controls.=' Next';
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel='stylesheet' href='courses.css'>
</head>
<body>
<div class="header">
<h1>Search Results for - <?= $search_param ?></h1>
</div>
<div>
<h3> <?php echo $page_user_is_on ?> </h3>
<p><?php echo $list; ?></p>
<p><?php /* echo $search_result['summary']; //Where was this coming from? */?> </p>
</div>
<div id="pagination_controls"><?php echo $pagination_controls; ?></div>
</body>
</html>
Every time you click next, the page refreshes and since you dont use
<?php
session_start();
....
Replace these 2 lines
$search_term = "%" . $_POST['searchBar'] . "%";
$search_param = $_SESSION['$search_term']=$search_term;
with:
if(isset( $_POST['searchBar'])){
$search_term = "%" . $_POST['searchBar'] . "%";
$search_param = $_SESSION['search'] = $search_term ;
}
else {
$search_param = $_SESSION['search'];
}
at the top,$_SESSION['$search_term'] is lost
Also add an isset check for your POST
EDIT
I think your problem is that $search_term is set only on load then on the next page it is NULL and session is also set to NULL.
You guys are helping me greatly. Please I have created a members page which I want to use pagination but it shows only one member's id in all ids.
Here is an image:
Here is my code:
<html>
<head>
<title>MEMBERS</title>
<link rel="icon" href="guyt.gif" type="image/x-icon">
<link rel="stylesheet" type="text/css" media="all" href="style.css">
</head>
<body>
<?php include 'connect.php'; ?>
<?php include 'functions.php'; ?>
<?php include 'header.php'; ?>
<div id="sidebar">
<br>
<?php include 'lists.php'; ?>
</div>
</body>
</html>
here is the lists.php code which contains the pagination:
<?php
$per_page = 5;
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
$mem_query = mysql_query("SELECT id FROM register");
while($run_mem = mysql_fetch_array($mem_query)){
$id = $run_mem['id'];
$first = getuser($id, 'first');
$last = getuser($id, 'last');
}
$sql = "SELECT * FROM register";
$num_rows = mysql_num_rows(mysql_query($sql));
$num_pages = ceil($num_rows / $per_page);
$sql .= " LIMIT $start, $per_page";
$result = mysql_query($sql);
While($row = mysql_fetch_array($result)){
echo "<div class='header'><table width='98%'><tr><td><div align='left'>$first $last</div></td><td><div align='right'><a href='profile.php?user=$id'>VISIT PROFILE</a></div></td></tr></table></div><br>";
}
$prev = $page - 1;
$next = $page + 1;
echo "<hr>";
if($prev > 0)
echo "<a href='?page=$prev' class='box'>Previous</a></font> ";
if($page < ceil($num_rows/$per_page))
echo " <a href='?page=$next' class='box'>Next</a></font>";
?>
if I change this:
$sql = "SELECT * FROM register";
to this:
$sql = "SELECT * FROM register WHERE id='$id'";
it will display like that same user but just once. please help me
You should output the property of each row you are looping in :
While($row = mysql_fetch_array($result)){
echo "<div class='header'><table width='98%'><tr><td><div align='left'>" . $row['name'] . "</div></td><td><div align='right'><a href='profile.php?user=$id'>VISIT PROFILE</a></div></td></tr></table></div><br>";
}
That's assuming the name is stored in a "name" key. Adapt it to your db structure.
I'm trying with a E-Commerce website. But I'm having trouble. I want that when I click in the view details link of a product, the details of the product will be shown on the product_details.php page. But I can't transfer the product id to the product_details.php page.
My code is here...
<?php
include ("include/header.php");
?>
<?php
mysql_connect("localhost", "root", "") or die("problem with Connection");
mysql_select_db("finalproject");
$per_page = 3;
$pages_query = mysql_query("SELECT COUNT('product_id') FROM product");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset ($_GET['page'])) ? (int) $_GET['page'] : 1;
$start = ($page - 1 ) * $per_page;
$query = mysql_query("SELECT * FROM product LIMIT $start,$per_page");
while ($query_row = mysql_fetch_assoc($query))
{
echo "<b>$query_row[product_name]</b><br>";
echo "<b>Brand : </b> $query_row[product_brand] <br>";
echo "<b>Description : </b> $query_row[description] <br>";;
echo "<b>Price : </b> $query_row[price] <br>";
echo ('View details<br><br>') ;
?>
<form action="product_details.php?productId=<?php echo $row['product_id'];? >>" method="post">
<?php
}
$prev = $page - 1;
$next = $page + 1;
if (!($page <=1))
{
echo "<a href='buyproduct.php?page=$prev'>Prev</a> ";
}
if($pages >= 1)
{
for ($x=1; $x<=$pages; $x++)
{
echo ($x == $page) ? '<b>'.$x.'</b> ' : ''.$x.' ';
}
}
if (!($page >= $pages))
{
echo "<a href='buyproduct.php?page=$next'>Next</a> ";
}
?>
<?php
include ("include/footer.php");
?>
and my product_details.php is
<?php
include ("include/header.php");
?>
<?php
include ("database.php");
$productId = $_GET['productId'];
$sql = "SELECT * FROM product WHERE product_id = $productId";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<b>$row[product_name]</b><br>";
echo "<b>Brand : </b> $row[product_brand] <br>";
echo "<b>Description : </b> $row[description] <br>";;
echo "<b>Price : </b> $row[price] <br><br>";
"<br>";
}
?>
<?php
include ("include/footer.php");
?>
and when running in the browser when clicking to viw details in product_details.php the error is :
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\project2\product_details.php on line 17
Now what can I do....
In product_details.php the syntax of sql query is incorrect. Put $productId in quote.
The query sholu look like this..
$sql = "SELECT * FROM product WHERE product_id = '$productId'";
Hope this will help..
You have an error in your html...
<form action="product_details.php?productId=<?php echo $row['product_id'];? >>" method="post">
Should be...
<form action="product_details.php?productId=<?php echo $row['product_id'];?>" method="post">
Your sending an extra ">" with your product id
Im trying to make a pagination to my article page.
the problem is i want it to count the articles WHERE category = 1
ive worked with the script for sometime, but it still just shows a blank page no errors.
any suggestions why it wont work?
<?php
error_reporting(E_ALL); ini_set("display_errors", 1);
$db = mysql_connect("localhost", "root", "");
mysql_select_db("dirts_mysql", $db);
echo "<h1>articles</h1>";
$pr_page = 2;
$number = mysql_result(mysql_query("SELECT COUNT(*) FROM article WHERE category = 1"), 0) or die(mysql_error());
$show_from = (isset($_GET["visfra"]) && is_numeric($_GET["visfra"]) && $_GET["visfra"] < $number) ? $_GET["visfra"] : 0;
$query = mysql_query("SELECT * FROM article ORDER BY id DESC limit $show_from, $pr_page") or die(mysql_error());
while ($row = mysql_fetch_array($query)) {
?>
<link rel="stylesheet" type="text/css" href="style.css">
<div id="news">
<h2><u><? echo $row['name']; ?></u></h2>
<p>
<?php echo nl2br($row['description']); ?>...
</p>
<br/><br/>
[...]
</div>
<br>
<?php
}
if ($show_from > 0) {
$back = $show_from - $pr_page;
echo "<a href='?showfrom=$back'>Forrige</a> ";
}
$page = 1;
for ($start = 0; $number > $start; $start = $start + $pr_page) {
if ($show_from != $page * $pr_page - $pr_page) {
echo "<a href='?showfrom=$start'>$page</a> ";
} else {
echo $page . " ";
}
$page++;
}
if ($show_from < $number - $pr_page) {
$next = $show_from + $pr_page;
echo " <a href='?&showfrom=$next'>Næste</a>";
}
?>
add the following on the top of your code:
error_reporting(E_ALL);
ini_set("display_errors", 1);
On the otherside, I will suggest you to start using function to structure your code. :)