0

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I need a really help since I could not find the problem in my coding. I have an included pagination.php file in cutomer.php file. The follwoing "Notice: Undefined variable: page in F:\wamp\www..." notice is continuously popping up in every single page where I have included. Please help me. Thank you!

<?php

$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

$stages = 3;

if(isset($_GET['page']))

$page = mysql_real_escape_string($_GET['page']);


if($page){                                 **//line 57**
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
    }   

// Get page data
$query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}                **//line 68**
$prev = $page - 1;  
$next = $page + 1;  

Notice: Undefined variable: page in F:\wamp\www\homepage\includes\pagination.php on line 57 Notice: Undefined variable: page in F:\wamp\www\homepage\includes\pagination.php on line 68

Community
  • 1
  • 1
xnote
  • 89
  • 2
  • 9
  • It might be duplicate however that post did not give me a solution. Because I needed more specific and simple solution to my problem – xnote Jan 20 '13 at 19:45
  • Read more carefully. That's also mentioned over there. (And in many of the hundreds other duplicates.) – mario Jan 20 '13 at 19:55

2 Answers2

5

When your variable $_GET['page'] is empty, $page won't be defined.

To avoid this, you might change

if($page) { 

to

if (isset($page)) {

...or something with a similar effect.

Better would be to initialize $page before using it, but that would perhaps require to change some of the logic afterwards.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Bjoern
  • 15,934
  • 4
  • 43
  • 48
0

Just initialize $page before the if isset statement and the error should go away

It is because if the if statement returns false, $page would be an undefined variable. The notice means that that the variable should be defined no matter what path the code takes.

Ahmed Aeon Axan
  • 2,139
  • 1
  • 17
  • 30
  • Why was this downvoted? It is the correct answer. – Lightness Races in Orbit Jan 20 '13 at 19:23
  • Do I have to change everywhere where $page is mentioned? Because if I correct one the other is popping up. Is there any way that I can do in a simple way? – xnote Jan 20 '13 at 19:41
  • you could initialize `$page = NULL` or something to that effect before the line `if(isset($_GET['page']))` and all notice's should go away. – Ahmed Aeon Axan Jan 20 '13 at 19:45
  • That's the technique that I wanted to get :). Ahmed, is it safe way to do that. It seems it really solved my problem. – xnote Jan 20 '13 at 19:49
  • Yes It is safe and actually good practice to initialize variables to NULL or something of that sort before in cases where variables are assigned values in an if condition with no else. – Ahmed Aeon Axan Jan 20 '13 at 19:51
  • Thank you very much. Simple and good technique! – xnote Jan 20 '13 at 19:53