Pagination error

User avatar
viddz
Sergeant Major
Sergeant Major
Posts: 45
Joined: Fri Aug 26, 2011 6:06 am
Location: Colombo

Pagination error

Post by viddz » Thu Jun 21, 2012 8:58 pm

I am having problem with pagination in php. I have already spent 3 days on this but i couldn't find the error. So finally decided to ask help from you guys.This pagination script simple queries like Select * from employee. But it gives errors when i use join in mysql query(no errors in 1st page. but when i click paginated pages there is no results and error are there).
errors
1.Notice: Undefined variable: lab_id in F:\xampplite\htdocs\chem\view_all_chemicallab.php on line 71
2.Notice: Undefined variable: lab_id in F:\xampplite\htdocs\chem\view_all_chemicallab.php on line 86

php script

Code: Select all

<?php
require_once('auth.php'); 
include("config.php");     
ini_set('display_errors', 1); 
error_reporting(E_ALL);   
       
if (isset($_POST['lab_id']) && is_numeric($_POST['lab_id'])) 
   {         
     $lab_id = $_POST['lab_id']; 
   } 
$targetpage = "view_all_chemicallab.php";      
    $limit      = 5; 
     
    $query = "SELECT COUNT(*) as num FROM chemical 
JOIN lab_inventory_chemical ON chemical.chemical_code = lab_inventory_chemical.chemical_code 
WHERE lab_inventory_chemical.lab_id= '$lab_id' ";         // this is line 71 
    $total_pages = mysql_fetch_array(mysql_query($query)); 
    $total_pages = $total_pages['num']; 
     
    $stages = 3; 
    $page =isset($_GET['name'])?mysql_escape_string($_GET['page']): ''; 
    if($page){ 
        $start = ($page - 1) * $limit; 
    }else{ 
        $start = 0;     
        }     
     
    // Get page data 
    $query1 = "SELECT * FROM chemical 
JOIN lab_inventory_chemical ON chemical.chemical_code = lab_inventory_chemical.chemical_code 
WHERE lab_inventory_chemical.lab_id='$lab_id' LIMIT $start, $limit";   //this is line 86 
    $result = mysql_query($query1); 
     
    // Initial page num setup 
    if ($page == 0){$page = 1;} 
    $prev = $page - 1;     
    $next = $page + 1;                             
    $lastpage = ceil($total_pages/$limit);         
    $LastPagem1 = $lastpage - 1;                     
     
     
    $paginate = ''; 
    if($lastpage > 1) 
    {     
     

     
     
        $paginate .= "<div class='paginate'>"; 
        // Previous 
        if ($page > 1){ 
            $paginate.= "<a href='$targetpage?page=$prev'>previous</a>"; 
        }else{ 
            $paginate.= "<span class='disabled'>previous</span>";    } 
             

         
        // Pages     
        if ($lastpage < 7 + ($stages * 2))    // Not enough pages to breaking it up 
        {     
            for ($counter = 1; $counter <= $lastpage; $counter++) 
            { 
                if ($counter == $page){ 
                    $paginate.= "<span class='current'>$counter</span>"; 
                }else{ 
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                     
            } 
        } 
        elseif($lastpage > 5 + ($stages * 2))    // Enough pages to hide a few? 
        { 
            // Beginning only hide later pages 
            if($page < 1 + ($stages * 2))         
            { 
                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++) 
                { 
                    if ($counter == $page){ 
                        $paginate.= "<span class='current'>$counter</span>"; 
                    }else{ 
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                     
                } 
                $paginate.= "..."; 
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>"; 
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";         
            } 
            // Middle hide some front and some back 
            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2)) 
            { 
                $paginate.= "<a href='$targetpage?page=1'>1</a>"; 
                $paginate.= "<a href='$targetpage?page=2'>2</a>"; 
                $paginate.= "..."; 
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++) 
                { 
                    if ($counter == $page){ 
                        $paginate.= "<span class='current'>$counter</span>"; 
                    }else{ 
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                     
                } 
                $paginate.= "..."; 
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>"; 
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";         
            } 
            // End only hide early pages 
            else 
            { 
                $paginate.= "<a href='$targetpage?page=1'>1</a>"; 
                $paginate.= "<a href='$targetpage?page=2'>2</a>"; 
                $paginate.= "..."; 
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++) 
                { 
                    if ($counter == $page){ 
                        $paginate.= "<span class='current'>$counter</span>"; 
                    }else{ 
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                     
                } 
            } 
        } 
                     
                // Next 
        if ($page < $counter - 1){ 
            $paginate.= "<a href='$targetpage?page=$next'>next</a>"; 
        }else{ 
            $paginate.= "<span class='disabled'>next</span>"; 
            } 
             
                 
    $paginate.= "</div>"; 
     
} 


// get results from database 
     //   $result = mysql_query("SELECT * FROM members") 
       //         or die(mysql_error());   
                 
        // display data in table 
        echo "<div>"; 
        echo "<table id='table1'>"; 
        echo "<tr> <th>Chemical Name</th> <th>Chemical Code</th> <th>Type</th> <th>Edit</th><th>Delete</th><th>Stock</th>  </tr>"; 

        // loop through results of database query, displaying them in the table 
        while($row = mysql_fetch_array( $result )) 
        { 
                 
                // echo out the contents of each row into a table 
                echo "<tr>"; 
                echo '<td>'. $row['name'] . '</td>'; 
                echo '<td>' . $row['chemical_code'] . '</td>'; 
                echo '<td>' . $row['type'] . '</td>'; 
                echo '<td><a href="editchemical.php?chemical_code=' . $row['chemical_code'] . '">Edit</a></td>'; 
                echo "<td><a href=\"delete_chemical.php?chemical_code=" . $row['chemical_code'] ."\" * onclick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>"; 
                echo '<td><a href="stock_chemical.php?chemical_code=' . $row['chemical_code'] . '">Stock</a></td>'; 
                echo "</tr>"; 
             
         
        } 
       echo "</table>"; 
       echo $paginate; 
       echo "</div>"; 

     
    ?>

This is my approach
Problem is with lab_id passing (As i think). So i have change this line
$targetpage = "view_all_chemicallab.php";
to this
$targetpage = "view_all_chemicallab.php?lab_id={$_REQUEST['lab_id']}";
reason for this is as i think when i click on paginated pages it goes as GET method and i am posting lab_id in the beginning so better to use REQUEST which suits for both methods. But problem is still there Your help is highly appreciated. Thanks in advance
User avatar
viddz
Sergeant Major
Sergeant Major
Posts: 45
Joined: Fri Aug 26, 2011 6:06 am
Location: Colombo

Re: Pagination error

Post by viddz » Thu Jun 21, 2012 10:06 pm

I have done this and make sure that error is because $lab_id is not passing to paginated pages.

Code: Select all

if (isset($_POST['lab_id']) && is_numeric($_POST['lab_id'])) 
   {         
     $lab_id = $_POST['lab_id']; 
   }else{$lab_id =0;} 
Now its a matter of how to pass $lab_id to paginated pages. any ideas??
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: Pagination error

Post by Saman » Sun Jun 24, 2012 5:23 pm

Can you try the following code. The logic is, in case the parameter is not passed or has a problem, we always set lab_id to 1. In this way lab_id is always defined.
Fine following code:

Code: Select all

if (isset($_POST['lab_id']) && is_numeric($_POST['lab_id']))
   {         
     $lab_id = $_POST['lab_id'];
   }  
Replace with:

Code: Select all

