Pagination links not working - php

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>

Related

converting my old script to PDO results do not show?

ok si im not very fimiliar with PDO im just starting off, could someone help me here why my results will not display? the pages are working but no data would show ..
<?php
require_once 'db/_db.php';
$stmt = $db->prepare("SELECT * FROM article");
$stmt->execute();
$result = $stmt->fetchAll();
$total_rows = $stmt->rowCount();
//$allRecords = mysql_query('select * from article');
//$total_rows = mysql_num_rows($allRecords);
//$base_url = 'https://localhost/pagi/'; //
global $per_page;
$num_links = 4;
$total_rows = $total_rows;
$cur_page = 1;
if(isset($_GET['page']))
{
$cur_page = $_GET['page'];
$cur_page = ($cur_page < 1)? 1 : $cur_page; //if page no. in url is less then 1 or -ve
}
$offset = ($cur_page-1)*$per_page; //setting offset
$pages = ceil($total_rows/$per_page);
$start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
$end = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;
//$res = mysql_query("SELECT * FROM article ORDER BY id DESC LIMIT ".$per_page." OFFSET ".$offset);
$stm = $db->prepare("SELECT * FROM article ORDER BY id DESC LIMIT ".$per_page." OFFSET ".$offset);
$stm->execute();
$res = $stm->fetchAll();
$rows = $stm->rowCount();
if($rows < 1){
header("Location: news-events.php");
exit;
}
if(is_resource($result))
{
foreach( $res as $row ) {
$desc = $row["ne_article"];
$img = $row['ne_image'];
if(!empty($img)){
$img = '<br/><img src="uploads/images/'.$img.'" alt="" class="responsive-shrink">';
}else{
$img = '';
}
$youtube = $row["ne_youtube"];
if(!empty($youtube)){
$youtube = '<br/><div class="video-container"><iframe src="http://www.youtube.com/embed/'.$youtube.'"></iframe></div>';
}else{
$youtube = '';
}
//shortenString($row['ne_article']);
?>
<h4><?php echo $row['ne_title']; ?></h4>
<h5><b>Views:</b> <?php echo $row['views']; ?>, <b>Posted on:</b> <?php echo format_date($row['created']); ?></h5>
<?php echo $img; ?>
<?php echo $youtube; ?>
<p>
<br/><?php echo bbcode(nl2br(shortenString($desc))); ?>
</p>
<div id="pagelink">
Read more...</button>
</div>
<?php
}
}
?>
<div id="pagination">
<div id="pagiCount">
<br/><br/>
<?php
if(isset($pages))
{
if($pages > 1)
{ if($cur_page > $num_links) // for taking to page 1 //
{ $dir = "First";
echo '<span id="prev"> '.$dir.' </span>';
}
if($cur_page > 1)
{
$dir = "Prev";
echo '<span id="prev"> '.$dir.' </span>';
}
for($x=$start ; $x<=$end ;$x++)
{
echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':''.$x.' ';
}
if($cur_page < $pages )
{ $dir = "Next";
echo '<span id="next"> '.$dir.' </span>';
}
if($cur_page < ($pages-$num_links) )
{ $dir = "Last";
echo ''.$dir.' ';
}
}
}
?>
would really appreciate your time and help
You are doing nearly everything wrong. And you did with old mysql as well
To get the number of records in database you should never select all the rows, nor fetch them. You have to select the count already.
$total_rows = $pdo->query("SELECT count(1) FROM article")->fetchColumn();
is how it have to be done with PDO.
Besides, for the other query you should use prepared statements.
Finally, you have to take out all the useless verifications and start with foreach right away:
$stm = $db->prepare("SELECT * FROM article ORDER BY id DESC LIMIT ?,?");
$stm->execute([$offset,$per_page]);
$res = $stm->fetchAll();
foreach( $res as $row ) {
And read up on setting the error mode properly here.
check if you have problem with the quotes... when you echo html code inside ' ' if you have " " and again inside '' like this example echo ' " '' " ' you will have problem. you should use \' in order to avoid these problem here echo ' '.$dir.' ';

php blog pagination ranking

i am still a novice in PHP. please i need help. i am creating a blog page with pagination and i want the ranking to descend start from the last "id" from my database table not from the first "id" so that if i add a new post to my database, it will display at the top of my blog page. here is my coding:
<?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;
include 'connect.php';
$sql = "SELECT * FROM pagination";
$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 $row['name']. "<br>";
}
$prev = $page - 1;
$next = $page + 1;
echo "<hr>";
if($prev > 0)
echo "<a href='?page=$prev'>Previous</a> ";
if($page < ceil($num_rows/$per_page))
echo " <a href='?page=$next'>Next</a>";
?>
and same with my comment page, i want the last comment to display first. here is my coding:
<?php
include 'connect.php';
function getuser($id, $field) {
$query = mysql_query("SELECT $field FROM suggest WHERE id='$id'");
$run = mysql_fetch_array($query);
return $run[$field];
}
$readq = mysql_query("SELECT id FROM suggest");
while($run_p = mysql_fetch_array($readq)){
$id = $run_p['id'];
$name = getuser($id, 'name');
$title = getuser($id, 'title');
$post = getuser($id, 'post');
?>
<table width="60%">
<tr>
<td><b><font color="blue"><?php echo $title; ?></font><br><br><?php echo $post; ?><br>Suggestion From: <font color="blue"><?php echo $name; ?></font></b><hr width="50%"></td>
</tr>
</table>
<?php
}
?>
SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC
you can use the ORDER BY column_name ASC or DESC in you query to display it the ways you want.
For latest records you need to use ORDER BY DESC in your query.
You can be use this query:
SELECT id FROM suggest
Like that:
SELECT id FROM suggest ORDER BY id DESC
Side note: i suggest to use mysqli_* or PDO because mysql_* is deprecated and closed in PHP 7.

