I'm having this error on my code
Notice: Undefined offset: 0 in C:\xampp\htdocs\rekmovie\paginator.php on line 29
here is the full code
<?php
$con=mysql_connect("localhost","root","");
$conn=mysql_select_db('rektechnologies');
if (!$con)
{
die('Could not connect: ' . mysql_error());
} // include your code to connect to DB.
if (!empty($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
$sql = "select * from videos order by id desc LIMIT $start_from, 10";
$rs_result = mysql_query($sql,$con);
?>
<table>
<tr><td>Name</td><td>Phone</td></tr>
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr>
<td><? echo $row["path"]; ?></td>
<td><? echo $row["description"]; ?></td>
</tr>
<?php
};
?>
<?php
$sql = "SELECT COUNT(*) FROM videos";
$rs_result = mysql_query($sql,$con);
$row = mysql_fetch_assoc($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='paginator.php?page=".$i."'>".$i."</a> ";
};
?>
</table>
can anyone help me with the error
Change
$row = mysql_fetch_assoc($rs_result);
to:
$row = mysql_fetch_row($rs_result);
mysql_fetch_assoc returns an associative array, but you're trying to access $row[0], which expects an indexed array.
You are using mysql_fetch_assoc, but then trying to access the column name by integer
$row = mysql_fetch_assoc($rs_result);
$total_records = $row[0];
use either
$row = mysql_fetch_assoc($rs_result);
$total_records = $row['COUNT(*)'];
or
$row = mysql_fetch_array($rs_result);
$total_records = $row[0];
change the query to SELECT COUNT(*) as some_alias FROM videos
and then use $total_records = $row['some_alias'];
Related
Sorting is work perfectly before I combine it with pagination. But right now I am facing with the problem this when to pass value of sorting to pagination:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?page=1 ASC LIMIT 0, 10' at line 3
Below is my code:
<?php
session_start();
$host = 'localhost';
$user = 'root';
$pass ='password';
$name = 'dbname';
$con = new mysqli ($host,$user,$pass,$name);
?>
<html>
<h2>Your Redeemed Ticket (<?php echo $_SESSION['username'];?>):</h2>
<table border='1' align='center' cellpadding='10'>
<tr align = "center">
<td>
Purchase ID
<td>
Ticket Type
<td>
Quantity
<td>
Date
</tr>
</html>
<?php
// default sorting
if(isset($_GET['sort'])){
$sortOrder = $_GET['sort'] ? :0;
/*Pagination*/
$limit = 10;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $limit;
$username = $_SESSION['username'];
$data =
"
SELECT *
FROM redeem
WHERE redeem_by = '$username' ORDER BY ".$sortOrder." ASC LIMIT $start_from, $limit
";
$result = $con->query($data);
if ($result === false)
die (mysqli_error($con));
while($rows = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'>";
echo $rows['purchase_id'];
echo "<td align='center'>";
echo $rows['ticket_type'];
echo "<td align='center'>";
echo $rows['qty'];
echo "<td align='center'>";
echo $rows['date'];
echo "</tr>";
}
echo "</table>";
/*PAGE NUMBER DISPLAY*/
$data2 = "SELECT COUNT(redeem_by) FROM redeem";
$rs_result = mysqli_query($con,$data2);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / $limit);
$pagLink = '';
for ($i=1; $i<=$total_pages; $i++)
{
$pagLink .="<a href='user_history.php?sort=".$sortOrder."?page=$i'>" .$i."/</a>";
};
echo "<center>".$pagLink."</center>";
/*LOGOUT*/
if(isset($_POST['logout']))
{
session_destroy();
header ("location:login.php");
exit();
}
}
?>
There are a few weird issues in your code, but the problem with your screwed up GET variable can probably be traced to here:
<a href='user_history.php?sort=".$sortOrder."?page=$i'>"
Obviously when you have url parameters, you delineate the starting point with the '?' but after that you need to seperate additional parameters with the '&'.
So it should be:
<a href='user_history.php?sort=".$sortOrder."&page=$i'>"
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?
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>
<?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
i am writing code of pagination & i got an error.
this is my code :
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("Admin") or die(mysql_error());
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * 2;
$sql = "SELECT * FROM events ORDER BY event ASC LIMIT $start_from, 2";
$result = mysql_query ($sql) or die(mysql_error());
$num=mysql_numrows($result);
$x=0;
?>
<table>
<tr><td>Event</td><td>Types</td></tr>
<?php
while ($x<$num) {
$row1 = mysql_result($result,$x,'event');
$row2 = mysql_result($result,$x,'types');
?>
<tr>
<td><? echo $row1; ?></td>
<td><? echo $row2; ?></td>
</tr>
<?php
$x++;
}
?>
</table>
<?php
$sql = "SELECT COUNT(event) FROM events";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($result);
$total_records = $row[0];
$total_pages = ceil($total_records / 2);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
}
?>
now i got an error.
object not found
Note that instead of terrible while ($x<$num) you should use way more neat while($row=mysql_fetch_array($res))
so, make it
<?
$per_page = 2;
$page = 1;
if (isset($_GET['page'])) $page = $_GET['page'];
$start_from = ($page-1) * $per_page;
$sql = "SELECT * FROM events ORDER BY event ASC LIMIT $start_from, $per_page";
$res = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
?>
<table>
<tr><td>Event</td><td>Types</td></tr>
<?php
while($row=mysql_fetch_array($res)) {
?>
<tr>
<td><? echo $row['event'] ?></td>
<td><? echo $row['types'] ?></td>
</tr>
<?php
}
?>
</table>
<?php
$sql = "SELECT COUNT(event) FROM events";
$res = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
$row = mysql_fetch_row($res);
$total_records = $row[0];
$total_pages = ceil($total_records / $per_page);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
}
?>
mysql_query returns false - i.e. your query failed and you are passing a wrong argument to mysql_num_rows. I think you have a typo in the table name in the SQL query, it should be events instead. Generally, you should check if the query succeeded:
$result = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);