if (isset($_POST['lab_id']) && is_numeric($_POST['lab_id']))
{         
     $lab_id = $_POST['lab_id'];
}
else
{
     $lab_id = 1;
{
 
Let us know the result. Instead of $_POST, try using $_REQUEST. This is the array with all POSTs and GETs.
User avatar
viddz
Sergeant Major
Sergeant Major
Posts: 45
Joined: Fri Aug 26, 2011 6:06 am
Location: Colombo

Re: Pagination error

Post by viddz » Mon Jun 25, 2012 2:57 am

thank u for replying saman

[1.]

Code: Select all

else
{
     $lab_id = 1;
} 
After i have include this part in 1st page i have results relevant to selected LAB(eg selected lab_id = 5). Link on address bar.
http://localhost/chem/view_all_chemicallab.php now it is on page 1

But when i click on next button or 2nd page It shows results relevant Lab_id =1. Link on address bar.
http://localhost/chem/view_all_chemicallab.php?page=2
According to the link my pagination should be 2. But it is still on page 1.


[2.]
I have tried $_REQUEST but error is still there.
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: Pagination error

Post by Saman » Mon Jun 25, 2012 2:51 pm

Find

Code: Select all

        $page =isset($_GET['name'])?mysql_escape_string($_GET['page']): ''; 
        if($page){ 
            $start = ($page - 1) * $limit; 
        }else{ 
            $start = 0;     
            }
 
Replace With:

Code: Select all

        $page = isset($_GET['page']) ? mysql_escape_string($_GET['page']): 1; 
        $start = ($page - 1) * $limit; 
 
There are 3 changes I made.
1. I changed isset checking on page instead of name.
2. I changed page to return 1 instead of blank
3. I removed the if block

Check it out!
User avatar
viddz
Sergeant Major
Sergeant Major
Posts: 45
Joined: Fri Aug 26, 2011 6:06 am
Location: Colombo

Re: Pagination error

Post by viddz » Mon Jun 25, 2012 9:51 pm

now its showing results which belongs to lab_id=1 correctly, pagination is also correct. no errors :D but still i couldn't make this script work for POST $lab_id value

two changes that i have made as u said

Code: Select all

else
{
     $lab_id = 1;
}
 

Code: Select all

 $page = isset($_GET['page']) ? mysql_escape_string($_GET['page']): 1; 
      $start = ($page - 1) * $limit;
 
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: Pagination error

Post by Saman » Mon Jun 25, 2012 10:11 pm

$targetpage = "view_all_chemicallab.php?lab_id={$_REQUEST['lab_id']}";
I see curly brackets there which is problematic. Change that to following code to see how it goes.

Code: Select all

$targetpage = "view_all_chemicallab.php?lab_id=$_REQUEST['lab_id']";
 
User avatar
viddz
Sergeant Major
Sergeant Major
Posts: 45
Joined: Fri Aug 26, 2011 6:06 am
Location: Colombo

Re: Pagination error

Post by viddz » Mon Jun 25, 2012 10:39 pm

Saman wrote:
$targetpage = "view_all_chemicallab.php?lab_id={$_REQUEST['lab_id']}";
I see curly brackets there which is problematic.
I have tried it without curly brackets. Then it gave this error.
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING when i put curly brackets there is no such error thats why i have put curly brackets there
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: Pagination error

Post by Rksk » Mon Jun 25, 2012 10:48 pm

viddz wrote: I have tried it without curly brackets. Then it gave this error.
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING when i put curly brackets there is no such error thats why i have put curly brackets there
try this,

Code: Select all

$targetpage = "view_all_chemicallab.php?lab_id=$_REQUEST[lab_id]";
 
if not works, just check your variables by printing them on page.

[ Post made via Mobile Device ] Image
User avatar
viddz
Sergeant Major
Sergeant Major
Posts: 45
Joined: Fri Aug 26, 2011 6:06 am
Location: Colombo

Re: Pagination error

Post by viddz » Mon Jun 25, 2012 11:10 pm

variables passing correctly in page 1, but the matter is $lab_id isnt pass for the 2,3,4..... pages. what i want to here is correctly pass $lab_id for the other pages.

to get an idea about posting or geting values i have included

Code: Select all

echo '<pre>';print_r($_REQUEST);echo '</pre>'; 
after

Code: Select all

echo $paginate; 
       echo "</div>";  
this what i have got in
<<<<1st page>>>>>
Array
(
[lab_id] => 1
[page] => 2
[PHPSESSID] => 86i2ob11e1fg3pib7q8o5cggl2
)

<<<<2nd page>>>>>
Array
(
[lab_id] => 1?page=2
[PHPSESSID] => 86i2ob11e1fg3pib7q8o5cggl2
)

According to this $lab_id is passing to the 2nd paginated page. but still i got Undefined variable: lab_id error :(
Post Reply

Return to “PHP & MySQL”