Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have no idea i tried to connect the DB with my panel but it gives some error any help would be appreciated...................................................................................................................
<?php
if (!(isset($pagenum)))
{
$pagenum = 1;
}
$qry = "SELECT * FROM posts LIMIT 0,3";
$result = mysqli_query($qry,$con);
$rows = mysqli_num_rows($qry);
$page_rows = 3;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$j=0;
$qry ="SELECT * FROM posts $max";
$result = mysqli_query($con,$qry);
//This is where you display your query results
while($rows = mysqli_fetch_array($result))
{
$j++;
echo "<p>";
// This shows the user what page they are on, and the total number Query and Results of pages
echo " --Page $pagenum of $last--
<p>";
// First we check if we are on page one. If we are then we don't need a
//link to the previous page or the first page so we do nothing. If we aren't
//then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a
href='{$_SERVER['PHP_SELF']}?pagenum=1'>
<<-First</a>
";
echo " ";
$previous = $pagenum-1;
echo " <a
href='{$_SERVER['PHP_SELF']}?
pagenum=$previous'> <-Previous</a>
";
}
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page,
//and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a
href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next
-></a> ";
echo " ";
echo " <a
href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last
->></a> ";
}
?>
echo $_SESSION['username'];
Would do the trick if you are saving the $_POST username within the session upon successful login. We ideally really need to see the php code you are working with but this is a basic idea of how to accomplish what you're after
Related
I am working on a university project, which is based on e-shopping, i tried this code to implement my result, this gives no error but it does not work when I press next page link or "button" please help any help would be appreciated
<?php
$con = mysqli_connect("localhost","root","","php184_proj_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (!(isset($pagenum)))
{
$pagenum = 1;
}
//Here we count the number of results
//Edit $qry to be your query
$qry = "SELECT * FROM posts";
$result = mysqli_query($con,$qry);
$row = mysqli_num_rows($result);
//This is the number of results displayed per page
$page_rows = 5;
//This tells us the page number of our last page
$last = ceil($row/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1; //Pagination of MySQL Query Results Setting the Variables
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$j=0;
$qry = "SELECT * FROM posts $max";
$result = mysqli_query($con,$qry);
while($row = mysqli_fetch_array($result))
{
$j++;
echo "<p>";
// This shows the user what page they are on, and the total number Query and Results of pages
echo " --Page $pagenum of $last--
<p>";
// First we check if we are on page one. If we are then we don't need a
//link to the previous page or the first page so we do nothing. If we aren't
//then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a
href='{$_SERVER['PHP_SELF']}?pagenum=1'>
<<-First</a>
";
echo " ";
$previous = $pagenum-1;
echo " <a
href='{$_SERVER['PHP_SELF']}?
pagenum=$previous'> <-Previous</a>
";
}
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page,
//and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a
href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next
-></a> ";
echo " ";
echo " <a
href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last
->></a> ";
}
?>
If you visit this URL: http://example.com/somepage.php?key=val, you don't automatically get a variable $key inside PHP. Instead, you have to use $_GET['key'], which will hold the value. (in this case: 'val')
So, somewhere in the beginning of your code, add the following:
if (isset($_GET['pagenum']) && $_GET['pagenum'] >= 1) {
$pagenum = (int) $_GET['pagenum'];
} else {
$pagenum = 1;
}
Not only does this create the $pagenum variable and give it the value from the URL, it also makes sure that the value is a valid number. If not, or if the URL doesn't contain a pagenum, it is set to 1.
Possible scenario's:
pagenum contains not an integer, but a string (might even be a SQL injection attempt)
pagenum is a negative number, or 0
pagenum isn't set at all
In all of the above cases, pagenum is set to 1.
If pagenum contains a float (for instance 1.5.), the value will be cast to an integer. In the case of 1.5, pagenum will become 1.
Remember, always make sure you sanitize user input.
I am having issues with some pagination with data i get from a MySQL database in PHP.
My code is below. Basically what happens is it creates the right amount of pages however each page shows the same data and doesn't even show only 5 rows per page.
I'm really stuck. This is my first time trying pagination. Any help would be greatly appreciated.
$pagenum = $_GET['pagenum'];
if (!(isset($pagenum)))
{
$pagenum = 1;
} else
{
$pagenum = $_GET['pagenum'];
}
//Count the number of results.
$data = mysql_query("SELECT * FROM `articles` WHERE `content` = '' AND `requestedby` != '$id'") or die(mysql_error());
$rows = mysql_num_rows($data);
//Set the number of results to be displayed per page.
$page_rows = 5;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
} elseif ($pagenum > $last)
{
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'LIMIT ' . ($pagenum - 1) * $page_rows .',' .$page_rows;
//This is the query again, the same one... the only difference is we add $max into it.
$data_p = mysql_query("SELECT * FROM `articles` WHERE `content` = '' AND `requestedby` != '$id' '$max'") or die(mysql_error());
//Work out writers earnings based on prices.
//100 Words - $1.25
//300 Words - $2.50
//500 Words - $4.00
//700 Words - $5.50
//1000 Words - $8.00
$_100earnings = "0.65";
$_300earnings = "1.25";
$_500earnings = "2.50";
$_700earnings = "3.00";
$_1000earnings = "5.00";
?>
<!-- main -->
<div id="main">
<center><h2>Write Articles</h2></center>
<br />Available Projects:
<table border="1">
<tr>
<td>Title:</td>
<td>Length:</td>
<td>Writers Earnings:</td>
</tr>
<?php
//This is where you display your query results
while($info = mysql_fetch_array($data_p))
{
echo "<tr>";
echo "<td>" . $info['keywords'] . "</td>";
echo "<td>" . $info['length'] . "</td>";
switch ($info['length'])
{
case 100:
$writersearnings = $_100earnings;
break;
case 300:
$writersearnings = $_300earnings;
break;
case 500:
$writersearnings = $_500earnings;
break;
case 700:
$writersearnings = $_700earnings;
break;
case 1000:
$writersearnings = $_1000earnings;
break;
}
echo "<td>$" . $writersearnings . "</td>";
//echo $info['Name'];
echo "</tr>";
}
?>
</table>
<br /><br />
<?php
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
} else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
} else
{
$next = $pagenum + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
use this code, remove ' around $max in query, when you add ' around $max the query becomes select.... from.... where..... 'LIMIT.....' and it fails the query.
$data_p = mysql_query("SELECT * FROM `articles` WHERE `content` = '' AND `requestedby` != '$id' $max") or die(mysql_error());
You need to specify LIMIT in your query LIMIT offset,count
$data_p = mysql_query("SELECT * FROMarticlesWHEREcontent= '' ANDrequestedby!= '$id' '$max'") or die(mysql_error());
Remove the ' quotes from '$max'
$data_p = mysql_query("SELECT * FROM articles WHERE content= '' AND requestedby!= '".$id."' ".$max) or die(mysql_error());
i searched about php pagination and i found a site who explains the codes very well, my question is about the codes, everytime i click "next" link, page 2 has to results. i dont understand why page 2 has no results. here is the code:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("kp_and_harang") or die(mysql_error());
if (!(isset($pagenum)))
{
$pagenum = 1;
}
$data = mysql_query("SELECT * FROM students") or die(mysql_error());
$rows = mysql_num_rows($data);
$page_rows = 4;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$data_p = mysql_query("SELECT * FROM students $max") or die(mysql_error());
while($info = mysql_fetch_array( $data_p ))
{
Print $info['surname'];
echo "<br>";
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
echo " ---- ";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
First of all, unless absolutely necessary it's not advisable to use SELECT * you should use specific fields. Right now you're making a full search on the same table twice. Select one field only if you want to count the rows only.
You should also use PDO functions instead of mysql_* which are deprecated.
Are you sure there should be a second page? How many records are there in students table?
To debug your code dump query results and check the "SELECT * FROM students $max" query.
You put the page number in the url, but you never retrieve it from the url?
I changed this part:
if (!(isset($pagenum)))
{
$pagenum = 1;
}
to this:
if (!(isset($_GET['pagenum'])))
{
$pagenum = 1;
} else {
$pagenum = $_GET['pagenum'];
}
And it seems to work perfectly.
EDIT
To just get the total count of rows you don't have to fetch everything. You can just do the following:
$result = mysql_query("SELECT count(*) FROM students") or die(mysql_error());
$rows = mysql_fetch_row($result);
Pagination really confuses me. My code works but it only shows the first page. The next page doesn't work. I just want to show 3 records per page.
Do you have to have another query to show the results of the second page?
<?php
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db_pet") or die(mysql_error());
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}
//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM tb_pet") or die(mysql_error());
$rows = mysql_num_rows($data);
//This is the number of results displayed per page
$page_rows = 3;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM tb_pet $max") or die(mysql_error());
//This is where you display your query results
while($info = mysql_fetch_array($data_p))
{
Print $info['pet_name'];
echo "<br>";
}
echo "<p>";
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
if (!(isset($pagenum))) {
$pagenum = 1;
}
you have not set $pagenum any where on this line instead it should be
if (!(isset($_GET['pagenum']))) {
$pagenum = 1;
}else{
$pagenum = $_GET['pagenum'];
}
it is just fix for your code, you need to improve the way you write code and try using PDO instead mysql functions
I have a PHP script which works with jQuery to provide search suggestions. It pulls results from a MySQL database. However, I only want 5 results to display at once for the letter the user has typed but it seems all of the results appear. Why could this be?
My code is:
<p id="searchresults"><?php
$db=new mysqli('localhost','username','password','database');
if(isset($_POST['queryString'])){
$queryString=$db->real_escape_string($_POST['queryString']);
if(strlen($queryString)>0){
$query = $db->query("SELECT * FROM search s WHERE name LIKE '%" . $queryString . "%'");
if($query){
while ($result = $query ->fetch_object()){
echo '<a href="/search/'.$result->name.'/1/">';
$name=$result->name;
echo ''.$name.'';
}
}
}
}
?></p>
I hope you can understand what I am trying to describe.
Change "SELECT * FROM search s WHERE name LIKE '%" . $queryString . "%'"
to "SELECT * FROM search s WHERE name LIKE '%" . $queryString . "%' LIMIT 5"
if you want to limit it to 5 results.
You need to add pagination code to your page:
There is the sample code:
<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("address") or die(mysql_error());
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}
//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM topsites") or die(mysql_error());
$rows = mysql_num_rows($data);
//This is the number of results displayed per page
$page_rows = 4;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM topsites $max") or die(mysql_error());
//This is where you display your query results
while($info = mysql_fetch_array( $data_p ))
{
Print $info['Name'];
echo "<br>";
}
echo "<p>";
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
//just a space
echo " -- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
Source: www.twitter.com/ZishanAdThandar