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. :)
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.
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.
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>
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
I've searched through the web and can't really find what's the problem with my code. The next and prev links are not working. it only gets the first entry.
Hope you guys can help with this! Thanks.
<?php
// ROWS DISPLAYED PER PAGE
$rows_per_page = 1;
// GET PAGE NUMBER
$page = $_GET['page'];
$offset = (!empty($page)) ? $page : $page = 1;
// URL CLEAN UP
$self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
$self = str_replace("page={$offset}", "", $self);
// GET LIST OF STATES
$offset = ($page) ? ($page - 1) * $rows_per_page : 0;
$id = $_GET['id'];
$sql = "SELECT * FROM updates WHERE update_categoryID = '$id' ORDER BY update_date DESC LIMIT {$offset},{$rows_per_page}";
$result = mysql_query($sql)or die('Error, query failed');
// GET NUMBER OF PAGES
$query1 = "SELECT * FROM updates WHERE update_categoryID = '$id'";
$result1 = mysql_query($query1)or die('Error, query failed');
$total = mysql_num_rows($result1);
$NumPgs = ceil($total/$rows_per_page);
while($row = mysql_fetch_assoc($result))
{
?>
<h2><?php echo $row['update_title']; ?></h2>
<p class="datetime"><?php echo $row['update_date'];?></p>
<br>
<p class="post"><?php echo $row['update_content'];?></p>
Post a Comment
<?php
}
?>
<span style="float:right">
<? if ($NumPgs > 0 && $page!=1) {
echo "<<Prev "; } ?>
[Page <?php echo $page; ?>]
<? if ($page < $NumPgs) {
echo " Next>>"; } ?>
<b><? echo $offset+1;?> to <? echo $offset+$rows_per_page;?>, of <? echo $total; ?> Entries</b>
</span>
</div>
Uhm maybe your page param is appended to the query string, try this :
$self = $_SERVER['PHP_SELF']."?".
preg_replace( $_SERVER[ 'QUERY_STRING' ], 'page=[0-9]+', '');
instead of :
// URL CLEAN UP
$self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
$self = str_replace("page={$offset}", "", $self);
Short open tags are not working in your case.
http://us3.php.net/echo
http://us3.php.net/manual/en/ini.core.php#ini.short-open-tag
Try the following code.
<?php
// ROWS DISPLAYED PER PAGE
$rows_per_page = 1;
// GET PAGE NUMBER
$page = $_GET['page'];
$offset = (!empty($page)) ? $page : $page = 1;
// URL CLEAN UP
$self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
$self = str_replace("page={$offset}", "", $self);
// GET LIST OF STATES
$offset = ($page) ? ($page - 1) * $rows_per_page : 0;
$id = $_GET['id'];
$sql = "SELECT * FROM updates WHERE update_categoryID = '$id' ORDER BY update_date DESC LIMIT {$offset},{$rows_per_page}";
$result = mysql_query($sql)or die('Error, query failed');
// GET NUMBER OF PAGES
$query1 = "SELECT * FROM updates WHERE update_categoryID = '$id'";
$result1 = mysql_query($query1)or die('Error, query failed');
$total = mysql_num_rows($result1);
$NumPgs = ceil($total/$rows_per_page);
while($row = mysql_fetch_assoc($result))
{
?>
<h2><?php echo $row['update_title']; ?></h2>
<p class="datetime"><?php echo $row['update_date'];?></p>
<br>
<p class="post"><?php echo $row['update_content'];?></p>
Post a Comment
<?php
}
?>
<span style="float:right">
<? echo ($prev = ($NumPgs > 0 && $page!=1) ? "Prev ":
"Prev "); ?>
[Page <?php echo $page; ?>]
<? echo ($next = ($page < $NumPgs) ? "Next":
"Next");?>
<b> <? echo $offset+1?> to <?=$offset+$rows_per_page?>, of <? echo $total?> Entries</b>
</span>
</div>