php blog pagination ranking - php

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.

Related

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?

Can anyone help me out to try pagination for the below code...!

Well below i have attached the table structure kindly go through it.
I would like to get serial no as my 1st column followed by id and name. serial no should continue for pagination rather then starting from 1st.
Thanks in advance..
Table structure is
create table departments (
id INT(20) AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL);
<html>
<head>
<?php
$db_connection = new mysqli("localhost","root","","emp_app");
if($db_connection->connect_error)
die("connection failed".$db_connection->connect_error);
?>
</head>
<body>
<table>
<tr>
<th>Serial no </th>
<th>id</th>
<th>name</th>
</tr>
<?php
$sql_query = "select * from departments";
$result = $db_connection->query($sql_query);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$rows["id"]."<td>";
echo "<td>".$rows["name"]."<td>";
echo "<tr>";
}
}
?>
</table>
</body>
here is a good example how to add pagination in php pagination in php
Refrence link is using depricated mysql query you need to replace with mysqli query
$num_rec_per_page=10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM departments LIMIT $start_from, $num_rec_per_page";
$rs_result = $db_connection->query ($sql); //run the query
$serial=1;
while ($row = $result->fetch_assoc) {
//code here
echo $serial;
$serial++;
};
$sql = "SELECT * FROM departments"; //select query for total records
$rs_result = $db_connection->query($sql); //run the query
$total_records =$rs_result->num_rows; //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='pagination.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='pagination.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
?>
You could append the URL to add a get variable that will serve as your pagination number i.e. youurl.com/?pagination=1 or 2 or 3
Then in your code add
$pagenumber = $_GET["pagination"];
$offset = $pagenumber * 5; //The number 5 is how many results per page if you wanted 10 results per page this would be 10
Where you have
$sql_query = "select * from departments";
You can change to
$sql_query = "select * from departments" LIMIT 5 OFFSET '$offset';
Which is basically saying; only give me 5 results starting from the pagination row (which is 5 results x page 2 or 3
<html>
<head>
<?php
$db_connection = new mysqli("localhost","root","","emp_app");
if($db_connection->connect_error)
die("connection failed".$db_connection->connect_error);
$num_rec_per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * $num_rec_per_page;
?>
</head>
<body>
<table>
<tr>
<th>Serial no </th>
<th>id</th>
<th>name</th>
</tr>
<?php
$sql_query = "SELECT * FROM departments LIMIT $start_from, $num_rec_per_page";
$result = $db_connection->query($sql_query);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$rows["id"]."<td>";
echo "<td>".$rows["name"]."<td>";
echo "<tr>";
}
}
$sql = "SELECT * FROM departments"; //select query for total records
$rs_result = $db_connection->query($sql); //run the query
$total_records =$rs_result->num_rows; //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='index.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='index.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='index.php?page=$total_pages'>".'>|'."</a> ";
?>
</table>
</body>

Using keyword LIMIT in MySQL

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("data_wis1") or die(mysql_error());
if(isset($_GET["id"])) {
$id = $_GET["id"];
$sql = "DELETE FROM info WHERE ID = '".$id."'";
mysql_query($sql) or die(mysql_error());
}
if(isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page = 1;
}
$start_from = ($page-1) * 1;
$sql = "SELECT * FROM info LIMIT 0, 1";
$query = mysql_query($sql) or die(mysql_error());
?>
<html>
<table>
<?php
for($i = 0; $i <mysql_num_rows($query); $i++) {
$id = mysql_result($query, $i, "ID");
$caseStatus = mysql_result($query, $i, "Case_Status");
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>".$caseStatus."</td>";
echo "<td><a href='del.php?id=".$id."'><input type='button' value='Delete'></a></td>";
echo "</tr>";
}
?>
</table>
<?php
$sql = "SELECT COUNT(ID) FROM info";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($query);
$total_records = $row[0];
$total_pages = ceil($total_records / 1);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='del.php?page=".$i."'>".$i."</a> ";
}
?>
</html>
My problem is that whenever i click page 2,3,4... the display doesn't change. Page 2,3,4 gets the display of page 1. It should be like this. For example, page 1 should display ID = 1 and case status = open. Page 2 should display ID = 2 and case status = close and so on.
You never used $start_from in anywhere. I think $sql = "SELECT * FROM info LIMIT '".$start_from."', 1"; is the answer

PHP Filter Drop Down Boxes not showing results where isset is not set

