PHP from external anchor to internal - php

I have modified my project into a one page website type.
Part of the code is this:
for ($i = 1; $i <= $total_pages; $i++)
{
echo ''.$i.'';
}
I need that code above to work with an internal link for example:
From here:
echo ''.$i.'';
TO
echo ''.$i.'';
Here is the full source code:
$per_page = 6;
$result = mysql_query("SELECT * FROM mytable ORDER BY date DESC");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo '<strong>View Page:</strong> ';
echo '<div data-role="controlgroup" data-type="horizontal">';
for ($i = 1; $i <= $total_pages; $i++)
{
echo ''.$i.'';
//echo ''.$i.'';
}
echo '</div>';
I need to change this:
echo ''.$i.'';
so it's internal.

Short answer: This can't be done in PHP alone. You will need send AJAX requests to a PHP script that grabs the data.
Basically, this will involve creating some javascript that listens for any changes to the hash (that's the part after the #);
(function(window) {
var hash = window.location.hash;
setTimeout(function() {
if(window.location.hash !== hash) {
hash = window.location.hash;
//Grab and update page with ajax...
}
setTimeout(arguments.callee, 50);
}, 50);
})(window);
For AJAX basics, check W3Schools. And please, please respect the back button.

Without reloading the page:
foo
If you modify the query string in the link, the page will reload and jump to the anchor:
foo
Or with JQuery:
<script type="text/javascript">
$(document).ready(function(){
window.location.hash = 'my-anchor';
});
</script>

Related

php pagination on second page not working

$dwdb=mysqli_connect("localhost","root","","dw");
//this is my page number
$page=(isset($_GET['page']) && $_GET['page']>0)?$_GET['page']:1 ;
//this is my cat_id
$new=(isset($_GET['year']) && $_GET['year']>0)?$_GET['year']:1 ;
$perpage=2;
$limit=($page > 1)?($page*$perpage)-$perpage:0;
$query=mysqli_query($dwdb,"select *from movies where y_id='$new' limit {$limit},{$perpage}");
while($result=mysqli_fetch_array($query)){
$id=$result['m_id'];
$name=$result['title'];
$img=$result['image'];
echo"<div><a href='downloadpage.php?yc=$id'>$id.....$name<br><img src='i/image/$img' style='height:200px;width:200px;'/></a></div>";
}
$query1=mysqli_query($dwdb,"select *from movies where y_id='$new'");
$total=mysqli_num_rows($query1);
$pages=ceil($total/$perpage);
echo "<a href='index1.php?page=1'>".'First Page'."</a>";
for ($i=1; $i<=$pages;$i++){
echo "<a href='index1.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='index1.php?page=$pages'>Last page</a>";
That is my code. The problem is that on my second page i have result of first $new variable .......................................................................................................................................................................
... i have a problem that on my second page i have result of first $new variable
That's because you're not including $new variable in the pagination links. So every time you go to 2nd, 3rd, 4th, ... page, you'll get the same $new value as 1, and that's because of this statement,
$new=(isset($_GET['year']) && $_GET['year']>0)?$_GET['year']:1 ;
Include this variable in the pagination links so that you could get it's value in the subsequent pages. So your pagination links section would be like this:
// your code
echo "<a href='index1.php?page=1&year=".$new."'>".'First Page'."</a>";
for ($i=1; $i<=$pages;$i++){
echo "<a href='index1.php?page=".$i."&year=".$new."'>".$i."</a> ";
}
echo "<a href='index1.php?page=".$pages."&year=".$new."'>Last page</a>";
for pagination use this code in file with name: view-paginated.php
$per_page = 10;
$result = mysql_query("SELECT * FROM table");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
$start = 0;
$end = $per_page;
}
}
else
{
$start = 0;
$end = $per_page;
}
echo "<p><a href='view.php'>show all</a> | <b>page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='view-paginated.php?page=$i'>$i</a> ";
}
for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results) { break; }
echo mysql_result($result, $i, 'YOUR COLUMN') ;
}

PHP Pagination of MySQL Query not incrementing/decrementing properly

