Home php教程 php手册 wg888 de分页类终结者

wg888 de分页类终结者

Jun 21, 2016 am 09:06 AM
gt nbsp this

分页|终结者


/*
------------------------------------------------------------------------------------
类名:Lwgpagenum
说明:PHP+MySQL分页类
作者:龙卫国
网络user:lwg888
邮箱:lwg888@163.com
使用、修改、传播请保留作者信息
------------------------------------------------------------------------------------
*/


require_once(dirname(__FILE__)."/Lwgdb.inc.php");
//Lwgdb.inc.php是数据库连接与sql语句执行类

class Lwgpageturn {
    //----------可以设置值的变量----------------------------------------
    var $maxnum;//每页显示数 
    var $maxnum_max_size=100;  //每页最多显示数,用来规定$maxnum不能超过$maxnum_max_size 
    var $sql;//sql语句
    var $navchar=array('[|]','[>>|]','[>]');
                 //导航条的显示字符,值可以自定义,如一个img标签 
                 //$navchar[0]表示第一页,$navchar[1]表示前一页,$navchar[2]表示后一页,$navchar[3]表示最后页,$navchar[4]表示前n页,$navchar[5]表示后n页 
    var $key;//如果一个页面中有多个分页时作为区别标记
    var $debug=true;//是否显示调试信息
    
    //----------用来获取值的变量---------------------------------------
    var $totalnum;//总记录数
    var $totalpage;//总页数
    var $startnum;//本页的第一条在总数中的序数
    var $endnum;//本页的最后一条在总数中的序数  
    var $pagenum;//本页在总页数中的序数
    var $field;//结果记录的集合
    var $id;//每条记录的序号
    var $linkhead;//链接指定的url及要传递的相关参数
    var $err;//记录最后一条错误信息
    
    //构造函数。
    //参数$maxnum用来指定每页显示多少条记录,如果不指定$maxnum,表示全部显示而不用分页
    //如果同一个页面中有两个以上的分页,参数$key作为区分标记,否则不用指定
    //使用方法:
    //       $obj=new Lwgpagenum('10');
    //或:   $obj=new Lwgpagenum();
    //       $obj->maxnum="10";
    //       $obj->key="1";
    function Lwgpageturn($maxnum="",$maxnum_max_size='',$key=""){
        $this->maxnum=$maxnum;
        if ($maxnum_max_size!="")$this->maxnum_max_size=$maxnum_max_size;
        $this->key=$key;
    }
    