Hi I am trying to build a filter from drop down boxes to select which results are returned from a mysql database.
Once i select the values that i want and then click the submit button the database results are filtered correctly and shows only the results that are determined by my selection.
The problem i am having is that when i load the page for the first time with out selecting any values and clicking on submit nothing is shown.
below is the code for the drop down boxes.
<form name="form1" method="post" action="visitor_list7.php">
<?php
if (isset($_POST['pagination']) && $_POST['pagination'] != "pagination") {
$select1 = $_POST['pagination'];
}
?>
<select name="pagination">
<?php
// Get records from database (table "name_list").
$list1=mysql_query("select * from pagination_drop_down order by id asc");
// Show records by while loop.
while($row_list1=mysql_fetch_assoc($list1)){
$pagination = $_POST['pagination']; ?>
<option value="<?php echo $row_list1['value']; ?>" <?php if($row_list1['value']==$select1){ echo "selected"; } ?>><?php echo $row_list1['title']; ?></option>
<?php
}
?>
</select>
<?php
if (isset($_POST['returning']) && $_POST['returning'] != "returning") {
$select2 = $_POST['returning'];
}
?>
<select name="returning">
<?php
// Get records from database (table "name_list").
$list2=mysql_query("select * from repeater_drop_down order by id DESC");
// Show records by while loop.
while($row_list2=mysql_fetch_assoc($list2)){
$returning = $_POST['returning']; ?>
<option value="<?php echo $row_list2['value']; ?>" <?php if($row_list2['value']==$select2){ echo "selected"; } ?>><?php echo $row_list2['title']; ?></option>
<?php
}
?>
</select>
<?php
if (isset($_POST['referrer']) && $_POST['referrer'] != "referrer") {
$select = $_POST['referrer'];
}
?>
<select name="referrer">
<option value="">Referrer</option>
<?php
// Get records from database (table "name_list").
$list=mysql_query("select DISTINCT referrer from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(referrer,'')) <> '' order by referrer DESC");
// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
$referrer = $_POST['referrer']; ?>
<option value="<?php echo $row_list['referrer']; ?>" <?php if($row_list['referrer']==$select){ echo "selected"; } ?>><?php echo $row_list['referrer']; ?></option>
<?php
}
?>
</select>
<?php
if (isset($_POST['sortby']) && $_POST['sortby'] != "sortby") {
$select3 = $_POST['sortby'];
}
?>
<select name="sortby">
<?php
// Get records from database (table "name_list").
$list4=mysql_query("select * from sortby_drop_down order by id asc");
// Show records by while loop.
while($row_list4=mysql_fetch_assoc($list4)){
$sortby = $_POST['sortby']; ?>
<option value="<?php echo $row_list4['value']; ?>" <?php if($row_list4['value']==$select3){ echo "selected"; } ?>><?php echo $row_list4['title']; ?></option>
<?php
}
?>
</select>
<input type="submit" name="button" id="button" value="Submit">
</form>
And here is the code for my elseif statements which determine which query to run.
$pagination = $_POST['pagination'];
$returning = $_POST['returning'];
$referrer = $_POST['referrer'];
$sortby = $_POST['sortby'];
$tableName="masterip_details";
$targetpage = "visitor_list6.php";
if(isset($_GET['pagination'])) {
$limit = "$pagination";
} else {
$limit = $pagination;
}
//SELECT FROM THE MYSQL DATABASE IF THE $RETUTNING AND $REFERRING VARIABLES ARE SELECTED INT HE DROP DOWN BOXES //
if (isset($returning, $referrer)){
$query = "SELECT COUNT(*) as num FROM $tableName Where client_id ='$client_id' AND country_code ='GB' AND type='1' AND repeater='$returning' AND referrer ='$referrer'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
$query1 = "SELECT * FROM $tableName Where client_id ='$client_id' AND country_code ='GB' AND type='1' AND repeater='$returning' AND referrer ='$referrer' Order by $sortby DESC LIMIT $start, $limit";
$result = mysql_query($query1);
echo "variables all set";
echo "$returning";
echo "$referrer";
}
//SELECT FROM THE DATABASE IF ONLY THE RETURNING DROP DOWN BOX VARIABLE IS SET //
elseif (isset($returning)){
$query = "SELECT COUNT(*) as num FROM $tableName Where client_id ='$client_id' AND country_code ='GB' AND type='1' AND repeater='$returning' ";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName Where client_id ='$client_id' AND country_code ='GB' AND type='1' AND repeater='$returning' Order by $sortby DESC LIMIT $start, $limit";
$result = mysql_query($query1);
echo " returning variables set";
}
// SELECT FROM THE DATABASE IF THE $REFERRER VARIABLE IS SET FROM THE DROP DOWN BOXES
elseif (isset($referrer)){
$query = "SELECT COUNT(*) as num FROM $tableName Where client_id ='$client_id' AND country_code ='GB' AND type='1' AND referrer ='$referrer'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName Where client_id ='$client_id' AND country_code ='GB' AND type='1' AND referrer ='$referrer' Order by $sortby DESC LIMIT $start, $limit";
$result = mysql_query($query1);
echo "$client_id";
echo " referrer variables set";
}
else {
//SELECT FROM THE DATABAS IF NO VARIABLES ARE SET FROM THE DROP DOWN BOXES
$query = "SELECT COUNT(*) as num FROM $tableName Where client_id ='$client_id' AND country_code ='GB' AND type='1'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName Where client_id ='$client_id' AND country_code ='GB' AND type='1' Order by timedate DESC LIMIT $start, $limit";
$result = mysql_query($query1);
echo "$client_id";
echo " no variables set";
}
I have never done this before and still not sure if this is the correct way to do this any so any sugestions or guidance would be appreciated.
Solved,
I add the variable pagination outside of the isset when it should have been a value
OLD CODE WITH MISTAKE
if(isset($_GET['pagination'])) {
$limit = "$pagination";
} else {
$limit = $pagination;
}
NEW CODE WORKING PERFECTLY
if(isset($_GET['pagination'])) {
$limit = "$pagination";
} else {
$limit = 30;
}

Pagination links not working

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>

Categories