I'm having trouble with the follow PHP which paginates the results of a MySQL query.
When I go to webpagename.php with the first page of the results and click Previous, the browser changes to webpagename.php?page=-1 and shows the first page of results again. If I click Previous again, it changes to webpagename.php?page=-2 and shows Page 1 of the results again, etc.
When I go to webpagename.php with the first page of the results and click Next, the browser changes to webpagename.php?page=1 and shows the first page of results again. I then have to hit Next a second time to move to Page 2.
When I go to the last page of the results - Page 8 - and click Next, the browser changes to webpagename.php?page=9 and shows Page 1 of the results. If I click Next again, it shows webpagename.php?page=10 and shows Page 1 of the results again, etc.
Expected Results:
When on Page 1 and a user hits Previous, I would like the code to do nothing/not decrement. When on Page 8 - the last page of results, I would like the code to do nothing/not increment. Of course, I would also expect that if you hit Next from Page 1 that it doesn't display Page 1 a second time but rather goes to Page 2.
Your exact changes to this code to make it work properly are very much appreciated. Thank you for time.
<?php
mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
// number of results to show per page
$per_page = 10;
// figure out the total pages in the database
$result = mysql_query("SELECT * FROM uc_users LEFT JOIN ent_dancers ON uc_users.id = ent_dancers.id WHERE ent_dancers.DancerYesNo = '1' AND ent_dancers.DancerEnabledYesNo = '1' ORDER BY uc_users.display_name ASC");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: webpagename.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
// display data in table
echo "<div class='dancerbio'>";
echo "<div class='uts-1'>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
$rowid = mysql_result($result, $i, 'id');
echo "<div class='uts-1-1'><a class='bodytxt5' href='webpagename-details.php?userid=$rowid'>" . mysql_result($result, $i, 'display_name') . "</a></div>";
}
// close table>
echo "<div class='ugen-1'></div>";
echo "</div>";
$prev = $_GET['page'] - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a class='bodytxt5' href='webpagename.php?page=$i'>$i</a> ";
}
$next = $_GET['page'] + 1;
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
// pagination
?>
replace this:
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
by this:
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
elseif ($show_page > $total_pages)
{
$show_page=$total_pages;
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else {
$show_page=1;
$start = 0;
$end = $per_page;
}
}
else {
$show_page=1;
$start = 0;
$end = $per_page;
}
then :
$prev=$show_page-1;
$next=$show_page+1;
if($show_page>1){//this way previsous won't appear if you are at page 1 already
//show previous div
}
if($show_page<$total_pages){ //this way next won't appear unless you are not at the last page
//show next div
}
Make a variable $page set it equal to $_GET['page'].
if(isset($_GET['page'])){
$page = $_GET['page'];
}
else{
$page = 1;
}
you need to put a condition before echoing previous link to check whether $_GET['page'] is set or not and is greater than 1.
Like this:
if($page!=($start+1)){
$prev = $page - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
}
Add another condition for next
if($page!=$total_pages)
{
$next = $page+1
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
}
I hope your issue is solved.
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
$show_page=1
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
$show_page=1;
}
// display pagination
// display data in table
echo "<div class='dancerbio'>";
echo "<div class='uts-1'>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
$rowid = mysql_result($result, $i, 'id');
echo "<div class='uts-1-1'><a class='bodytxt5' href='webpagename-details.php?userid=$rowid'>" . mysql_result($result, $i, 'display_name') . "</a></div>";
}
// close table>
echo "<div class='ugen-1'></div>";
echo "</div>";
if($show_page!=($start+1)){
$prev = $page - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
}
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a class='bodytxt5' href='webpagename.php?page=$i'>$i</a> ";
}
if($show_page!=$total_pages)
{
$next = $page+1
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
}
// pagination
?>

pagination of search result is not working

