var undefined getting value $ _GET ['page'] - php

I want paging is a content of a news blog. Everything works correctly, the page content is successful. But I get an error screen displays PHP:
Notice: Undefined index: page in C:\wamp\www\index.php on line 146
code with the line that gives the error:
$maxreg = 1;
$pag = $_GET['page'];
if (!isset($pag) || empty($pag)){
$min = 0;
$pag = 1;
}else{
if($pag == 1){
$min = 0;
}else{
$min = $maxreg * $pag;
$min = $min - $maxreg;
}
}
include("js/class.AutoPagination.php");
$obj = new AutoPagination(contar_contenido(), $pag);
mostrar_contenido($min,$maxreg);
echo $obj->_paginateDetails();
The line gives the error is this:
$ page = $ _GET ['page'];
The first page by default index.php and contains no var in the url.
I do not understand why if fails below by a conditional var I determine if that has content or not.
Should not show any error php, could someone give me a solution? Thanks

You don't need to do
$pag = $_GET['page'];
Check if $_GET['page'] is set first:
if(!isset($_GET['page']) || empty($_GET['page'])) {
$min = 0;
$pag = 1;
}else{
if($pag == 1){
$min = 0;
}else{
$min = $maxreg * $pag;
$min = $min - $maxreg;
}
}

I quote;
Declare your variables. Or use isset() to check if they are declared before referencing them;
PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

I found the solution, I've built so, assigning a value to the variable empty get in if it is indefinite.
Maybe it's a very elegant solution, but it works perfect paging.
if(!isset($_GET['page']) || empty($_GET['page'])) {
$_GET['page']="";
}
$maxreg = 2;
$min = 0;
$pag = $_GET['page'];
I think the second conditional left over.
I leave it here in case anyone has the same problem. This way you can fix it.

Related

How to call incremented variable defined in one page to another in php

Ok. This might seem duplicate to few people, but its not :)
Here's the question
page-1.php
$link = 'page-2.php'
$i = 0;
foreach( $data as $row ){
$i++;
$id = $i;
echo "
<tr>
<td><a href='".$link."'>{$row['name']}</a></td>
</tr>";
}
page-2.php
if (isset($_GET[$id])) {
if ($id == '0')
$offset = '0';
else if ($id == '1')
$offset = '1';
else if ($id == '2')
$offset = '2';
else if ($id == '3')
$offset = '3';
}
$name = $global[$offset]['name'];
I keep getting these errors
Notice: Undefined variable: id
Notice: Undefined variable: offset
How to solve this, so that $offset value is aligned to the $id value in way that when $id = '0', $offset = '0' and so on.
Rgds..
You can add the variable in the link to the next page:
<td>Page2</td>
This will create a GET variable.
And then on page two:
//Using GET
$var_value = $_GET['varname'];
You should not send data like that.. instead Use session to send data from one page to another.
You can set session variable by starting the session as session_start();
Then you can set session variable as:
$_SESSION['id'] = $id;
Hope it helps!

