Jump to content

PHP Pagination working locally, but not on the live server


Nonks

Recommended Posts

Hey guys, i was wondering if any1 cud help me. I have a database search engine that I'm currently working on, everything seems to be working fine locally including pagination but as soon as i upload the site online, the pagination doesn't work properly. when you use the search bar the results on the first page are correct, but as soon as you click next or page 2 it automatically returns wrong data. how can i fix this since it works fine locally???

Link to comment
Share on other sites

Im using PHP

 

 

this is my form:

 

<form class="searchbox" action="display.php" method="get">
        <input type="text" name="keyword" placeholder="What?..." />
        <input type="text" name="Countries"  placeholder="Where?..." />
        <button type="submit" ></button>
    </form>

 

 

This is what i have before my header:

 

<?php


session_start();


if(isset($_GET['keyword']) && isset ($_GET['Countries']) )
{
    $_SESSION['sname'] = $_GET['keyword'];
    $_SESSION['cname'] = $_GET['Countries'];
    
    
    
}
?>
 

 

 

this is my PHP code including pagination:

 

   <?php
    
 



include('includes/connect.php');


$Lname = isset( $_GET['sname'] )? $_GET['sname']: false;
$mname = isset( $_GET['cname'] )? $_GET['cname']: false;


$Lname= $_SESSION['sname'];
$mname= $_SESSION['cname'];



    $tableName="company_details";    
    $tableName1 = "access_adverts";
    $targetpage = "display.php";     
    $limit = 4;
    $number = 4;
    
    $Lname = mysql_real_escape_string($Lname);
    $mname = mysql_real_escape_string($mname);
    
    $query = "SELECT COUNT(*) as num FROM $tableName WHERE keyword LIKE '%$Lname%' AND Countries LIKE '%$mname%' ORDER BY Cname ";


        
        $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages['num'];
    
    $stages = 3;
    
    $page = (isset($_GET['page'])) ? intval($_GET['page']) : 1;
    //$page = mysql_escape_string($_GET['page']);
    if($page){
        $start = ($page - 1) * $limit;
    }else{
        $start = 0;    
        }    
        
    
    // Get page data

    $query1 = "SELECT * FROM $tableName WHERE keyword LIKE '%$Lname%' AND Countries LIKE '%$mname%' ORDER BY CompId DESC LIMIT $start, $limit";
    
    
    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>";        
        

}


$sql=mysql_query("SELECT * FROM $tableName WHERE keyword LIKE '%$Lname%' AND Countries LIKE '%$mname%' ORDER BY CompId DESC LIMIT $start, $limit") or die (mysql_error()."<br>Error: Advanced Listings Error");
$count = mysql_num_rows($sql);

while($row = mysql_fetch_array($sql))
 {
 $adv_id = $row['CompId'];
 $img_id = $row['CompId'];
 
 
 

//GET LOGO details from DB //

$logo=mysql_query("SELECT lo_id, lo_path, lo_link FROM $db_name.access_logo WHERE CompId='$img_id' ")or die(mysql_error());
$cnt = mysql_num_rows($logo);

if ($cnt == 0) { $logo=mysql_query("SELECT lo_path, lo_link FROM $db_name.access_adverts WHERE CompId='$adv_id' ")or die(mysql_error());}

while($log = mysql_fetch_array($logo))
 
{


$link=$log['lo_link'];
$imgurl=$log['lo_path'];

}


 
 //LOGO CODE ENDS //

  echo
 
 
"<div class='newsbox11'><table width=\"900\" cellpadding=\"0\" cellspacing=\"0\" valign=\"bottom\" >

<tr>

<td width=\"75\" rowspan=\"1\" valign=\"top\">
<div class='aligner-image'>

<a href='$link' target='blank' onClick=\"return popup2(this, 'notes')\"><img src=\"$imgurl\"border=\"0\" ></a>

</div>
</td>


<td colspan=\"2\" valign=\"bottom\">

<div class='aligner2'>

<b><font size='2' face = 'arial' color='#1b7a94'><a href='display.php?id=$adv_id' onClick=\"return popup1(this, 'notes')\">{$row['Cname']}</a></font>  -
<font size='2' face = 'arial' color='#1b7a94'></font> <font size='2' face = 'arial' color='#333'>{$row['Ccategory']}</font></b>
 <b><font size='2' face = 'arial' color='#333'>({$row['prov']})</font></b><br>



<b><font size='2' face = 'arial' color='#333'>Tel:</font> <font size='2' face = 'arial' color='#333'>{$row['Ctel']}</font> | 
<font size='2' face = 'arial' color='#333'>Fax:</font> <font size='2' face = 'arial' color='#333'>{$row['Cfax']}</font></b>    <br>


<i><font size='2' face = 'arial' color='#929393'>{$row['physical']}</font></i>   <br>
<i><font size='2' face = 'arial' color='#929393'>{$row['Postal']}</font></i>   <br>
</div>

</td>



</tr>

</table>
</div>
";
}
    
    ?>
    <br />
    
    <div class="pag">
