Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    96
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Question Contents of page 2 not being displayed

    The PHP code below is not loading the contents of page 2. Could someone tell me why please?

    Code:
    <?php
    
    $page = (int) $_GET['page'];
    if ($page < 1) $page = 1;
    
             // Include database connection settings
              include('config.inc'); 
    
             //-select the database to use
             $mydb=mysql_select_db("a3763404_posts");
    
    if(isset($_POST['submit']))
    {
    if(isset($_GET['go']))
      {
         if(preg_match("/[A-Z | a-z]+/", $_POST['name']))
    	    {
              $name=$_POST['name'];
    
            //-query the database table
            //$sql="SELECT * FROM entries WHERE Title LIKE '%" . $name . "%' OR Title LIKE '%" . $name . "%' OR Post LIKE '%" . $name ."%' OR Date LIKE '%" . $name ."%'LIMIT $startResults, $resultsPerPage";  
    
            //-run the query against the mysql query function
            $result=mysql_query("SELECT * FROM entries WHERE Title LIKE '%" . $name . "%' OR Post LIKE '%" . $name ."%' OR Date LIKE '%" . $name ."%' LIMIT $startResults, $resultsPerPage");
    
            //-count results
            
    		$numberOfPages = 100;
            $resultsPerPage = 20;
            $startResults = ($page - 1) * $resultsPerPage;
    		$numrows = mysql_num_rows(mysql_query("SELECT * FROM entries WHERE Title LIKE '%" . $name . "%' OR Post LIKE '%" . $name ."%' OR Date LIKE '%" . $name ."%'"));
            $totalPages = ceil($numrows / $resultsPerPage);
    		$result=mysql_query("SELECT * FROM entries WHERE Title LIKE '%" . $name . "%' OR Post LIKE '%" . $name ."%' OR Date LIKE '%" . $name ."%' LIMIT $startResults, $resultsPerPage");
            //$numrows=mysql_num_rows($numrows);  
    
    		
            echo "<p>" .$numrows . " Results found for " . stripslashes($name) . "</p>"; 
    
            
               while ($output = mysql_fetch_array($result))
                 { 
                   echo '<a href="selected_post.php?Index='.$output['Index'].'">' .$output['Title'].'</a><br>'; 
                 }
    
           $halfPages = floor($numberOfPages / 2);
           $range = array('start' => 1, 'end' => $totalPages);
           $isEven = ($numberOfPages % 2 == 0);
           $atRangeEnd = $totalPages - $halfPages;
    
           if($isEven) $atRangeEnd++;
    
           if($totalPages > $numberOfPages)
                {
    	         if($page <= $halfPages)
    		     $range['end'] = $numberOfPages;
    	         elseif ($page >= $atRangeEnd)
    		     $range['start'] = $totalPages - $numberOfPages + 1;
    	         else
    	           {
    		         $range['start'] = $page - $halfPages;
    		         $range['end'] = $page + $halfPages;
    		         if($isEven) $range['end']--;
    	           }
                }
    
            if($page > 1)
    	    echo '<a href="?page='.($page - 1).'">&laquo;</a>&nbsp';
    
            for ($i = $range['start']; $i <= $range['end']; $i++)
              {
    	        if($i == $page)
    		    echo '<strong>'.$i.'</strong>&nbsp;';
    	        else
    		    echo '<a href="?page='.$i.'">'.$i.'</a>&nbsp;';
              }
            if ($page < $totalPages)
    	     echo '<a href="?page='.($page + 1).'">&raquo;</a>&nbsp;';
                          
            }
                     
        else
            {
               echo "<p>Please enter a search query</p>";
            }	
       }
     }
    ?>

  • #2
    New Coder
    Join Date
    Sep 2012
    Posts
    96
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Unhappy Help in understanding why contents of page 2 are not being displayed

    Below is the code for searching data from a database and displaying the results on a webpage. The data to be searched is gotten from a form i.e. a search box. The code should list the first 20 results on page 1, the next 20 on page 2 and so on. Why is the code below not displaying results for page 2 onwards?

    Code:
    <?php
    
    $page = (int) $_GET['page'];
    if ($page < 1) $page = 1;
    
             // Include database connection settings
              include('config.inc'); 
    
             //-select the database to use
             $mydb=mysql_select_db("a3763404_posts");
    
    if(isset($_POST['submit']))
    {
    if(isset($_GET['go']))
      {
         if(preg_match("/[A-Z | a-z]+/", $_POST['name']))
    	    {
              $name=$_POST['name'];
    
            //-query the database table
            //$sql="SELECT * FROM entries WHERE Title LIKE '%" . $name . "%' OR Title LIKE '%" . $name . "%' OR Post LIKE '%" . $name ."%' OR Date LIKE '%" . $name ."%'LIMIT $startResults, $resultsPerPage";  
    
            //-run the query against the mysql query function
            $result=mysql_query("SELECT * FROM entries WHERE Title LIKE '%" . $name . "%' OR Post LIKE '%" . $name ."%' OR Date LIKE '%" . $name ."%' LIMIT $startResults, $resultsPerPage");
    
            //-count results
            
    		$numberOfPages = 100;
            $resultsPerPage = 20;
            $startResults = ($page - 1) * $resultsPerPage;
    		$numrows = mysql_num_rows(mysql_query("SELECT * FROM entries WHERE Title LIKE '%" . $name . "%' OR Post LIKE '%" . $name ."%' OR Date LIKE '%" . $name ."%'"));
            $totalPages = ceil($numrows / $resultsPerPage);
    		$result=mysql_query("SELECT * FROM entries WHERE Title LIKE '%" . $name . "%' OR Post LIKE '%" . $name ."%' OR Date LIKE '%" . $name ."%' LIMIT $startResults, $resultsPerPage");
            //$numrows=mysql_num_rows($numrows);  
    
    		
            echo "<p>" .$numrows . " Results found for " . stripslashes($name) . "</p>"; 
    
            
               while ($output = mysql_fetch_array($result))
                 { 
                   echo '<a href="selected_post.php?Index='.$output['Index'].'">' .$output['Title'].'</a><br>'; 
                 }
    
           $halfPages = floor($numberOfPages / 2);
           $range = array('start' => 1, 'end' => $totalPages);
           $isEven = ($numberOfPages % 2 == 0);
           $atRangeEnd = $totalPages - $halfPages;
    
           if($isEven) $atRangeEnd++;
    
           if($totalPages > $numberOfPages)
                {
    	         if($page <= $halfPages)
    		     $range['end'] = $numberOfPages;
    	         elseif ($page >= $atRangeEnd)
    		     $range['start'] = $totalPages - $numberOfPages + 1;
    	         else
    	           {
    		         $range['start'] = $page - $halfPages;
    		         $range['end'] = $page + $halfPages;
    		         if($isEven) $range['end']--;
    	           }
                }
    
            if($page > 1)
    	    echo '<a href="?page='.($page - 1).'">&laquo;</a>&nbsp';
    
            for ($i = $range['start']; $i <= $range['end']; $i++)
              {
    	        if($i == $page)
    		    echo '<strong>'.$i.'</strong>&nbsp;';
    	        else
    		    echo '<a href="?page='.$i.'">'.$i.'</a>&nbsp;';
              }
            if ($page < $totalPages)
    	     echo '<a href="?page='.($page + 1).'">&raquo;</a>&nbsp;';
                          
            }
                     
        else
            {
               echo "<p>Please enter a search query</p>";
            }	
       }
     }
    ?>


  •  

    Posting Permissions

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