2012-02-22 27 views
0

我需要找到一種方式來獲得A HREF從這個HREF /箱開關需要幫助

<a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> "; 

去像

<a href='index.php?menukey=7... then currentpage=1'>First</a>"; 

我需要一個分頁頁面的開關打開案例區域。以下代碼是我對分頁頁面的要求。任何幫助,將不勝感激。

謝謝

<?php 
require_once ('mysqli_connect.php'); 
$sql = "SELECT COUNT(NewCustomerID) FROM customer"; 
$result = @mysqli_query($dbc, $sql); 
$r = @mysqli_fetch_row($result); 
$numrows = $r[0]; 

$rowsperpage = 10; 

    // find out total pages 
    $totalpages = ceil($numrows/$rowsperpage); 

    // get the current page or set a default 
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { 
     // cast var as int 
     $currentpage = (int) $_GET['currentpage']; 
    } else { 
     // default page num 
     $currentpage = 1; 
    } // end if 


    // if current page is greater than total pages... 
    if ($currentpage > $totalpages) { 
     // set current page to last page 
     $currentpage = $totalpages; 
    } // end if 

    // if current page is less than first page... 
    if ($currentpage < 1) { 
     // set current page to first page 
     $currentpage = 1; 
    } // end if 

    // the offset of the list, based on current page 
    $offset = ($currentpage - 1) * $rowsperpage; 


    $q = "SELECT CONCAT(left(FirstName,1),left(MiddleName,1),LastName) AS UserName, 
     CONCAT(LastName, ', ', FirstName, ' ', MiddleName) AS Name, 
     (NewCustomerID) AS customerid, 
     (OldCustomerID) AS oldcustomerid, 
     (zlu_birthmonth.Description) AS birthmonth, 
     (zlu_cars.Description) AS cartype, 
     (zlu_carcolor.Description) AS carcolor,  
     (zlu_computers.Description) AS computer, 
     (zlu_race.Description) AS race, 
     (zlu_residence.Description) AS residence, 
     (IsLaptop) AS IsLaptop, 
     CASE IsLaptop 
      WHEN '1' THEN 'Yes' 
      WHEN '0' THEN 'No' 
     END AS laptop 

    FROM customer 
    INNER JOIN zlu_cars ON(customer.CarID = zlu_cars.CarID) 
    INNER JOIN zlu_birthmonth ON(customer.BirthMonthID = zlu_birthmonth.BirthMonthID) 
    INNER JOIN zlu_carcolor ON (customer.CarColorID = zlu_carcolor.CarColorID) 
    INNER JOIN zlu_computers ON (customer.ComputerID = zlu_computers.ComputerID) 
    INNER JOIN zlu_race ON(customer.RaceID = zlu_race.RaceID) 
    INNER JOIN zlu_residence ON(customer.ResidenceID = zlu_residence.ResidenceID) 
    order by NewCustomerID LIMIT $offset, $rowsperpage"; 

$result = @mysqli_query($dbc, $q); if(!$result){die(mysqli_error($dbc));} 


    echo '<table border="1"> 
       <tr> 
       <th>Customer ID</th> 
       <th>Old Customer ID</th> 
       <th>Customer Name</th> 
       <th>UserName</th> 
       <th>Car</th> 
       <th>Car Color</th> 
       <th>Birth Month</th> 
       <th>Computer Brand</th> 
       <th>Laptop</th> 
       <th>Race</th> 
       <th>Residence</th> 
      </tr>'; 

    $bg = '#eeeeee'; // set initial back ground color 

    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
     $bg = ($bg =='#eeeeee' ? '#ffffff' : '#eeeeee'); // switch the background color. 

    echo '<tr bgcolor="' . $bg . '"> 
       <td>' . $row['customerid']. '</td> 
       <td>' . $row['oldcustomerid']. '</td> 
       <td>' . $row['Name'].'</td> 
       <td>' . $row['UserName'].'</td> 
       <td>' . $row['cartype'].'</td> 
       <td>' . $row['carcolor'].'</td> 
       <td>' . $row['birthmonth'].'</td> 
       <td>' . $row['computer'].'</td> 
       <td>' . $row['laptop'].'</td> 
       <td>' . $row['race'].'</td> 
       <td>' . $row['residence'].'</td> 
     </tr>'; } // end of while loop 