How to go product details page from product page

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

Passing query to next page (pagination)

Hi what I'm trying to do is make a pagination for the whole and gives also a chance the user to search for a name and returns a paginated result.
Here is my code
<form method = "POST">
<td>
Search:<input name="search_name" type="text" id="t_searchkey" style="width:35%;" placeholder = "Name">
<input type="submit" name="b_find" id="b_find" title="Find" value = "Find">
</td>
PHP code:
<?php
if(!isset($_POST['b_find']))
{
$query = "SELECT * FROM reginformation
WHERE deleted = 0";
$search_name = "";
}
if(isset($_POST['b_find']))
{
$search_name = trim($_POST['search_name']);
$query = "SELECT * FROM reginformation WHERE name = '$search_name' AND deleted = 0 ";
}
?>
<?php
$result = mysql_query($query) or die(mysql_error());
?>
<?php
$num_rec_per_page=5;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
if (isset($_GET["search"]))
{
$search_name = $_GET['search'];
}
$start_from = ($page-1) * $num_rec_per_page;
$query2 =$query . " LIMIT $start_from, $num_rec_per_page";
echo "query2 $query2";
$rs_result = mysql_query ($query2); //run the query
while ($row = mysql_fetch_assoc($rs_result)) {
echo "<tr onClick =window.location='infodetailsresp.php?id=$row[regID]'><td>$row[name]</td><td>$row[emailadd]</td><td>$row[contactno]</td><td>$row[event]</td><td>$row[date_register]</td></tr>";
};
//$sql = "SELECT * FROM reginformation";
$rs_result = mysql_query($query); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
?>
<?php
echo "<a href='reglistresp.php?page=1&search=$search_name'>".'|< '."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='reglistresp.php?page=".$i."&search=$search_name'>". $i ."</a> ";
};
echo "<a href='reglistresp.php?page=$total_pages&search=$search_name'>".' >| '."</a> "; // Goto last page
?>
What happens when I click the next page it gives me the query in the
if(!isset($_POST['b_find']))
So what should be changed so I can get my desired query to the next page?

Page system where category = 1

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. :)

Categories