需要href / case开关帮助

时间:2012-02-22 10:17:29

标签: php paginate

我需要找到一种方法来获得一个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);
?>

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

网页代码

                                             IT-5334:PHP和MySQL           - &GT;          

<?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;
    }
}
?>
相关问题