    //通过run方法运行sql并取得相关信息
    //$sql参数为有效的sql语句。可以通过$obj->sql=""来指定
    //$db参数为数据库连接ID
    //使用方法:
    //$sql="select * from table";
    //$db=mysql_connect('host','user','pass');
    //$obj->run($sql,$db);
    //或:
    //$obj->sql="select * from table";
    //$obj->run();
    function run($sql='',$db=''){
        if ($sql!="")$this->sql=$sql;
        if ($this->sql=="") return $this->output("错误:未给出sql查询语句!");
        if ($this->maxnummaxnum_max_sizeoutput("错误:maxnum、maxnum_max_size都不能小于0!");
        if ($db=="")$db=new Lwgdb();
        //如果没有指定参数$db,则用Lwgdb类与数据库建立连接

        if (empty($this->maxnum)){
            //如果初始maxnum为空,则显示全部记录而不用分页
            $result = $db->query($this->sql);//执行sql语句
            $this->totalnum = mysql_num_rows($result);//取得总记录数
            
            if ($this->maxnum_max_size>0 && $this->totalnum>$this->maxnum_max_size)return $this->output("错误:记录数太多,请使用分页!"); 

            $this->startnum=0;//第一条记录数为0
            $this->endnum=$this->totalnum;//最后一条记录数与总记录数相同
        }
        else {
            $ifpost=false; 
            //是否有$_POST变量,如果有的话,则在翻页时只传递其值,其它的一律省略  
            if (sizeof($_POST)>0){
                $formlink = "";
                $ifpost=true;
                foreach ($_POST as $key => $value) {
                    //循环分析出$_POST变量的键值
                    if (!empty($value)) $formlink .=$key."=".rawurlencode($value)."&";
                }
                $querystring=$formlink;//将$_POST变量的键值作为翻页时传递的参数 
            }
            else {
                //如果没有$_POST变量,则将$_GET变量分析后作为翻页时传递的参数  
                $qs=explode("totalnum".$this->key,$_SERVER['QUERY_STRING']);
                if ($qs[0]!="")$querystring=(substr($qs[0],-1)=="&")?$qs[0]:$qs[0]."&"; 
                //扔掉totalnum及其以后的参数,因为要付新的值 
            }

            if (isset($_GET["totalnum".$this->key]) && $_GET["totalnum".$this->key]>0 && !$ifpost)$this->totalnum = sprintf('%d',$_GET["totalnum".$this->key]);
            //如果有$_POST变量,则不再传递旧的totalnum参数
            //否则如果在$_GET中有totalnum,则直接传递它,而不用重新计算
            else {
                $all_result = $db->query($sql);
                $this->totalnum = mysql_num_rows($all_result);
                //如果$totalnum            }
            
            if (isset($_GET["maxnum".$this->key]) && $_GET["maxnum".$this->key]>0)$this->maxnum = sprintf('%d',$_GET["maxnum".$this->key]); 
            //如果通过get接收到$maxnum,则使用之,这样便能在客户端指定要显示的记录数 
            if ($this->maxnum_max_size>0 && $this->maxnum>$this->maxnum_max_size)$this->maxnum=$this->maxnum_max_size;
            //如果$maxnum        
            if ($this->maxnum==$this->totalnum){  
                //虽然前面说maxnum的初始值为空时也是全部显示,但没有任何统计与导航, 适用于如首页的显示前n条记录
                $this->totalpage=1;
                $this->startnum=0;
                $this->endnum=$this->totalnum;
                $result=(empty($all_result))?$db->query($sql):$all_result;
                //$result是用来显示记录的数据库查询id;
                //如果前面计算总数时已有id,则直接使用前面的
            }
            else {
                if (!empty($all_result))mysql_free_result($all_result);
          
                $this->totalpage=ceil($this->totalnum/$this->maxnum);

                $this->pagenum =(isset($_GET["pagenum".$this->key]) && $_GET["pagenum".$this->key]>0 && !$ifpost)?sprintf('%d',$_GET["pagenum".$this->key]):1;
                //如果没有$_POST变量,并且$_GET变量中有发pagenum参数,则使用之,否则,表示页数的pagenum为1
                if ($this->pagenum>$this->totalpage)$this->pagenum=$this->totalpage;

                $this->startnum = max(($this->pagenum-1) * $this->maxnum,0);
                $this->endnum=min($this->startnum+$this->maxnum,$this->totalnum);
                //本页显示数为本页的最后一条在总数中的序数减去本页的第一条在总数中的序数

                $limitstart=0; 
                // 如果sql语句中有limit,则重新设置limit的参数
                //重设limit的第一个参数为 $limitstart=0  
                if (eregi("limit (-?[0-9]+) *,? *(-?[0-9]*)",$sql,$regs)){
                   if (!empty($regs[2])) $limitstart=$regs[1];
                    //$regs[1]为limit的第一个参数;$regs[2]为limit的第二个参数
                    //如果存在第二个参数,则让$limitstart=第一个参数 
                    //此时第二个参数已没有用,因为在计算总数时已发挥作用  
                    $sql=eregi_replace("limit (-?[0-9]+) *,? *(-?[0-9]*)","",$sql);
                    //去掉limit,因为后面要新建limit 
                }
                $limitstart+=$this->startnum;

                $query_limit = sprintf("%s LIMIT %d, %d", $sql, $limitstart,$this->maxnum);
                //重设limit,使结果为本页需要显示的记录
                $result = $db->query($query_limit);
            }
        
            $querystring.="totalnum".$this->key."=".$this->totalnum;
            if (isset($_GET["maxnum".$this->key]))$querystring.="&maxnum".$this->key."=".$this->maxnum;
            $this->linkhead=$_SERVER['PHP_SELF']."?".$querystring;
            //将需要传递的参数加上totalnum和maxnum,然后加在url后
        }
        $i=0;
        while($myrow = mysql_fetch_array($result)){
            $this->field[$i]=$myrow;
            //用两维数组返回要显示的记录
            $this->id[$i]=$this->startnum+$i+1;
            //每条记录的序号
            $i++;
        }
      
        mysql_free_result($result);
    }
    
    //显示如"共14页27条"
    //使用方法:echo $obj->total()
    function total(){
        if ($this->maxnum=="")return;
        return "共".$this->totalpage."页".$this->totalnum."条";
    }
    
    //显示如"本页从第9条到第10条"
    //使用方法:echo $obj->fromto()
    function fromto(){
        if ($this->maxnum=="")return;
        $startnum=$this->startnum+1;
        if ($this->totalnum==0)$startnum=0;
        return "本页从第".$startnum."条到第".$this->endnum."条";
    }
    
    //navbar方法显示页数导航条
    //$num_size表示多少个导航数字,如$num_size=5则显示" 1 2 3 4 5 "
    //$num_style为数字导航条的风格,
    //当$num_style的值为1时将数字分组,显示如"前5页 前1页 1 2 3 4 5 后一页 后5页";  
    //当$num_style的值为非1时,显示如" 1 2 3 4 5 …";  
    //$nolink_show没有链接的导航字符是否显示,true显示,false不显示
    //$nolink_color没有链接的导航字符显示的颜色 
    //使用方法:echo $obj->navbar(10,2,false,"#ffff00")
    function navbar($num_size=0,$num_style=1,$nolink_show=false,$nolink_color="#ff0000"){
        if ($this->totalpage        
        if ($num_size>0){
            if ($num_style==1){
                $thisunit=ceil($this->pagenum/$num_size);//取得本页所有的组 
                $preunit=($thisunit>1)?($thisunit-1):"";//取得上页所有的组 
                $nextunit=($thisunittotalpage/$num_size))?($thisunit+1):"";//取得下页所有的组
                $startpage=($thisunit-1)*$num_size+1;//取得本组的开始页 
                $endpage=min($thisunit*$num_size,$this->totalpage);//取得本组的最后页 
                if ($preunit!=""){
                    $str_preunit=" linkhead."&pagenum".$this->key."=".(($thisunit-2)*$num_size+1)."\" title=\"前".$num_size."页\">".$this->navchar[4]." ";
                }
                if ($nextunit!=""){
                    $str_nextunit=" linkhead."&pagenum".$this->key."=".($thisunit*$num_size+1)."\" title=\"后".$num_size."页\">".$this->navchar[5]."";
                }
            }
            else {
                $tmpnum=ceil($num_size/2); 
                $startpage=max(min($this->pagenum-$tmpnum,$this->totalpage-$num_size+1),1);
                $endpage=min($startpage+$num_size-1,$this->totalpage);
                if ($startpage>1)$str_frontell=" … ";
                if ($endpagetotalpage)$str_backell=" … ";
            }
            $str_num="";
            for ($i=$startpage;$i                if ($i==$this->pagenum)$str_num.=" ".$i." ";
                else $str_num.= " linkhead."&pagenum".$this->key."=".$i."\" title=\"第".$i."页\">".$i." ";
            }
        }

        if ($this->pagenum > 1){
            $str_first=" linkhead."&pagenum".$this->key."=1\" title=\"第一页\">".$this->navchar[0]." ";
            $str_pre=" linkhead."&pagenum".$this->key."=".($this->pagenum-1)."\" title=\"前一页\">".$this->navchar[1]." ";
        }
        else if ($nolink_show){
            $str_first=" ".$this->navchar[0]." ";
            $str_pre=" ".$this->navchar[1]." ";
        }
        if ($this->pagenumtotalpage){
            $str_next= " linkhead."&pagenum".$this->key."=".($this->pagenum+1)."\" title=\"后一页\">".$this->navchar[2]." ";
            $str_last= " linkhead."&pagenum".$this->key."=".$this->totalpage."\" title=\"最后页\">".$this->navchar[3]."  ";
        }
        else if ($nolink_show){
            $str_next=" ".$this->navchar[2]." ";
            $str_last=" ".$this->navchar[3]." ";
        }
        return $str_first.$str_preunit.$str_pre.$str_frontell.$str_num.$str_backell.$str_next.$str_nextunit.$str_last;
    }

    //用下拉列表显示如"到第n页共m页"
    //使用方法:echo $obj->pagejump()
    function pagejump(){
        if ($this->totalpage        
        $options=array();
        for ($i=1;$itotalpage;$i++)$options[$i]=$i;
        return "到第".$this->droplist("pagenum".$this->key,$options)."页/共".$this->totalpage."页";
    }
    
    //用下拉列表显示如"每页显示n条 "
    //使用方法:echo $obj->maxnum()
    function maxnum(){
        if ($this->maxnum=="")return;
        $options=array('1'=>'1','2'=>'2','3'=>'3','4'=>'4','6'=>'6','8'=>'8','9'=>'9','10'=>'10','20'=>'20','30'=>'30','50'=>'50','100'=>'100','全部'=>$this->totalnum);
        $str="";
        return "每页显示".$this->droplist("maxnum".$this->key,$options)."条";
    }
    
    //droplist()用来生成下拉选单
    function droplist($name,$options,$class=""){
        $write="";
        $write.="        if ($class!="")$write.="class='".$class."' ";
        $write.=">";
        $tmplinkhead=eregi_replace("&maxnum".$this->key."=[0-9]*","",$this->linkhead);
        $preval=0;
        while ( list( $key, $val ) = each( $options ) ) {
          if ($name=="maxnum".$this->key){
            if ($val!=$this->totalnum){
              $pagenum=ceil($this->pagenum*$this->maxnum/$val);
              while (($pagenum-1)*$val>$this->startnum)$pagenum-=1;
              //由于每页显示记录数改变了,所以要重新计算$pagenum
              $linkhead=$tmplinkhead."&pagenum".$this->key."=".max($pagenum,1);
            }
            else $linkhead=$this->linkhead;
            
            if ($this->maxnum_max_size>0 && $this->totalnum>$this->maxnum_max_size){
                if ($val>=$this->maxnum_max_size){
                    $write.="";
                    //创建值为$this->maxnum的选单
                    break;
                }
               //如果总数大于$this->maxnum_max_size,则不显示"all"以及大于$this->maxnum_max_size的选项
            }
            else if ($prevalmaxnum && $val>$this->maxnum)$write.="";
            //当$maxnum的值不在选单中时则创建它
            
            $write.="            if ($this->maxnum==$val)$write.=" selected";
            $preval=$val;
          }
          else if ($name=="pagenum".$this->key){
            $write.="            if ($this->pagenum==$val)$write.=" selected";
          }
          $write.=">".$key."";
        }
        $write.="";
        return $write;
    }
    
    //输出错误信息
    function output($msg){
        if ($msg!="")$this->err=$msg;
        if ($this->debug)echo "";
        return false;
    }
}

/*---------使用方法----------------------------------------------
$sql="……";
$obj=new lwg_pageturn(20); 
$obj->run($sql); 
//下面单列显示记录示例
for ($i=0;$iendnum-$obj->startnum;$++){
    echo $obj->id[$i];//显示序号
    echo $obj->field[$i]['name']."
";//显示字段名为name的第$i条记录
}
//下面多列显示记录示例
echo "

";
$colnum=2;//表示分两列
$totalraw=ceil(($obj->endnum-$obj->startnum)/$colnum);
for ($x=0;$x    echo "";
    $startsell=$colnum*$x;
    $endsell=min($x*$colnum+$colnum,$obj->endnum-$obj->startnum);
    for ($i=$startsell;$i        echo "";
    }
    echo "";
}
echo "
";
        echo $obj->id[$i];//显示序号
        echo $obj->field[$i]['name']."
";//显示字段名为name的第$i条记录
        echo "
";
-----------------------------------------------------------------
*/
?>



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

See all articles