Previous Next Function Driving me Mad

Discussion in 'Directories' started by john128, Aug 25, 2006.

  1. #1
    Hi,

    I have been trying to add the previous next function with page numbers a la google to the folowing code but I just cannot to it. Please could someone help me.

    include("/data.inc");
     
      $connection = mysql_connect($host,$user,$password)
           or die ("couldn't connect to server");
      $db = mysql_select_db($database,$connection)
           or die ("Couldn't select database");
    	 
    
    
     $query = "SELECT * FROM propertydetailstable ";
     if ($_POST['type'] == 'no_pref' AND $_POST['interest'] == 'no_pref') {
    	$query .= "WHERE Beds >= \"{$_POST[minbeds]}\" AND Eurocal >= \"{$_POST['minprice']}\" AND Eurocal <= \"{$_POST['maxprice']}\" ORDER BY Eurocal";   	}
    	elseif  ($_POST['type'] == 'no_pref') { 
     $query .= "WHERE Location_Look_up=\"{$_POST['interest']}\" AND Beds >= \"{$_POST[minbeds]}\" AND Eurocal >= \"{$_POST['minprice']}\" AND Eurocal <= \"{$_POST['maxprice']}\" ORDER BY Eurocal";   
    	}
    	elseif ($_POST['interest'] == 'no_pref') { */
        $query .= "WHERE Dwelling=\"{$_POST['type']}\" AND Beds >= \"{$_POST[minbeds]}\" AND Eurocal >= \"{$_POST['minprice']}\" AND Eurocal <= \"{$_POST['maxprice']}\" ORDER BY Eurocal";   
      }
    	else {
    			$query .= "WHERE Location_Look_up=\"{$_POST['interest']}\" AND Dwelling=\"{$_POST['type']}\" AND Beds >= \"{$_POST[minbeds]}\" AND Eurocal >= \"{$_POST['minprice']}\" AND Eurocal <= \"{$_POST['maxprice']}\" ORDER BY Eurocal"; 
    			}
      $result = mysql_query($query)
           or die ("Couldn't execute query.");
    
      while ($row = mysql_fetch_array($result,MYSQL_ASSOC))   
      { 
    
    echo '<br><br><A title="View details" ';
    echo 'href="http://www.dolphin-estates.com/propertydetails.php?ref=';
    echo "{$row['Ref_No']}";
    echo '">View ';
    echo 'details</A> <br><br></div>';
    echo '<div class="clear2"></div>';
     }
    
    
    ?>
    PHP:
    Thanks John
     
    john128, Aug 25, 2006 IP
  2. iwm1979

    iwm1979 Well-Known Member

    Messages:
    835
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    130
    #2
    Why don't you visit phpbuilder? They have quite a few code snippets with this functionality.
     
    iwm1979, Aug 26, 2006 IP
  3. john128

    john128 Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The code in the PHP brakets is the code I used and like it is it works fine, but I need it to display the search results useing the criteria from the search form.

    I tried adding the code below which searches the database for the results using an if statement but i just do not know how to add it to the code below.

    Do i need to create a temporary table with the search results?


    if ($_POST['type'] == 'no_pref' AND $_POST['interest'] == 'no_pref') {
    $sql .= "WHERE Beds >= \"{$_POST[minbeds]}\" AND Eurocal >= \"{$_POST['minprice']}\" AND Eurocal <= \"{$_POST['maxprice']}\" ORDER BY Eurocal"; }
    elseif ($_POST['type'] == 'no_pref') {
    $sql .= "WHERE Location_Look_up=\"{$_POST['interest']}\" AND Beds >= \"{$_POST[minbeds]}\" AND Eurocal >= \"{$_POST['minprice']}\" AND Eurocal <= \"{$_POST['maxprice']}\" ORDER BY Eurocal";
    }
    elseif ($_POST['interest'] == 'no_pref') {
    /*$query .= "WHERE Dwelling=\"".$_POST['type']."\" AND Beds >= \"".$_POST[minbeds]."\" AND Eurocal >= \"".$_POST['minprice']."\" AND Eurocal <= \"".$_POST['maxprice']."\"";
    } */
    $sql .= "WHERE Dwelling=\"{$_POST['type']}\" AND Beds >= \"{$_POST[minbeds]}\" AND Eurocal >= \"{$_POST['minprice']}\" AND Eurocal <= \"{$_POST['maxprice']}\" ORDER BY Eurocal";
    }
    else {
    $sql .= "WHERE Location_Look_up=\"{$_POST['interest']}\" AND Dwelling=\"{$_POST['type']}\" AND Beds >= \"{$_POST[minbeds]}\" AND Eurocal >= \"{$_POST['minprice']}\" AND Eurocal <= \"{$_POST['maxprice']}\" ORDER BY Eurocal";
    }





    
    <?php
      include("access.inc");
       $connection = mysql_connect($host,$user,$password)
           or die ("couldn't connect to server");
      $db = mysql_select_db($database,$connection)
           or die ("Couldn't select database");
    	 	
    
    
    
    class MySQLPagedResults {
    
       var $total_results_sql;
       var $current_page;
       var $results_per_page;
       var $links_per_page;
       var $previous_link_text;
       var $next_link_text;
    
       function MySQLPagedResults($total_results_sql,$page_name,$url_string,$results_per_page,$links_per_page,$first_link_text,$previous_link_text,$next_link_text,$last_link_text,$seperator) {
          $this->total_results_sql = $total_results_sql;
          if(!isset($_GET[$page_name])) {
             $this->current_page = 1;
          } else {
             $this->current_page = $_GET[$page_name];
          }
          $this->results_per_page = $results_per_page;
          $this->links_per_page = $links_per_page;
          $this->previous_link_text = $previous_link_text;
          $this->next_link_text = $next_link_text;
          $this->first_link_text = $first_link_text;
          $this->last_link_text = $last_link_text;
          $this->page_name = $page_name;
          $this->url_string = $url_string;
          $this->seperator = $seperator;
       }
    
       function totalResults() {
          $query = mysql_query($this->total_results_sql);
          $result = mysql_fetch_array($query);
          return $result[0];
       }
    
       function totalPages() {
          return ceil($this->totalResults()/$this->results_per_page);
       }
    
       function currentOffset() {
          return ($this->current_page-1)*$this->results_per_page;
       }
    
       function isFirstPage() {
          return ($this->current_page <= 1);
       }
    
       function isLastPage() {
          return ($this->current_page >= $this->totalPages());
       }
    
       function getPrevNav() {
          $nav='';
          //Deal with previous link
          if(!$this->isFirstPage()) {
             $nav.='<a href="?'.$this->page_name.'='.($this->current_page-1).''.$this->url_string.'">'.$this->previous_link_text.'</a>';
          } else {
             $nav.=$this->previous_link_text;
          }
          return $nav;
       }
    
       function getNextNav() {
          //Deal with next link
          if(!$this->isLastPage()) {
             $nav.='<a href="?'.$this->page_name.'='.($this->current_page+1).''.$this->url_string.'">'.$this->next_link_text.'</a>';
          } else {
             $nav.=$this->next_link_text;
          }
          return $nav;
       }
    
       function getFirstNav() {
          $nav='';
          //Deal with previous link
          if(!$this->isFirstPage()) {
             $nav.='<a href="?'.$this->page_name.'=1'.$this->url_string.'">'.$this->first_link_text.'</a>';
          } else {
             $nav.=$this->first_link_text;
          }
          return $nav;
       }
    
       function getLastNav() {
          $nav='';
          //Deal with previous link
          if(!$this->isLastPage()) {
             $nav.='<a href="?'.$this->page_name.'='.$this->totalPages().''.$this->url_string.'">'.$this->last_link_text.'</a>';
          } else {
             $nav.=$this->last_link_text;
          }
          return $nav;
       }
    
       function getResultNumbersStart() {
          return ($this->current_page*$this->results_per_page)-$this->results_per_page+1;
       }
    
       function getResultNumbersEnd() {
          return $this->getResultNumbersStart()+$this->results_per_page-1;
       }
    
       function getStartNumber() {
          $links_per_page_half = $this->links_per_page/2;
          if($this->current_page<=$links_per_page_half) {
             return 1;
          } elseif($this->current_page>=$this->totalPages()-$links_per_page_half) {
             return $this->totalPages()-$this->links_per_page+1;
          } else {
             return $this->current_page-$links_per_page_half;
          }
       }
    
       function getEndNumber() {
          if($this->totalPages() < $this->links_per_page) {
             return $this->totalPages();
          } else {
             return $this->links_per_page;
          }
       }
    
       function getPagesNav() {
          $nav='';
          for($i=$this->getStartNumber(); $i<$this->getStartNumber()+$this->getEndNumber(); $i++) {
             if($i!=$this->current_page) {
                $nav.="<a href='?".$this->page_name."=$i".$this->url_string."'>$i</a>";
             } else {
                $nav.="$i";
             }
             if($i!=$this->getStartNumber()+$this->getEndNumber()-1) { $nav.=$this->seperator; }
          }
          return $nav;
       }
    
    }
    
    
    
    
    
    
    
    $paging_results = new MySQLPagedResults("SELECT count(*) FROM propertydetailstable","page","",25,10,"<<","Previous","Next",">>"," | ");
    
    
    
    //Make the variables from the class easier to read/use
    $first_nav = $paging_results->getFirstNav();
    $prev_nav = $paging_results->getPrevNav();
    $next_nav = $paging_results->getNextNav();
    $last_nav = $paging_results->getLastNav();
    $pages_nav = $paging_results->getPagesNav();
    $offset = $paging_results->currentOffset();
    $results_per_page = $paging_results->results_per_page;
    $current_page = $paging_results->current_page;
    $total_pages = $paging_results->totalPages();
    $start_number = $paging_results->getResultNumbersStart();
    $end_number = $paging_results->getResultNumbersEnd();
    
    //Loop through a table of our database using the $offset and $results_per_page variables from our class.
    $sql = "SELECT * FROM propertydetailstable LIMIT $offset,$results_per_page";
    
    $query = mysql_query($sql);
    while($row = mysql_fetch_array($query)) {
     #  echo $data[0] . ", ";
       echo '<div class="summary">';
    echo '<div class="resultsTitle"><span class="propertyResultsListingHeader">';
    echo "{$row['Ref No']}";
    echo ' in ';
    echo "{$row['Location_Look_up']}";
    }
    
    echo "<p><b>Page:</b> $current_page of $total_pages &nbsp;|&nbsp; <b>Results:</b> $start_number - $end_number<br>";
    echo "$first_nav $prev_nav $pages_nav $next_nav $last_nav</p>";
    
    
    ?>
    PHP:
     
    john128, Aug 28, 2006 IP