Pagination problem in search result
I am trying to paginate the search results, Here is the code that i'm using to look up for the search queries from user's form. I am getting search results paginated, but when i click 2 nd page of results it shows undefined index for those item posted...While gone through tutorials i have seen to use $_GET instead of $_POST ,after doing that chane also no improvement in results
As i am new to this php code, can you guys help or guide me in the right direction?
/////////////test.php//////////////////
mysqli_report(MYSQLI_REPORT_ERROR);
// number of results to show per page
$per_page = 2;
// figure out the total pages in the database
if ($result = $mysqli->query("SELECT * FROM bridegroom where Gender like '%$Name%' and Castest like'%$caste%' and Location like '%$location%' order by AGE"))
{
if ($result->num_rows != 0){
$total_results = $result->num_rows;
// ceil() returns the next highest integer value by rounding up value if necessary
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
$start = 0;
$end = $per_page;
}
}
else
{
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p> <b>View search results:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo $i . " ";
}
else
{
echo "<a href='test.php?page=$i'>$i</a> ";
}
}
echo "</p>";
// display data in table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// find specific row
$result->data_seek($i);
$row = $result->fetch_row();
// echo out the contents of each row into a table
echo ("Name:$row[1]<br><span class=\"old\"> Age:$row[4]</span><br><span class=\"sold\">Caste:$row[5]</span><br><span class=\"old\">Location:</span><span class=\"sold\">$row[6]</span><br><br>");
}
// close table>
}
else
{
echo "No results to display!";
}
}
// error with the query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
// display pagination
echo "<p> <b>Your search results:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo $i . " ";
}
else
{
echo "<a href='test.php?page=$i'>$i</a> ";
}
}
echo "</p>";
?>

issue in selecting value in pagination

I think i am very near to my correct answer. I found one code on this forum like i wrote below.but it didn't work for me.Whats wrong with this code???
<?php
include("config.php");
$start = 0;
$per_page = 10;
$targetpage = "manual.php?id=$id"; // This is my target page
if(!isset($_GET['page'])){
$page = 1;
} else{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
........................
.............................
.................?>
The answer which i saw contains id=42 in $targetpage = "manual.php?id=$id";But i don't understand why he used 42 there???
Then linking target page in next previous code as follows
<?php
if($page > 1){
$prev = $page - 1;
$prev = " <a href=\'$targetpage&page=$prev'>prev</a> ";
} else {
$prev = "";
}
if($page < $num_pages){
$next = $page + 1;
$next = " <a href=\'$targetpage&page=$next'>next</a> ";
}
else
{
$next = "";
}
echo $prev;
echo $next;
?>
But it gives me error like \'manual.php is not found..But i have this file.
just a guess, try to use:
$targetpage = "./manual.php?id=$id";
if it doesn't work then it means you are specifying the wrong URL to your manual.php page, try to find the correct URL
and id=42 it's an application specific parameter so it's related to that app so it's okay if you don't use it in your app.

How to add Ajax on existing PHP & Mysql based pagination

Hi i am a new programmer,
i have a PHP & Mysql based pagination, i want to add Ajax functionality to it.
i have gone through many tutorials but i was not able to find any which tells about adding ajax onto existing pagination they all tell about making Ajax based pagination.
i want user be able to paginate even if javascript is turned off. so i want to add some Ajax to my code so that i can be able to paginate with Ajax and PHP.
i can use jquery .load() method to paginate.
please look at my code and suggest me how i can fetch page url for ajax to paginate
i guess something like this has to work. i cant figure out how, please help.
or tell me some tutorial i can learn from.
Jquery Code
$(document).ready(function(){
$('#pagination').click(function(){
$('pageurl').load('is-test2.php #PaginationDiv');});
});
PHP & MySQL Based Pagination
<?php
require_once('_ls-global/php/connection.php');
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sql1 = "SELECT COUNT(*) FROM $table";
$result1 = mysql_query($sql1, $connection) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_row($result1);
$numrows = $row[0];
$rowsperpage = 2;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$currentpage = (int) $_GET['page'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql2 = "SELECT * FROM internet_security ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result2);
$startrow = ($currentpage-1) * $rowsperpage;
Code in html
h3>Results <?php echo ($startrow+1) ?> - <?php echo min($startrow + $rowsperpage, $row) ?> of <?php echo ($totalpages *$rowsperpage) ?></h3>
<ul><?php
if ($currentpage!=$totalpages) {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$totalpages'>$totalpages</a></li> ";
$nextpage = $currentpage + 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next»»</a></li> ";
}?></ul>
<ul><?php
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li>";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li> ";
}}}
}
}
?> </ul>
<ul><?php
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>««Prev</a></li> ";
echo "<li><a href='{$_SERVER['PHP_SELF']}?page=1'>1</a></li> ";
}?></ul>
since you are getting your page variable into the PHP script with GET method, you can pass the variable like :
$(document).ready(function(){
$('#pagination ul li a').click(function(){
e.preventDefault();
$('#divtoreplace').load($(this).attr("href"));
});
});

Categories