echo '</table>'; 


?> 
<?php 

/****** build the pagination links ******/ 
// range of num links to show 
$range = 3; 
if($currentpage==1) 
{ 
    echo '<span class="prn"> First &lt;&lt;</span>&nbsp;'; 
} 
if ($currentpage > 1) { 
    // show << link to go back to page 1 

    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> "; 
    // get previous page num 
    $prevpage = $currentpage - 1; 
    // show < link to go back to 1 page 

    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'> < </a> "; 
} // end if 

// loop to show links to range of pages around current page 
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { 
    // if it's a valid page number... 
    if (($x > 0) && ($x <= $totalpages)) { 
     // if we're on current page... 
     if ($x == $currentpage) { 
     // 'highlight' it but don't make a link 
     echo " <b>$x</b> "; 
     // if not current page... 
     } else { 
     // make it a link 
     echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; 
     } // end else 
    } // end if 
} // end for 

// if not on last page, show forward and last page links   
if ($currentpage != $totalpages) { 
    // get next page 
    $nextpage = $currentpage + 1; 
    // echo forward link for next page 
    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'> > </a> "; 
    // echo forward link for lastpage 
    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a> "; 
} // end if 
else 
{ 
    echo '<span class="prn"> Last &gt;&gt;</span>&nbsp;'; 
} 
/****** end build pagination links ******/ 
mysqli_close($dbc); 
?> 
+0

你的問題不是很清楚,你的意思是你怎麼追加其它值的聯繫?比如'index.php?currentpage = 1&menukey = 7'或者你需要幫助才能得到menukey? – Avanche 2012-02-22 10:27:13

+0

Nope Ricardo,將index.php?menukey = 7添加到鏈接。 – 2012-02-22 11:14:03

+0

還是不太清楚我想,你是否想用'index.php?menukey = 7&currentpage = 1'替換'{$ _SERVER ['PHP_SELF']}?currentpage = 1'是它嗎? – Avanche 2012-02-22 12:48:59

回答

0

試試這個:

<?php 
$url = $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']; 
echo $url; 
+0

那麼我怎麼會得到該菜單鍵打開? BPE? – 2012-02-22 11:18:46

0

頁代碼

IT-5334:PHP和MySQL - >

<?php 
    //Include the PS_Pagination class 
    include('ps_pagination.php'); 
    require_once ('mysql_connect.php'); 

    $q = "SELECT CONCAT(left(FirstName,1),left(MiddleName,1),LastName) AS UserName, 
     CONCAT(LastName, ', ', FirstName, ' ', MiddleName) AS Name, 
     (NewCustomerID) AS customerid, 
     (OldCustomerID) AS oldcustomerid, 
     (zlu_birthmonth.Description) AS birthmonth, 
     (zlu_cars.Description) AS cartype, 
     (zlu_carcolor.Description) AS carcolor,  
     (zlu_computers.Description) AS computer, 
     (zlu_race.Description) AS race, 
     (zlu_residence.Description) AS residence, 
     (IsLaptop) AS IsLaptop, 
     CASE IsLaptop 
      WHEN '1' THEN 'Yes' 
      WHEN '0' THEN 'No' 
     END AS laptop 

    FROM customer 
    INNER JOIN zlu_cars ON(customer.CarID = zlu_cars.CarID) 
    INNER JOIN zlu_birthmonth ON(customer.BirthMonthID = zlu_birthmonth.BirthMonthID) 
    INNER JOIN zlu_carcolor ON (customer.CarColorID = zlu_carcolor.CarColorID) 
    INNER JOIN zlu_computers ON (customer.ComputerID = zlu_computers.ComputerID) 
    INNER JOIN zlu_race ON(customer.RaceID = zlu_race.RaceID) 
    INNER JOIN zlu_residence ON(customer.ResidenceID = zlu_residence.ResidenceID) 
    order by NewCustomerID"; 


    //Create a PS_Pagination object 
    $countperpage = 10; 
    $pager = new PS_Pagination($conn,$q,$countperpage,6); 

    //The paginate() function returns a mysql result set 
    $rs = $pager->paginate(); 

    echo '<table border="1"> 
       <tr> 
       <th>Customer ID</th> 
       <th>Old Customer ID</th> 
       <th>Customer Name</th> 
       <th>UserName</th> 
       <th>Car</th> 
       <th>Car Color</th> 
       <th>Birth Month</th> 
       <th>Computer Brand</th> 
       <th>Laptop</th> 
       <th>Race</th> 
       <th>Residence</th> 
      </tr>'; 


    while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) { 

    echo '<tr> 
       <td>' . $row['customerid']. '</td> 
       <td>' . $row['oldcustomerid']. '</td> 
       <td>' . $row['Name'].'</td> 
       <td>' . $row['UserName'].'</td> 
       <td>' . $row['cartype'].'</td> 
       <td>' . $row['carcolor'].'</td> 
       <td>' . $row['birthmonth'].'</td> 
       <td>' . $row['computer'].'</td> 
       <td>' . $row['laptop'].'</td> 
       <td>' . $row['race'].'</td> 
       <td>' . $row['residence'].'</td> 
     </tr>'; } // end of while loop 

      echo '</table>'; 


    //Display the full navigation in one go 
    echo "<br><center><font face=verdana size=3 color=blue>"; 
    echo $pager->renderFullNav(); 
    echo "</font></center>"; 

    mysql_free_result($rs); 
    mysql_close($conn); 



/* Or you can display the inidividual links... 
     echo $pager->renderFirst();   //Display the link to first page: First 
     echo $pager->renderPrev();   //Display the link to previous page: << 
     echo $pager->renderNav();   //Display page links: 1 2 3 
     echo $pager->renderNext();   //Display the link to next page: >> 
     echo $pager->renderLast();   //Display the link to last page: Last 
*/ 

?> 

分頁代碼

<?php 
/** 
* PHPSense Pagination Class 
* 
* PHP tutorials and scripts 
* 
* @package  PHPSense 
* @author  Jatinder Singh Thind 
* @copyright  Copyright (c) 2006, Jatinder Singh Thind 
* @link  http://www.phpsense.com 
*/ 

// ------------------------------------------------------------------------ 

class PS_Pagination { 
    var $php_self; 
    var $rows_per_page; //Number of records to display per page 
    var $total_rows; //Total number of rows returned by the query 
    var $links_per_page; //Number of links to display per page 
    var $sql; 
    var $debug = false; 
    var $conn; 
    var $page; 
    var $max_pages; 
    var $offset; 

    /** 
    * Constructor 
    * 
    * @param resource $connection Mysql connection link 
    * @param string $sql SQL query to paginate. Example : SELECT * FROM users 
    * @param integer $rows_per_page Number of records to display per page. Defaults to 10 
    * @param integer $links_per_page Number of links to display per page. Defaults to 5 
    */ 

    function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5) { 
     $this->conn = $connection; 
     $this->sql = $sql; 
     $this->rows_per_page = $rows_per_page; 
     $this->links_per_page = $links_per_page; 
     $this->php_self = htmlspecialchars($_SERVER['PHP_SELF']); 
     if(isset($_GET['page'])) { 
      $this->page = intval($_GET['page']); 
     } 
    } 

    /** 
    * Executes the SQL query and initializes internal variables 
    * 
    * @access public 
    * @return resource 
    */ 
    function paginate() { 
     if(!$this->conn) { 
      if($this->debug) echo "MySQL connection missing<br />"; 
      return false; 
     } 

     $all_rs = @mysql_query($this->sql); 
     if(!$all_rs) { 
      if($this->debug) echo "SQL query failed. Check your query.<br />"; 
      return false; 
     } 
     $this->total_rows = mysql_num_rows($all_rs); 
     @mysql_close($all_rs); 

     $this->max_pages = ceil($this->total_rows/$this->rows_per_page); 
     //Check the page value just in case someone is trying to input an aribitrary value 
     if($this->page > $this->max_pages || $this->page <= 0) { 
      $this->page = 1; 
     } 

     //Calculate Offset 
     $this->offset = $this->rows_per_page * ($this->page-1); 

     //Fetch the required result set 
     $rs = @mysql_query($this->sql." LIMIT {$this->offset}, {$this->rows_per_page}"); 
     if(!$rs) { 
      if($this->debug) echo "Pagination query failed. Check your query.<br />"; 
      return false; 
     } 
     return $rs; 
    } 

    /** 
    * Display the link to the first page 
    * 
    * @access public 
    * @param string $tag Text string to be displayed as the link. Defaults to 'First' 
    * @return string 
    */ 
    function renderFirst($tag='First') { 
     if($this->page == 1) { 
      return $tag; 
     } 
     else { 
      return '<a href="'.$this->php_self.'?menukey=7&page=1">'.$tag.'</a>'; 
     } 
    } 

    /** 
    * Display the link to the last page 
    * 
    * @access public 
    * @param string $tag Text string to be displayed as the link. Defaults to 'Last' 
    * @return string 
    */ 
    function renderLast($tag='Last') { 
     if($this->page == $this->max_pages) { 
      return $tag; 
     } 
     else { 
      return '<a href="'.$this->php_self.'?menukey=7&page='.$this->max_pages.'">'.$tag.'</a>'; 
     } 
    } 

    /** 
    * Display the next link 
    * 
    * @access public 
    * @param string $tag Text string to be displayed as the link. Defaults to '>>' 
    * @return string 
    */ 
    function renderNext($tag=' &gt;&gt;') { 
     if($this->page < $this->max_pages) { 
      return '<a href="'.$this->php_self.'?menukey=7&page='.($this->page+1).'">'.$tag.'</a>'; 
     } 
     else { 
      return $tag; 
     } 
    } 

    /** 
    * Display the previous link 
    * 
    * @access public 
    * @param string $tag Text string to be displayed as the link. Defaults to '<<' 
    * @return string 
    */ 
    function renderPrev($tag='&lt;&lt;') { 
     if($this->page > 1) { 
      return '<a href="'.$this->php_self.'?menukey=7&page='.($this->page-1).'">'.$tag.'</a>'; 
     } 
     else { 
      return $tag; 
     } 
    } 

    /** 
    * Display the page links 
    * 
    * @access public 
    * @return string 
    */ 
    function renderNav() { 
     for($i=1;$i<=$this->max_pages;$i+=$this->links_per_page) { 
      if($this->page >= $i) { 
       $start = $i; 
      } 
     } 

     if($this->max_pages > $this->links_per_page) { 
      $end = $start+$this->links_per_page; 
      if($end > $this->max_pages) $end = $this->max_pages+1; 
     } 
     else { 
      $end = $this->max_pages; 
     } 

     $links = ''; 

     for($i=$start ; $i<$end ; $i++) { 
      if($i == $this->page) { 
       $links .= " $i "; 
      } 
      else { 
       $links .= ' <a href="'.$this->php_self.'?menukey=7&page='.$i.'">'.$i.'</a> '; 
      } 
     } 

     return $links; 
    } 

    /** 
    * Display full pagination navigation 
    * 
    * @access public 
    * @return string 
    */ 
    function renderFullNav() { 
     return $this->renderFirst().'&nbsp;'.$this->renderPrev().'&nbsp;'.$this->renderNav().'&nbsp;'.$this->renderNext().'&nbsp;'.$this->renderLast(); 
    } 

    /** 
    * Set debug mode 
    * 
    * @access public 
    * @param bool $debug Set to TRUE to enable debug messages 
    * @return void 
    */ 
    function setDebug($debug) { 
     $this->debug = $debug; 
    } 
} 
?>