SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My Search result aint displaying on the second page

    Good day all programmers in the house. please I need help in the following area. The code posted below works perfectly for search and paginating my result as I used the same code for other pagination like product listing which doesn't involve any search but the challenge I have now is that after displaying the result on the search page (The page is self processing page), clicking the link to page 2 or 3 or other pages comes up with a blank page. I thought its because I used $_GET instead of $_POST but using $_POST doesn't solve the problem. Please what do I do to get the pagination links to work.
    PHP Code:
    //This does the actual searching
    if (isset($_GET['action']) and $_GET['action'] == 'search')
    {

    //connect to the database
        
    if(include_once ('DatabaseManager.php')); 
        
    $conn DatabaseManager::getConnection();
        
    //get the function
        
    include_once ('function.php');

            
    $page = (int) (!isset($_GET["page"]) ? $_GET["page"]);
            
    $limit 1;
            
    $startpoint = ($page $limit) - $limit;
           

    if (
    $_GET['product'] != '')
    {
       
    $product trim($_GET['product']);
    }

    if (
    $_GET['purchaseDateFrom'] != '')
    {
       
    $purchaseDateFrom trim($_GET['purchaseDateFrom']);
    }

    if (
    $_GET['purchaseDateTo'] != '')
    {
       
    $purchaseDateTo trim($_GET['purchaseDateTo']);
    }

    if (
    $_GET['manufacturer'] != '')
    {
       
    $manufacturer trim($_GET['manufacturer']);
    }

               
    $statement "assets WHERE ";

    if(isset(
    $product)){    
                       
    $statement .= "product LIKE '%" $product .  "%' AND ";
                    }

    if(isset(
    $manufacturer)){    
                       
    $statement .= "manufacturer LIKE '%" $manufacturer .  "%' AND ";
                    }
    if(isset(
    $purchaseDateFrom) && isset($purchaseDateTo)){    
                       
    $statement .= "(purchaseDate >=  '$purchaseDateFrom' AND purchaseDate <= '$purchaseDateTo') AND ";
                    }
    $statement .= '1';

     
    $query $conn->query("SELECT * FROM {$statement} ORDER BY id DESC LIMIT {$startpoint} , {$limit}"); 
     
    $assetRegister $conn->query("SELECT * FROM {$statement}"); 
     
    $asset $assetRegister->rowCount(); 


    The included function is below
    PHP Code:
    function pagination($query$per_page 10,$page 1$url '?'){ 
       include_once (
    'DatabaseManager.php'); 
       
    $conn DatabaseManager::getConnection();
               
            
    $query "SELECT COUNT(*) as `num` FROM {$query}";
            foreach(
    $conn->query($query) as $row);
            
    $total $row['num'];
            
    $adjacents "2"

            
    $page = ($page == $page);  
            
    $start = ($page 1) * $per_page;                                
            
            
    $prev $page 1;                            
            
    $next $page 1;
            
    $lastpage ceil($total/$per_page);
            
    $lpm1 $lastpage 1;
            
            
    $pagination "";
            if(
    $lastpage 1)
            {    
                
    $pagination .= "<ul class='pagination'>";
                        
    $pagination .= "<li class='details'>Page $page of $lastpage</li>";
                if (
    $lastpage + ($adjacents 2))
                {    
                    for (
    $counter 1$counter <= $lastpage$counter++)
                    {
                        if (
    $counter == $page)
                            
    $pagination.= "<li><a class='current'>$counter</a></li>";
                        else
                            
    $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                    }
                }
                elseif(
    $lastpage + ($adjacents 2))
                {
                    if(
    $page + ($adjacents 2))        
                    {
                        for (
    $counter 1$counter + ($adjacents 2); $counter++)
                        {
                            if (
    $counter == $page)
                                
    $pagination.= "<li><a class='current'>$counter</a></li>";
                            else
                                
    $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                        }
                        
    $pagination.= "<li class='dot'>...</li>";
                        
    $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
                        
    $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";        
                    }
                    elseif(
    $lastpage - ($adjacents 2) > $page && $page > ($adjacents 2))
                    {
                        
    $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                        
    $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                        
    $pagination.= "<li class='dot'>...</li>";
                        for (
    $counter $page $adjacents$counter <= $page $adjacents$counter++)
                        {
                            if (
    $counter == $page)
                                
    $pagination.= "<li><a class='current'>$counter</a></li>";
                            else
                                
    $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                        }
                        
    $pagination.= "<li class='dot'>..</li>";
                        
    $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
                        
    $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";        
                    }
                    else
                    {
                        
    $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                        
    $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                        
    $pagination.= "<li class='dot'>..</li>";
                        for (
    $counter $lastpage - (+ ($adjacents 2)); $counter <= $lastpage$counter++)
                        {
                            if (
    $counter == $page)
                                
    $pagination.= "<li><a class='current'>$counter</a></li>";
                            else
                                
    $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                        }
                    }
                }
                
                if (
    $page $counter 1){ 
                    
    $pagination.= "<li><a href='{$url}page=$next'>Next</a></li>";
                    
    $pagination.= "<li><a href='{$url}page=$lastpage'>Last</a></li>";
                }else{
                    
    $pagination.= "<li><a class='current'>Next</a></li>";
                    
    $pagination.= "<li><a class='current'>Last</a></li>";
                }
                
    $pagination.= "</ul>\n";        
            }
        
        
            return 
    $pagination;
        } 

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Seems, that you have to pass your search parameters to your pagination function, so that correct links would get generated.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please how do I do that? Just an example will let me know how to go by it.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Before calling pagination() you can add variables to $url as needed.
    This is just an example just adds all existing $_GET variables, except $page, which is added to $url in the function itself.
    PHP Code:
    $url '?';
    foreach(
    $_GET as $k => $v) {
        if(
    $k != 'page') {
            
    $url .= "{$k}={$v}&";
        }
    }

    ...

    pagination($query$per_page$page$url); 

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ronald, you're too brilliant, you're a genius man. thanks, it works very well. Thanks man.

  6. #6
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by papadammy View Post
    Ronald, you're too brilliant, you're a genius man. thanks, it works very well. Thanks man.
    That code is also a major security threat and should not be used under any conditions whatsoever.

    Also, your pagination code is a complete mess.

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,142
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    That code is also a major security threat and should not be used under any conditions whatsoever.

    Also, your pagination code is a complete mess.
    Please elaborate this further.

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    That code is also a major security threat and should not be used under any conditions whatsoever.

    Also, your pagination code is a complete mess.
    Of course, the code should not be copied and used as is, I was showing just a principle of getting the variables from $_GET array, in real world situation those should be escaped before outputting to page.

  9. #9
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ronalds View Post
    Of course, the code should not be copied and used as is, I was showing just a principle of getting the variables from $_GET array, in real world situation those should be escaped before outputting to page.
    You should always explicitly grab the variables from $_GET and $_POST superglobals, because doing automatic stuff like this means you leave yourself open to any number of potential attacks. If you are explicit, then only the values and variables you want to enter into your program can, whereas if you try to be too clever like this, you can end up with the potential for sql injection and xss exploits, depending on the context. It's just not a good idea, and it's not a good thing to recommend.

    In terms of why the pagination class is a mess - primarily because all the presentation logic is tied into the class itself. The class should just handle the logic and should not contain any html. The html build should be called outside of this, and should only reference the class to work out which position it's in and which links should come next etc. Sticking the presentation logic into the class itself makes it very hard to change the design at a later date, and would mean you wouldn't be able to use the same class on multiple projects, which you would be able to do if it was more carefully designed.

  10. #10
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please guys, I need more help with this question. I want a situation whereby the user of my application can choose how many record set should be displayed per page by making a selection from a drop down menu I will incorporate into the search page. How do I accomplish this?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •