How to pass $_GET in pagination in php - php

This code works perfectly if i am just running the pagination, but when there is a $_GET variable passed it shows the first page properly and when clicked on next it loads all the data in the db and runs from beginning.
I would like to know how can i pass a $_GET variable passed.
<?php
if ($currentpageRec > 1) {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpageRec=1'>First Page</a> ";
prevpageRec = $currentpageRec - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpageRec=$prevpageRec'>Previous Page</a> ";
}
for ($x = ($currentpageRec - $rangeRec); $x < (($currentpageRec + $rangeRec) + 1); $x++) {
if (($x > 0) && ($x <= $totalpagesRec)) {
if ($x == $currentpageRec) {
echo " [<b>$x</b>] ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpageRec=$x'>$x</a> ";
}
}
}
if ($currentpageRec != $totalpagesRec) {
$nextpageRec = $currentpageRec + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpageRec=$nextpageRec'>Next Page</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpageRec=$totalpagesRec'>Last Page</a> ";
}
?>

Based on your code, I am wondering whether you should register global variables
http://php.net/manual/en/security.globals.php
Your code could use the variables as globals, whereas in fact it should refer to the GET variables, e.g.
<?php
if ($_GET['currentpageRec'] > 1) {
//[etc]
?>
Further to the comment below: I tried to say: user $_GET (etcetera), as the code appears to be based on the (depreciated) global variabels.
--- edit ---
Further to the updated shared code: you have to pass each variable again when using the pagination, e.g. for your last line
<?
// code to define $totalpagesRec
// code to define $KeyWord
// personal preference to split the echo between static texts and variables
echo " <a href='".$_SERVER['PHP_SELF']."?currentpageRec=".$totalpagesRec."&keyword=".$KeyWord."'>Last Page</a> ";
?>
Hope this helps

Related

How to sub a html and php code with php open and close tag into a php echo function

I want to echo out this code but there's already open and close php tag , so how do I sub the html and php in the echo and make the php function works.
This is the code that I want to echo it out.
<div class="tutor-zoom-join-button-wrap">
<?php echo $browser_text; ?>
<?php _e('Join in Zoom App', 'tutor-pro'); ?>
</div>
This is the function that I made(replace with code above with 123)
<?php
$var1 = 1;
if ($var1 = 1) {
echo "123 ";
} else {
echo "The course ID, password and join button will only be shown before 30min of course start";
}
?>
This is what I try but not working
<?php
$var1 = 1;
if ($var1 = 1) {
echo
"<div class="tutor-zoom-join-button-wrap">"
"",.$browser_text."",
""._e('Join in Zoom App', 'tutor-pro')."",
"</div>",
"</div>" ;
} else {
echo "The course ID, password and join button will only be shown before 30min of course start";
}
?>
I have modified your code. Please check and you also refer to some PHP tutorials to learn php.
<?php
$var1 = 1;
if ($var1 == 1) {
echo
"<div class='tutor-zoom-join-button-wrap'>
<a href=" .$browser_url. " target='_blank' class='tutor-btn tutor-button-block'>".$browser_text."</a>,
<a href=".$meeting_data['join_url']."target='_blank' class='tutor-btn bordered-btn tutor-button-block'>"._e('Join in Zoom App', 'tutor-pro')."</a>",
"</div>",
"</div>" ;
}
else {
echo "The course ID, password and join button will only be shown before 30min of course start";
}
?>
The problem is in the long string you want to echo. The double quote " starts and ends the string. You have started and ended the string several times when what you want is one long string. If you want to use a double quotes inside the string you have to precede it with a slash \". To concatenate strings you need to use a dot .. Double quotes also allow you to place variables inside the string that will be replaced. So your echo statement could be done like this:
echo
"<div class=\"tutor-zoom-join-button-wrap\">" .
"$browser_text" .
"" . _e('Join in Zoom App', 'tutor-pro') . "" .
"</div>" .
"</div>" ; // Note that this tag does not appear to be necessary based on the code you have shown
You can write it like this:
<?php
$var1 = 1;
if ($var1 = 1) {
echo `<div class="tutor-zoom-join-button-wrap">
`.$browser_text.`
`._e('Join in Zoom App', 'tutor-pro').`
</div>
</div>` ;
} else {
echo "The course ID, password and join button will only be shown before 30min of course start";
};
?>
Hope It will work for you!

Paginating mysql data using PHP

I borrow this code from this video: https://www.youtube.com/watch?v=takddjxhWT0
But when I run this code manually, the data that I get into the database doesn't change when I click the other page number. What do you think is the best code in order to run code this manually?
Any comment will help, thanks.
<html>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("steer");
$page=(isset($_GET["page"]));
if($page=="" || $page=="1")
{
$page1=0;
}
else
{
$page1=($page*12)-5;
}
$res=mysql_query("select * from studtut LIMIT $page1,5");
while($row=mysql_fetch_array($res))
{
echo $row["id"]." ".$row["name"];
echo "<br>";
}
$res1=mysql_query("select * from studtut");
$cou=mysql_num_rows($res1);
$a=$cou/5;
$a=ceil($a);
echo "<br>";
echo "<br>";
for($b=1;$b<=$a;$b++){
?><?php echo $b; " " ?> <?php
}
?>
</body>
</html>
You need to set the page variable. This:
<a href="paging.php?=<?php echo $b; ?>"
Must be changed to this:
<a href="paging.php?page=<?php echo $b; ?>"
Also when you get the variable, you must assign it's value. And not the value, returned by isset function:
$page= isset($_GET["page"]) ? $_GET["page"] : 1;
Use <a href="paging.php?page=<?php echo $b; ?>" .Why ?
Because you access $_GET["page"] as Query string in PHP file . So it should be set in Link so that it doesnt become Blank in PHP file.
In your case $page is always Blank as its not defined . Turn error_reporting(E_ALL) always in Development mode to see errors,warnings,notices.

How to make current page with different style (PHP ?page href)?

My piece of code.
And problem now are about styling, i just want style page number, wich is open, like a active id, if page open, then that number is in different color or smth else.
I need make a new variable? Or what, i just try to add but its wont work, it only works if at the end i write echo $page; , then it show style on this, but i need on links, numbers.
<?php
if($total_pages > 1){
if($page != 1){
echo ' < ';
}
for($number=1;$number<=$total_pages;$number++)
{
echo ''.$number.'';
}
if($page != $total_pages){
echo ' > ';
}
}
?>
You can do something like this.
$currentPageNumber = $_GET['page'];
for($number=1;$number<=$total_pages;$number++){
$currentPageStyle = '';
if($number == $currentPageNumber){
$currentPageStyle = 'style="color:red"';
}
echo '<a href="?page='.$number.'" '.$currentPageStyle.'>'.$number.'</a>';
}

Can see the value of the variable but can't use it to validate PHP

<?php
function number_of_fb_comments()
{
return $number = "<fb:comments-count href=" . get_permalink($post->ID) . " ></fb:comments-count>";
}
?>
<?php
function get_fb_comments()
{
$number = number_of_fb_comments();
if ($number == 1) {
echo "<a href =$url#facebook_comments > $number Comment</a></span>";
} else if ($number > 1) {
echo "<a href =$url#facebook_comments > $number Comments</a></span>";
} else {
echo "<a href=$url#facebook_comments>Leave a comment</a> </span>";
}
} ?>
I'm trying to make a validation of my facebook comments, but I can't seem to use the '$number' to validate it in the if statement, but I can echo the value and see it.
So it will only goes to the 'else' part....
I have made $number as a global variable and after using var_dumb($number)
I'm seeing series of out put on each of the post
string(104) "0"
string(118) "1"
string(111) "0"
The $number you're calling returns a string (return $number = "<fb:comments-count href="), which is not 1 or >1 - it appears you'll want to return some different data from your number_of_fb_comments() function.

php and mysql not showing data, not entering foreach loop

I am having trouble with modifying a php application to have pagination. My error seems to be with my logic, and I am not clear exactly what I am doing incorrectly. I have had before, but am not currently getting errors that mysql_num_rows() not valid result resource
and that invalid arguments were supplied to foreach. I think there is a problem in my logic which is stopping the results from mysql from being returned.
All my "test" echos are output except testing while loop. A page is generated with the name of the query and the word auctions, and first and previous links, but not the next and last links. I would be grateful if a more efficient way of generating links for the rows in my table could be pointed out, instead of making a link per cell. Is it possible to have a continuous link for several items?
<?php
if (isset($_GET["cmd"]))
$cmd = $_GET["cmd"]; else
die("You should have a 'cmd' parameter in your URL");
$query ='';
if (isset($_GET["query"])) {
$query = $_GET["query"];
}
if (isset($_GET["pg"]))
{
$pg = $_GET["pg"];
}
else $pg = 1;
$con = mysql_connect("localhost","user","password");
echo "test connection<p>";
if(!$con) {
die('Connection failed because of' .mysql_error());
}
mysql_query('SET NAMES utf8');
mysql_select_db("database",$con);
if($cmd=="GetRecordSet"){
echo "test in loop<p>";
$table = 'SaleS';
$page_rows = 10;
$max = 'limit ' .($pg - 1) * $page_rows .',' .$page_rows;
$rows = getRowsByProductSearch($query, $table, $max);
echo "test after query<p>";
$numRows = mysql_num_rows($rows);
$last = ceil($rows/$page_rows);
if ($pg < 1) {
$pg = 1;
} elseif ($pg > $last) {
$pg = $last;
}
echo 'html stuff <p>';
foreach ($rows as $row) {
echo "test foreach <p>";
$pk = $row['Product_NO'];
echo '<tr>' . "\n";
echo '<td>'.$row['USERNAME'].'</td>' . "\n";
echo '<td>'.$row['shortDate'].'</td>' . "\n";
echo '<td>'.$row['Product_NAME'].'</td>' . "\n";
echo '</tr>' . "\n";
}
if ($pg == 1) {
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pg=1'> <<-First</a> ";
echo " ";
$previous = $pg-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pg=$previous'> <-Previous</a> ";
}
echo "---------------------------";
if ($pg == $last) {
} else {
$next = $pg+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pg=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pg=$last'>Last ->></a> ";
}
echo "</table>\n";
}
echo "</div>";
function getRowsByProductSearch($searchString, $table, $max) {
$searchString = mysql_real_escape_string($searchString);
$result = mysql_query("SELECT Product_NO, USERNAME, ACCESSSTARTS, Product_NAME, date_format(mycolumn, '%d %m %Y') as shortDate FROM {$table} WHERE upper(Product_NAME) LIKE '%" . $searchString . "%'" . $max);
if($result === false) {
echo mysql_error();
}
$rows = array();
while($row = mysql_fetch_assoc($result)) {
echo "test while <p>";
$rows[] = $row;
}
return $rows;
mysql_free_result($result);
}
edit: I have printed out the mysql error of which there was none. However 8 "test whiles" are printed out, from a database with over 100 records. The foreach loop is never entereded, and I am unsure why.
The problem (or at least one of them) is in the code that reads:
$rows = getRowsByProductSearch($query, $table, $max);
$numRows = mysql_num_rows($rows);
The $numRows variable is not a MySQL resultset, it is just a normal array returned by getRowsByProductSearch.
Change the code to read:
$rows = getRowsByProductSearch($query, $table, $max);
$numRows = count($rows);
Then it should at least find some results for you.
Good luck, James
Hi there,
The next problem is with the line that reads:
$last = ceil($rows/$page_rows);
It should be changed to read:
$last = ceil($numRows / $page_rows);
Would recommend adding the following lines to the start of you script at least while debugging:
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'On');
As that would have thrown up a fatal error and saved you a whole lot of time.
if (!(isset($pg))) {
$pg = 1;
}
How is $pg going to get set? You don't appear to be reading it from $_GET. If you're relying on register_globals: don't do that! Try to read it from $_GET and parse it to a positive integer, falling back to '1' if that fails.
<a href='{$_SERVER['PHP_SELF']}?pg=$next'>Next -></a>
You appear to be losing the other parameters your page needs, 'query' and 'cmd'.
In general I'm finding it very difficult to read your code, especially the indentation-free use of echo(). Also you have untold HTML/script-injection vulnerabilities every time you "...$template..." or .concatenate a string into HTML without htmlspecialchars()ing it.
PHP is a templating language: use it, don't fight it! For example:
<?php
// Define this to allow us to output HTML-escaped strings painlessly
//
function h($s) {
echo(htmlspecialchars($s), ENT_QUOTES);
}
// Get path to self with parameters other than page number
//
$myurl= $_SERVER['PHP_SELF'].'?cmd='.urlencode($cmd).'&query='.urlencode($query);
?>
<div id="tableheader" class="tableheader">
<h1><?php h($query) ?> Sales</h1>
</div>
<div id="tablecontent" class="tablecontent">
<table border="0" width="100%"> <!-- width, border, cell width maybe better done in CSS -->
<tr>
<td width="15%">Seller ID</td>
<td width="10%">Start Date</td>
<td width="75%">Description</td>
</tr>
<?php foreach ($rows as $row) { ?>
<tr id="row-<?php h($row['Product_NO']) ?>" onclick="updateByPk('Layer2', this.id.split('-')[1]);">
<td><?php h($row['USERNAME']); ?></td>
<td><?php h($row['shortDate']); ?></td>
<td><?php h($row['Product_NAME']); ?></td>
</tr>
<?php } ?>
</table>
</div>
<div class="pagercontrols">
<?php if ($pg>1) ?>
<<- First
<?php } ?>
<?php if ($pg>2) ?>
<-- Previous
<?php } ?>
<?php if ($pg<$last-1) ?>
Next -->
<?php } ?>
<?php if ($pg<$last) ?>
Last ->>
<?php } ?>
</div>
Is it possible to have a continuous link for several items?
Across cells, no. But you're not really using a link anyway - those '#' anchors don't go anywhere. The example above puts the onclick on the table row instead. What exactly is more appropriate for accessibility depends on what exactly your application is trying to do.
(Above also assumes that the PK is actually numeric, as other characters may not be valid to put in an 'id'. You might also want to consider remove the inline "onclick" and moving the code to a script below - see "unobtrusive scripting".)
This is wrong:
if($cmd=="GetRecordSet")
echo "test in loop\n"; {
It should be:
if($cmd=="GetRecordSet") {
echo "test in loop\n";
In your getRowsByProductSearch function, you return the result of mysql_error if it occurs. In order to debug the code, maybe you can print it instead, so you can easily see what the problem is.

Categories