undefined offset PHP, not sure what it does [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 months ago.
I have the following error in PHP error.log
[Tue Dec 19 12:08:22.887574 2017] [:error] [pid 32196] [client xx.xx.xx.x:20560] PHP Notice: Undefined offset: 8 in /var/www/html/page.php on line 55, referer: view.php?1x=8
and the php code that i think causes this is:
$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;
// echo $_SESSION['websites'][$i];
$website = explode(";", $_SESSION['websites'][$i]);
// echo $website[0];
$i++;
$_SESSION['i'] = $i;
I dont really know what $i = isset($_SESSION['i']) ? $_SESSION['i'] : 0; does
thank you
session_start();
$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;
if ($_SESSION['i'] < $sesioni1x) {
// echo $_SESSION['websites'][$i];
$website = explode(";", $_SESSION['websites'][$i]);
// echo $website[0];
$i++;
$_SESSION['i'] = $i;
header("Location: $website[0]"); //redirect
die();
// echo $website[0];
// echo $sesioni1x;
// echo $website[0]." Frame-URL<br>";
// $_SESSION['actual_website'] = $website[0];
}
if ($_SESSION['i'] == $sesioni1x) {
$handle = fopen($list1x, "a"); //open file to append content to csv file
fputcsv($handle, $_SESSION['addwebsite'], ";"); //insert line in opened file
fclose($handle); //close file
header("Location: index.php"); //redirect
die();
// echo "session = var";
}
this is the full code where i get this warning, i must say that script is doing his job, but i want to get rid of the error
the $_SESSION['i'] in the source is 0
this is ternary called ternary operator. Basically it is a simple if/else.
$i = (isset($_SESSION['i']) ? $_SESSION['i'] : 0);
To help you understand it's this code in 1 line
if(isset($_SESSION['i'])){
$i=$_SESSION['i'];
}
else{
$i=0;
}
$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;
The above code is just an if/else nothing else
this will check for the $_SESSION['i'] is it is set then assign it's value to $i otherwise it assigns 0 to $i variable.
Now looking to your problem, it looks like your $_SESSION['i'] is already set and its value is 8 so $i = 8,
Now, $website = explode(";", $_SESSION['websites'][$i]); this like is checking for your $_SESSION['website'] array and trying to find 8th element from the array which has not been set so it gives an error its undefined.
(e) ? r1 : r2 is a conditional statement.
If the expression e is true the value is r1, if e is false the value is r2. So
$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;
means "i is $_SESSION['i'], if $_SESSION['i'] is set, otherwise it is 0".

getting an "undefined variable" on isset [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
So i've got a rather complex bit of code that i'm trying to make work (actually; it works fine on my test server but not so much my live server. so i'm trying to make it rework) -- it keeps telling me that a variable i'm asking it to check with isset is not a defined variable. I'm probably over thinking this whole method and there's probably a simplier way. but i need you guys to bash me over the head with it apparently
include('bootstra.php');
include('includes/parts/head.php');
if(!isset($_SESSION['user']['uid']))
{
echo $html;
include('includes/parts/login.php');
if(isset($_GET['mode']))
{
$file = $_GET['mode'];
$path = "includes/parts/{$file}.php";
if (file_exists($path)) {
require_once($path);
} else {
die("The Page REquested could not be found!");
}
}
else
{
include('includes/parts/frontpage.php');
}
}
else
{
if(!isset($_SESSION['x']) || !isset($_SESSION['y']))
{
if(isset($_GET['x']) && isset($_GET['y']))
{
$_SESSION['x'] = $_GET['x'];
$_SESSION['y'] = $_GET['y'];
}
else
{
$_SESSION['x'] = 0;
$_SESSION['y'] = 0;
}
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
if($_SESSION['x'] != $_GET['x'] || $_SESSION['y'] != $_GET['y'] )
{
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
echo $html;
echo $base->displayMap($_GET['x'], $_GET['y']);
include('includes/context_menu.php');
}
}
}
Here is the exact error:
Notice: Undefined index: x in /home/****/public_html/reddactgame/index.php on line 27
Change it
<pre>
if( !isset($_SESSION['x']) || !isset($_SESSION['y']) )
</pre>
to
<pre>
if( !( isset($_SESSION['x']) && isset($_SESSION['y']) ) )
</pre>
There is no error, that is a Warning...
It is saying that $_SESSION['x'] was never setted, so, when you do that isset it tells you that it was never declared.
Example:
This gives you the same warning
empty($x);
This does not give you warning
$x = 0;
empty($x);
EDIT
I see this is a common question, so here is a better explanation.

PHP for loop variable [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have made this for loop to get a variable amount of columns selected from the database. As I'm running this, I get an error saying :
Notice: Undefined variable: kolom_1
Notice: Undefined variable: kolom_2
Notice: Undefined variable: kolom_3
Notice: Undefined variable: kolom_4
Notice: Undefined variable: kolom_5
Notice: Undefined variable: kolom_6
But I have it all placed in a for loop, why does he not recognize them? I am not getting what I'm doing wrong.
function lijst_ophalen($data, $from){
$totaal = count($data);
for ($i=1; $i<$totaal; $i++){
$kolom_[$i] = $this->mysqli->real_escape_string($data['kolom_' . $i . '']);
if($kolom_[$i]!="") $kolom_[$i] = "{$kolom_[$i]},"; else $kolom_[$i]="";
if($kolom_[$i]==$totaal) $kolom_[$i] = "{$kolom_[$i]}";
}
$from_table = "";
$categorie = "";
if($from == "bv"){
$from_table = "klanten_algemene_gegevens_bv";
$categorie = "";
}
if(($from == "1manszaak") || ($from == "vof")){
$from_table = "klanten_algemene_gegevens_vof_1manszaak";
if($from == "1manszaak"){
$categorie = "1manszaak";
}
if($from == "vof"){
$categorie = "vof";
}
$categorie = "WHERE soort_onderneming = '{$categorie}'";
}
if($from == "ib"){
$from_table = "klanten_ib";
$categorie = "";
}
$result = $this->mysqli->query(
<<<EOT
SELECT
{$kolom_1}
{$kolom_2}
{$kolom_3}
{$kolom_4}
{$kolom_5}
{$kolom_6}
FROM {$from_table}
{$categorie}
EOT
);
if($result){
$waardes = array();
while ($row = $result->fetch_assoc()) {
$waardes[]=$row;
}
return $waardes;
}
}
When you are initializing / filling data into the $kolom_ you are creating an' array instead of your (properly) intended series of different variables.
$kolom_[$i] <--- ARRAY
And lower down you are calling a series of variables $kolom_1, which doesn't exist because you created an' array and not a series of variables.
To avoid the error you can simply change your
$kolom_1
$kolom_2 (and so on)
calls into
$kolom_[1]
$kolom_[2] (and so on)
and you should be set to go.

Undefined variable error when using defined variable in PHP?

I'm making a website where i login, press a button and then some information is loaded from a database. The code works fine expect for one thing which is: I get this error:
<b>Notice</b>: Undefined variable: error in <b>/character-load.php</b> on line <b>42</b><br />
But the variable is defined here:
if ($user["banned"] == 1){
$error = 1;
}
if (mysql_num_rows($character) == 0){
$error = 1;
}
if (mysql_num_rows($username) == 0){
$error = 1;
}
if (empty($charid)){
$error = 1;
}
Is there anything wrong with this? I dont understand why it says that the variable error is undefined? I can add more information on request as i don't really know what more to add.
try this -
$error = 0;
if ($user["banned"] == 1){
$error = 1;
}
if (mysql_num_rows($character) == 0){
$error = 1;
}
if (mysql_num_rows($username) == 0){
$error = 1;
}
if (empty($charid)){
$error = 1;
}
and add check at the time of use.
The variable is only defined if one of these 4 situations occurs. In a situation where none of this occurs, it won't exist.
Easiest fix is to define it as 0 at the top of the block.
Remember; in PHP a variable is only defined if one of the assignment lines is actually executed. Simply being inside the file but being skipped over doesn't count.

Categories