Pagination Problems(PHP superglobal variable issue, undefined index) - php

i am trying to implement pagination somewhere and i have this issue:
I have this part to change links:
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
which gives this error for this part:
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
It says undefined index..
Why do I get this Error?
Thanks

You should quote the array index. also use html entities.
Like this
echo " <a href='{$_SERVER['SCRIPT_NAME']}?Page=$Prev_Page'><< Back</a> ";
And its safe to check if $_GET["Page"] exists.
$Page = isset($_GET["Page"]) ? $_GET["Page"]: false;

This happens because you are missing an index in the array. $_GET is just an array, so you should check if the key exists first.
$Page = (array_key_exists('page', $_GET)) ? $_GET["page"] : false;
if($Page===false)
{
//no page
return;
}

// empty() works even if the variable doesn't exist, kind of like isset()
if(!empty($_GET['Page']) !== false) {
// Do stuff
$page = $_GET['Page'];
}

Related

How I can create multiple request get/post page url

How I can create multiple get/post request in page URL?
Like this:
http://www.example.com/?goto=profile&id=11326
I already tried with this:
$doVariableprofile = isset($_GET['goto']) ? $_GET['goto'] : null;
if($doVariableprofile == 'profile') {
if(empty($_SESSION['logged'])) require_once('register.php');
}
how i can add more request?
now i have http://www.example.com/?goto=profile
i trying do this http://www.example.com/?goto=profile&id=1
$testt1 = isset($_GET['goto']) ? $_GET['goto']:null;
if($_GET['goto'] == 'profile?id=".$_GET['id']"'){
require_once('profile.php');
}
Doesn't work page when I add to profile?id="$_GET['id']"')
$goto = isset($_GET['goto']) ? $_GET['goto']:null;
$id = isset($_GET['id']) ? $_GET['id']:0;
if($goto == 'profile' && $id != 0){
require_once('profile.php');
}
you need to assign these values to variables, if you directly write $_GET['id'] in
'if condition' and those values are not available then you may get
"Notice: Undefined index: " error.
I believe this could be done like this
$url = "profile?id=".$_GET['id'];
if($_GET['goto'] == $url){
require_once('profile.php');
}
another thing I understood this could be done like this
if(isset($_GET['goto']) && $_GET['goto']=="profile"){
if(isset($_GET['id'] || $_GET['id']==''){
header("Location: profile.php");
}
}

PHP get parameter pulls in wrong values

I'm trying to pull in the parameter of a URL and use that to determine what information to display on page, but for some reason the information is being read wrong. The first thing I do is check for the parameter below and assign it to $page
<?php
if(isset($_GET["page"])) {
$page=$_GET["page"];
}
?>
I then check if the $page is equal to 2 or 3. For some reason, if I echo out $page, I get the proper value of the parameter but it displays incorrect info.
<?php
if(isset($page) == '2') { ?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) == '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
For some reason, even though $page returns 3, I receive INFO A that's supposed to be displayed on page 2. Am I pulling the parameter wrong? The URL Looks like this:
feed.php?page=3
php isset function return Boolean.
You should change code to:
<?php
if(isset($page) && $page== '2') {
?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) && $page== '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
This is wrong:
if(isset($page) == '2') {
It should be
if( $page == '2') {
This will only seem to work on page 1, because isset($page) returns true, which truthy gets converted to 1. The isset() function is only to check if the variable has been set or not
if($page == '3')
Why the isset()? You already do that when you assign it. Maybe this as well:
if(isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = '0'; // or something
}
isset() return a TRUE/FALSE, yet you're comparing it against normal integers. Boolean TRUE in mysql is equivalent to integer 1, but will fail the rest of your tests. You need to have:
if (isset($_GET['page'])) {
if ($_GET['page'] == 1) { ... 1 stuff }
else if ($_GET['page'] == 2) { ... 2 stuff }
}

how to fix undefined index on $_GET?

I am unable to understand how to fix this notice. It gives a undefined index on a $_GET[' '] and I cannot set a isset() as it'll brake the query and code.
class.php
private $perPage = 8;
private $startPage = 0;
public function latestArticles()
{
if($_GET['page'] <= 1) NOTICE ERROR <-- is here on the $_GET['page'];
$this->startPage = 0;
else
$this->startPage = $_GET['page'] * $this->perPage - $this->perPage;
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$sth = $this->db->prepare("SELECT * FROM articles ORDER BY id DESC LIMIT ?, ?");
$sth->execute(array($this->startPage, $this->perPage));
$data = $sth->fetchAll();
return $data;
}
And this code which sets the header if the button is clicked(next or previous button). If you first load the page, there is no header it loads like ex: www.site.com/ so it gives the error as there are no $_GET['page'] being set, but I cannot get around this issue.. this here is how I am printing the data being retrieved by the function.
index.php
<?php
if(!isset($_GET['page']))
$page = 1;
else
$page = $_GET['page'];
foreach($latestArticles as $article)
{
$title = $article['title'];
echo ''.$title.'';
}
$prev = $page-1;
$next = $page+1;
echo "
<a class='nxt_prevButton' href='?page=$prev'>previous page</a>
<a class='nxt_prevButton' href='?page=$next'>next page</a>
";
?>
failed attempts
I have set the isset() and empty() but it doesn't allow the code to work which is a next and previous button for more results, 8 results per page. (notice goes away but code (query) stops working).
What is a way to get around this, by fixing the code? WITHOUT using error_reporting(0); ?
You definitely need to use isset, it's just a matter of working out the logic.
I think, if I understand this right, that you could do this:
if(!isset($_GET['page']) || $_GET['page'] <= 1)
$this->startPage = 0;
else
$this->startPage = $_GET['page'] * $this->perPage - $this->perPage;
So if $_GET['page'] isn't set, startPage will be 0.

Keeping array from POST when using pagination link

I am having a litte issue with my searchengine.
It outputs the searchresult just fine, but when I am clicking a pagination link it fails, of course because it no longer have the $_POST array. I am using this piece of code:
if(empty($_POST) == false) {
$res = $search->search_ads($_POST);
if($res == false) {
$view->setData('numres', 0);
} else {
$view->setData('numres',$res[2]);
$view->setData('adverts', $res[0]);
}
$app->view()->setData('paginate', $res[1]);
$app->render('search.tpl');
} else {
$app->render('404.tpl');
}
When I click on f.x. "Page 2" it will render the 404 template.
Is there a way I can keep the $_POST array and reuse it in the search_ads function?
"paginate" contains the HTML for the pagination
<li><a class=\"paginate\" href=\"$target?page=$next_page&ipp=$this->items_per_page\">«</a></li>":"<li><span class=\"inactive\" href=\"#\">«</span></li>
use sessions.
session_start(); //on the top of your php file.
...
if(!empty($_POST))
$_SESSION['post_data']= $_POST;
...
if(empty($_SESSION['post_data']) == false) {
$res = $search->search_ads($_SESSION['post_data']);
...
}
Store the post array in a variable and use that elsewhere?
$post_array = $_POST;
$res = $search->search_ads($post_array);
You want to check for $_GET rather than $_POST on subsequent pages, as that's the method your using to pass the data:
href=\"$target?page=$next_page&ipp=$this->items_per_page\"
Will provide you with a $_GET array containing key value pairs:
$_GET['page'] = value of $next_page
$_GET['ipp'] = value of $this->items_per_page

What does this syntax ( page = $page ? $page : 'default' ) in PHP mean?

I'm new to PHP. I came across this syntax in WordPress. What does the last line of that code do?
$page = $_SERVER['REQUEST_URI'];
$page = str_replace("/","",$page);
$page = str_replace(".php","",$page);
$page = $page ? $page : 'default'
That's the ternary operator:
That line translates to
if ($page)
$page = $page;
else
$page = 'default';
It's an example of the conditional operator in PHP.
It's the shorthand version of:
if (something is true ) {
Do this
}
else {
Do that
}
See Using If/Else Ternary Operators
http://php.net/manual/en/language.operators.comparison.php.
It's a ternary operation which is not PHP or WordPress specific, it exists in most langauges.
(condition) ? true_case : false_case
So in this case the value of $page will be "default", when $page is something similar to false — otherwise it will remain unchanged.
It means that if $page does not have a value (or it is zero), set it to 'default'.
It means if the $page variable is not empty then assign the $page variable on the last line that variable or set it to 'default' page name.
It is called conditional operator
More verbose syntax of the last line is:
if ($page)
{
$page = $page;
}
else
{
$page = 'default';
}
That's the so-called conditional operator. It functions like an if-else statement,
so
$page = $page ? $page : 'default';
does the same as
if($page)
{
$page = $page;
}
else
{
$page = 'default';
}

Categories