<?php

 echo $paginate;
?>
 
</div>

Link to comment
Share on other sites

I would advise you to move over to a new SQL extension such as mysqli or PDO making use of prepared statements as the method you're using opens you up to SQL injections. Have you tried debugging at all to find out the value of $start and $limit when you access page 2? If this value is incorrect, you may find that the code you're using to create your page number later in the program isn't being calculated correctly?

Link to comment
Share on other sites

hey Ian, i haven't tried debugging, I'm kinda new in PHP. so please tell me, why is the code working perfectly locally?? because i have tested it using different browsers but as soon as i Upload it stops working

Link to comment
Share on other sites

Are your session variables being set correctly? I have seen scenarios where if the location of the session variable is set incorrectly on the server, the variables aren't saved as expected.

 

I would also suggest re-thinking your logic as the following does not make sense to me:

 

$Lname = isset( $_GET['sname'] )? $_GET['sname']: false;
$mname = isset( $_GET['cname'] )? $_GET['cname']: false;
 

$Lname= $_SESSION['sname'];
$mname= $_SESSION['cname'];

 

You set the variables to be the value of the GET variables and following this, set them to be the value of the session variables, can I ask why? Using your current logic, you could be passing FALSE or a null value into your SQL query - something that doesn't make sense to me?

 

At the beginning of your code, you set your session variables to be the value of the GET variables if set. From my understanding of session variables, their new value won't be accessible until the next page you visit.

Edited by IanA
Link to comment
Share on other sites

I have also noticed, your GET variable names don't match  

 

$Lname = isset( $_GET['sname'] )? $_GET['sname']: false;
$mname = isset( $_GET['cname'] )? $_GET['cname']: false;

 

Where is sname and cname being placed into a URL? Maybe I just can't see it?

 

Try the below - Not checked for any syntax errors

 

if(isset($_GET['keyword']) && isset ($_GET['Countries']) )
{
    $_SESSION['sname'] = $_GET['keyword'];
    $_SESSION['cname'] = $_GET['Countries'];
 }

else if(!isset($_SESSION['sname]) || !isset($_SESSION['cname]))

{

      Error handling - This shouldn't happen unless session variables have timed out.

}

 

$Lname = isset( $_GET['keyword] )? $_GET['keyword]: $_SESSION['sname];

$mname = isset( $_GET['Countries] )? $_GET['Countries]: $_SESSION['cname];

 

Replace the relevant sections with the above and let me know what happens. I really think you should be echoing these variables somewhere to find out their true value either using error_log or something similar?

Link to comment
Share on other sites

As I said earlier. Debugging the variables to find out what they are is surely to be the best way forward with this issue. If it's just a development environment, you can echo or error_log these variables to the page and you will be able to see if anything is incorrect. 

 

If those variables are incorrect, you will be receiving bad results from your SQL query.

 

Furthermore, have you ensured you've deleted the below lines following the replacement of the modified code snippet I gave you:

 

$Lname= $_SESSION['sname'];
$mname= $_SESSION['cname'];

Edited by IanA
Link to comment
Share on other sites

because of the mixup in the get/session variable names, about the only way the original code could work is if register_globals are on, which would cause the $_GET['sname'] and $_GET['cname'] variables to magically set the correct $_SESSION['sname'] and $_SESSION['cname'] variables.

 

the solution is to use and match up the correct get/session names so that they are what you want and your program logic sets the session variable from the get variable only when the get variable exists and has been validated (assuming you need the session variable at all.) you currently have three different names for each set of data. pick a consistent name for each set that indicates the meaning of the data in the variable and use that same name for the get, session, and regular php variable (or just get rid of the regular php variable and use the session variable in it's place or if you don't need the session variable, just use the get variable everywhere.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.