吴伟贤のBlog

Feed Rss

存档: ‘php’ 分类

PHP获取IP地址的多种方法

06.04.2009, PHP获取IP地址的多种方法已关闭评论, php, by .

<?php

//获取当前登陆用户IP
function get_client_ip()
{
if ($_SERVER[‘REMOTE_ADDR’]) {
$cip = $_SERVER[‘REMOTE_ADDR’];
} elseif (getenv("REMOTE_ADDR")) {
$cip = getenv("REMOTE_ADDR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$cip = getenv("HTTP_CLIENT_IP");
} else {
$cip = "unknown";
}
return $cip;
}
echo "你的IP地址是:".get_client_ip();

?>

方法二:
echo "
";
?>
error_reporting (E_ERROR | E_WARNING | E_PARSE);
if($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) 

$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];

elseif($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]) 

$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];

elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"]) 

$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"]; 

elseif (getenv("HTTP_X_FORWARDED_FOR")) 

$ip = getenv("HTTP_X_FORWARDED_FOR"); 

elseif (getenv("HTTP_CLIENT_IP")) 

$ip = getenv("HTTP_CLIENT_IP"); 

elseif (getenv("REMOTE_ADDR")) 

$ip = getenv("REMOTE_ADDR"); 

else 

$ip = "Unknown"; 

echo "你的IP地址是:".$ip."
"; 
?> 
方法三(最简单):
echo "
";
?>
$iipp = $_SERVER["REMOTE_ADDR"];
echo $iipp;
echo "
";
?>
方法四:
echo "
";
?>
$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
echo $user_IP."
";
?>
方法五:
echo "
";
?>
function get_real_ip()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’]))
{
$ips = explode (", ", $_SERVER[‘HTTP_X_FORWARDED_FOR’]);
if ($ip)
{
array_unshift($ips, $ip); $ip = FALSE;
}
for ($i = 0; $i < count($ips); $i++)
{
if (!eregi ("^(10|172.16|192.168).", $ips[$i]))
{
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER[‘REMOTE_ADDR’]);
}
echo get_real_ip();
echo "
";
?>
方法六:
echo "
";
?>
if(getenv(‘HTTP_CLIENT_IP’))
{
$onlineip = getenv(‘HTTP_CLIENT_IP’);
}
elseif(getenv(‘HTTP_X_FORWARDED_FOR’))
{
$onlineip = getenv(‘HTTP_X_FORWARDED_FOR’);
}
elseif(getenv(‘REMOTE_ADDR’))
{
$onlineip = getenv(‘REMOTE_ADDR’);
}
else
{
$onlineip = $HTTP_SERVER_VARS[‘REMOTE_ADDR’];
}
echo $onlineip;
echo "
";
?>
方法一:
echo "
";
?>
function GetIP()
{
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
$cip = $_SERVER["HTTP_CLIENT_IP"];
else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if(!empty($_SERVER["REMOTE_ADDR"]))
$cip = $_SERVER["REMOTE_ADDR"];
else
$cip = "无法获取!";
return $cip;
}
echo "
";
?>

了解更多

php实时倒计时

05.21.2009, php实时倒计时已关闭评论, php, by .

<?php
//php的时间是以秒算。js的时间以毫秒算

date_default_timezone_set("Asia/Hong_Kong");//地区

//配置每天的活动时间段
$starttimestr = "09:00:00";
$endtimestr = "13:50:00";

$starttime =  strtotime($starttimestr);
$endtime  =   strtotime($endtimestr);
$nowtime  =   time();
if ($nowtime<$starttime){
die("活动还没开始,活动时间是:{$starttimestr}至{$endtimestr}");
}
$lefttime = $endtime-$nowtime;  //实际剩下的时间(秒)
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP实时倒计时!</title>
<script language="JavaScript">
<!– //
var runtimes = 0;
function GetRTime(){
    var nMS = <?=$lefttime?>*1000-runtimes*1000;
var nH=Math.floor(nMS/(1000*60*60))%24;
var nM=Math.floor(nMS/(1000*60)) % 60;
var nS=Math.floor(nMS/1000) % 60;
time=nH+""+nM+""+nS;
document.getElementById("RemainH").innerHTML=nH;
document.getElementById("RemainM").innerHTML=nM;
document.getElementById("RemainS").innerHTML=nS;
document.getElementById("kkkk").innerHTML=time;
runtimes++;
setTimeout("GetRTime()",1000);
}
window.onload=GetRTime;
// –>
</script>
</head>
<body>
  <h1><strong id="RemainH">XX</strong>:<strong id="RemainM">XX</strong>:<strong id="RemainS">XX</strong></h1>
  <strong id=kkkk></strong>
</body>
</html>

了解更多

php数字转中文(包括了数字转中文,小数转中文,日期转中文)

05.21.2009, php数字转中文(包括了数字转中文,小数转中文,日期转中文)已关闭评论, php, by .

今天代码需要,找了一个数字转中文的函数

 

<?php
echo num_to_gb(12);
echo ‘<br/>’;
echo num_to_gb(12,1,false);
echo ‘<br/>’;
echo num_to_gb(32.45,2);
echo ‘<br/>’;
echo num_to_gb(‘2008-2-18’);
echo ‘<br/>’;

/**
* 将数字转换成中文(阿拉伯数字转换成中文)
* @string $num 序列
* @int $mode 处理模式(1.删除前导零 2.小数部分不删除前导零 3.日期)
* @bool $lower 中文小写
* @int $conv 转换方式 (0.普通方式 1.逐位转换没有十百千万)
* return 中文
*/
function num_to_gb($num,$mode=1,$lower=true,$conv=0) {
   $num_char = array(‘0′,’一’,’二’,’三’,’四’,’五’,’六’,’七’,’八’,’九’,’零’,’壹’,’贰’,’叁’,’肆’,’伍’,’陆’,’柒’,’捌’,’玖’);
   $num_unit = array(”,’十’,’百’,’千’,”,’万’,’亿’,’兆’,”,’拾’,’佰’,’仟’,”,’萬’,’億’,’兆’);
   $date_char = array(‘年’,’月’,’日’);
   $decimal_point = array(‘点’,’點’);
   if ($lower)
    $lo_flag = 0;
   else
    $lo_flag = 1;
   $result = ”;
   switch($mode) {
    case 1:
     preg_match_all("/^0*(\d*)\.?(\d*)/",$num,$numbers);
     break;
    case 2:
     preg_match_all("/(\d*)\.?(\d*)/",$num,$numbers);
     break;
    case 3:
     preg_match_all("/^(\d*)\-(\d*)\-(\d*)$/",$num,$numbers);
     break;
    default:
     return null;
   }
   if (isset($numbers[3][0]) && !empty($numbers[3][0])) {
    // 日期格式
    return num_to_gb($numbers[1][0],1,$lower,1) . $date_char[0] . num_to_gb($numbers[2][0]) . $date_char[1] . num_to_gb($numbers[3][0]) . $date_char[2];
   }
   if (isset($numbers[2][0]) && !empty($numbers[2][0])) {
    // 小数
    $point = $decimal_point[ count($decimal_point)/2 * $lo_flag ];
    return num_to_gb($numbers[1][0],1,$lower) . $point . num_to_gb($numbers[2][0],2,$lower,1);
   }
   if (isset($numbers[1][0]) && !empty($numbers[1][0])) {
    // 整数
    $cur_num = $numbers[1][0];
    $st_char = count($num_char)/2 * $lo_flag;
    $st_unit = count($num_unit)/2 * $lo_flag;
    $out = array();
    switch ($conv) {
     // 日期方式
     case 1:
      for($i=0;$i<strlen($cur_num);$i++)
       $out[$i] = $num_char[ $st_char+$cur_num[$i] ];
      break;
     default:
      $cur_num_s = strrev($cur_num);
      for($i=0;$i < strlen($cur_num_s);$i++) {
       if ($i%4==1 && $cur_num>=10 && $cur_num<20) //去掉 ‘一’十一中的’一’
        $out[$i] = ”;
       else
        $out[$i] = $num_char[ $st_char+$cur_num_s[$i] ];
       $out[$i] .= $cur_num_s[$i] != ‘0’ ? $num_unit[ $st_unit+$i%4 ] : ”;
       $pre_num = $i>0 ? $cur_num_s[$i-1] : 0;
       if($cur_num_s[$i]+$pre_num == 0)
        $out[$i] = ”;
       if($i%4 == 0)
        $out[$i] .= $num_unit[ $st_unit+4+floor($i/4) ];
      }
      $out = array_reverse($out);
    }
    return join(”,$out);
   }
   return null;
}
?>

了解更多

php中wml页面自动跳转

05.12.2009, php中wml页面自动跳转已关闭评论, php, by .

function redirect($newURL)
{
ob_clean();
echo "<?xml version=\"1.0\" encoding=\"uft-8\"?>\t\n";
echo "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.xml\">\t\n";
echo "<wml>\t\n";
echo "<card ontimer=’$newURL’>\t\n";
echo "<timer value=’1′ />";
   echo "</card>\t\n</wml>\t\n";
   exit();
}

了解更多

md5 16位转32位的转化思路

04.24.2009, md5 16位转32位的转化思路已关闭评论, php, by .
  • 1、会员32位加密 与 16位加密 共享密码字段存储。
  • 2、当会员登陆时,分别判断是否与 16位 或 32 位 相等。如果两者其一相等,则登陆成功。
  • 3、如果是16位相等的,则登陆成功的同时对密码重新32位加密写入数据库
  • 4、等待所有会员密码为16位转换完成,去掉 2 所加入的 16位 相等的判断代码。
  • 5 按以上步骤操作完全可以达到要求。不需要会员重新注册。
  •  

    php 中

    1. 16位的MD5加密用substr函数截取了字符得到实现….
    2. 比如 "admin" MD5.
    3. 32位: 21232f297a57a5a743894a0e4a801fc3
    4. 16位: ________7a57a5a743894a0e________
    5. 可以看出 其实 16位和32的小写MD5编码对比下来 
    6. 16位编码其实就是把32位编码的前后分别去掉8个字符,因此可以实现算出32位密码
    7. 通过substr()函数得到16位编码
    <?php
    echo substr(md5("admin"),8,16); // 16位MD5加密
    echo "<hr>";
    echo md5("admin"); // 32位MD5加密
    ?>

    了解更多

    php字符串中提取数字

    03.09.2009, php字符串中提取数字已关闭评论, php, by .

    如:78.34万元

    结果取得
    数字部分78.34 (数字为任意)
    字符部分 万元  (字符为可变)

    请问在php中如何实现?谢谢

     

    方法一:

    需要数字直接转就可以了,会自动截取到第一个不是数字的字符为止,但是前提是第一个字符是数字开头的
    $foo = "78.34万元" + 0;          // $foo is float (78.34)
    然后你再算下数字的长度,取原串后面的内容就可以了

    了解更多

    PHP导入导出Excel方法小结

    02.19.2009, PHP导入导出Excel方法小结已关闭评论, php, by .

    最近因项目需要,需要开发一个模块,把系统中的一些数据导出成Excel,修改后再导回系统。就趁机对这个研究了一番,下面进行一些总结。
    基本上导出的文件分为两种:
    1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已。修改这种文件后再保存,通常会提示你是否要转换成Excel文件。
    优点:简单。
    缺点:难以生成格式,如果用来导入需要自己分别编写相应的程序。
    2:Excel格式,与类Excel相对应,这种方法生成的文件更接近于真正的Excel格式。

    如果导出中文时出现乱码,可以尝试将字符串转换成gb2312,例如下面就把$yourStr从utf-8转换成了gb2312:
    $yourStr = mb_convert_encoding("gb2312", "UTF-8", $yourStr);

    下面详细列举几种方法。
    一、PHP导出Excel

    1:第一推荐无比风骚的PHPExcel,官方网站: http://www.codeplex.com/PHPExcel
    导入导出都成,可以导出office2007格式,同时兼容2003。
    下载下来的包中有文档和例子,大家可以自行研究。
    抄段例子出来:
    <?php
    /**
    * PHPExcel
    *
    * Copyright (C) 2006 – 2007 PHPExcel
    *
    * This library is free software; you can redistribute it and/or
    * modify it under the terms of the GNU Lesser General Public
    * License as published by the Free Software Foundation; either
    * version 2.1 of the License, or (at your option) any later version.
    *
    * This library is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this library; if not, write to the Free Software
    * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    *
    * @category   PHPExcel
    * @package    PHPExcel
    * @copyright Copyright (c) 2006 – 2007 PHPExcel ( http://www.codeplex.com/PHPExcel)
    * @license    http://www.gnu.org/licenses/lgpl.txt    LGPL
    * @version    1.5.0, 2007-10-23
    */

    /** Error reporting */
    error_reporting(E_ALL);

    /** Include path **/
    set_include_path(get_include_path() . PATH_SEPARATOR . ‘../Classes/’);

    /** PHPExcel */
    include ‘PHPExcel.php’;

    /** PHPExcel_Writer_Excel2007 */
    include ‘PHPExcel/Writer/Excel2007.php’;

    // Create new PHPExcel object
    echo date(‘H:i:s’) . " Create new PHPExcel object\n";
    $objPHPExcel = new PHPExcel();

    // Set properties
    echo date(‘H:i:s’) . " Set properties\n";
    $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
    $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
    $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
    $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
    $objPHPExcel->getProperties()->setDescrīption("Test document for Office 2007 XLSX, generated using PHP classes.");
    $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
    $objPHPExcel->getProperties()->setCategory("Test result file");

    // Add some data
    echo date(‘H:i:s’) . " Add some data\n";
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->setCellValue(‘A1’, ‘Hello’);
    $objPHPExcel->getActiveSheet()->setCellValue(‘B2’, ‘world!’);
    $objPHPExcel->getActiveSheet()->setCellValue(‘C1’, ‘Hello’);
    $objPHPExcel->getActiveSheet()->setCellValue(‘D2’, ‘world!’);

    // Rename sheet
    echo date(‘H:i:s’) . " Rename sheet\n";
    $objPHPExcel->getActiveSheet()->setTitle(‘Simple’);

    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);

          
    // Save Excel 2007 file
    echo date(‘H:i:s’) . " Write to Excel2007 format\n";
    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    $objWriter->save(str_replace(‘.php’, ‘.xlsx’, __FILE__));

    // Echo done
    echo date(‘H:i:s’) . " Done writing file.\r\n";

    2、使用pear的Spreadsheet_Excel_Writer
    下载地址: http://pear.php.net/package/Spreadsheet_Excel_Writer
    此类依赖于OLE,下载地址:http://pear.php.net/package/OLE
    需要注意的是导出的Excel文件格式比较老,修改后保存会提示是否转换成更新的格式。
    不过可以设定格式,很强大。
    <?php
    require_once ‘Spreadsheet/Excel/Writer.php’;

    // Creating a workbook
    $workbook = new Spreadsheet_Excel_Writer();
       
    // sending HTTP headers
    $workbook->send(‘test.xls’);
       
    // Creating a worksheet
    $worksheet =& $workbook->addWorksheet(‘My first worksheet’);
       
    // The actual data
    $worksheet->write(0, 0, ‘Name’);
    $worksheet->write(0, 1, ‘Age’);
    $worksheet->write(1, 0, ‘John Smith’);
    $worksheet->write(1, 1, 30);
    $worksheet->write(2, 0, ‘Johann Schmidt’);
    $worksheet->write(2, 1, 31);
    $worksheet->write(3, 0, ‘Juan Herrera’);
    $worksheet->write(3, 1, 32);
       
    // Let’s send the file
    $workbook->close();
    ?>

    3:利用smarty,生成符合Excel规范的XML或HTML文件
    支持格式,非常完美的导出方案。不过导出来的的本质上还是XML文件,如果用来导入就需要另外处理了。
    详细内容请见rardge大侠的帖子:http://bbs.chinaunix.net/viewthread.php?tid=745757

    需要注意的是如果导出的表格行数不确定时,最好在模板中把"ss:ExpandedColumnCount="5" ss:ExpandedRowCount="21""之类的东西删掉。

    4、利用pack函数打印出模拟Excel格式的断句符号,这种更接近于Excel标准格式,用office2003修改后保存,还不会弹出提示,推荐用这种方法。
    缺点是无格式。
    <?php
    // Send Header
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=test.xls ");
    header("Content-Transfer-Encoding: binary ");
    // XLS Data Cell

    xlsBOF();
    xlsWriteLabel(1,0,"My excel line one");
    xlsWriteLabel(2,0,"My excel line two : ");
    xlsWriteLabel(2,1,"Hello everybody");

    xlsEOF();

    function xlsBOF() {
        echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
        return;
    }
    function xlsEOF() {
        echo pack("ss", 0x0A, 0x00);
        return;
    }
    function xlsWriteNumber($Row, $Col, $Value) {
        echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
        echo pack("d", $Value);
      &n
    bsp; return;
    }
    function xlsWriteLabel($Row, $Col, $Value ) {
        $L = strlen($Value);
        echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
        echo $Value;
        return;
    }
    ?>
    不过笔者在64位linux系统中使用时失败了,断句符号全部变成了乱码。

    5、使用制表符、换行符的方法
    制表符"\t"用户分割同一行中的列,换行符"\t\n"可以开启下一行。
    <?php
    header("Content-Type: application/vnd.ms-execl");
    header("Content-Disposition: attachment; filename=myExcel.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    /*first line*/
    echo "hello"."\t";
    echo "world"."\t";
    echo "\t\n";

    /*start of second line*/
    echo "this is second line"."\t";
    echo "Hi,pretty girl"."\t";
    echo "\t\n";
    ?>

    6、使用com
    如果你的PHP可以开启com模块,就可以用它来导出Excel文件
    <?PHP
    $filename = "c:/spreadhseet/test.xls";
    $sheet1 = 1;
    $sheet2 = "sheet2";
    $excel_app = new COM("Excel.application") or Die ("Did not connect");
    print "Application name: {$excel_app->Application->value}\n" ;
    print "Loaded version: {$excel_app->Application->version}\n";
    $Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");
    $Worksheet = $Workbook->Worksheets($sheet1);
    $Worksheet->activate;
    $excel_cell = $Worksheet->Range("C4");
    $excel_cell->activate;
    $excel_result = $excel_cell->value;
    print "$excel_result\n";
    $Worksheet = $Workbook->Worksheets($sheet2);
    $Worksheet->activate;
    $excel_cell = $Worksheet->Range("C4");
    $excel_cell->activate;
    $excel_result = $excel_cell->value;
    print "$excel_result\n";
    #To close all instances of excel:
    $Workbook->Close;
    unset($Worksheet);
    unset($Workbook);
    $excel_app->Workbooks->Close();
    $excel_app->Quit();
    unset($excel_app);
    ?>
    一个更好的例子: http://blog.chinaunix.net/u/16928/showart_387171.html

    一、PHP导入Excel

    1:还是用PHPExcel,官方网站: http://www.codeplex.com/PHPExcel。

    2:使用PHP-ExcelReader,下载地址: http://sourceforge.net/projects/phpexcelreader
    举例:
    <?php
    require_once ‘Excel/reader.php’;

    // ExcelFile($filename, $encoding);
    $data = new Spreadsheet_Excel_Reader();

    // Set output Encoding.
    $data->setOutputEncoding(‘utf8’);

    $data->read(‘ jxlrwtest.xls’);

    error_reporting(E_ALL ^ E_NOTICE);

    for ($i = 1; $i <= $data->sheets[0][‘numRows’]; $i++) {
        for ($j = 1; $j <= $data->sheets[0][‘numCols’]; $j++) {
            echo "\"".$data->sheets[0][‘cells’][$i][$j]."\",";
        }
        echo "\n";
    }

    ?>
     

    了解更多

    PHP导出csv excel 格式的文件

    02.10.2009, PHP导出csv excel 格式的文件已关闭评论, php, by .

    CVS的例子

    <?
    header("Content-type:application/vnd.ms-excel"
    );
    header("Content-Disposition:filename=test.csv"
    );
    echo
    "test1,"
    ;
    echo
    "test2,"
    ;
    echo
    "test1,"
    ;
    echo
    "test2,"
    ;
    echo
    "test1,"
    ;
    echo
    "test2,"
    ;
    echo
    "test1,"
    ;
    echo
    "test2,"
    ;
    echo
    "test1,"
    ;
    echo
    "test2,"
    ;
    echo
    "test1,"
    ;
    echo
    "test2\t\n"
    ;
    ?>

    EXCEL的例子

    <?
    header
    ("Content-type:application/vnd.ms-excel"
    );
    header("Content-Disposition:filename=test.xls"
    );
    echo
    "test1\t"
    ;
    echo
    "test2\t\n"
    ;
    echo
    "test1\t"
    ;
    echo
    "test2\t\n"
    ;
    echo
    "test1\t"
    ;
    echo
    "test2\t\n"
    ;
    echo
    "test1\t"
    ;
    echo
    "test2\t\n"
    ;
    echo
    "test1\t"
    ;
    echo
    "test2\t\n"
    ;
    echo
    "test1\t"
    ;
    echo
    "test2\t\n"
    ;
    ?>

    了解更多

    php取整函数

    11.12.2008, php取整函数已关闭评论, php, by .

    经常用到的PHP取整函数,主要是:ceil,floor,round,intval

    ceil — 进一法取整
    说明
    float ceil ( float value )
    返回不小于 value 的下一个整数,value 如果有小数部分则进一位。ceil() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。

    例子 1. ceil() 例子

    <?php
    echo ceil(4.3);     // 5
    echo ceil(9.999);     // 10
    ?>

    floor — 舍去法取整
    说明
    float floor ( float value )
    返回不大于 value 的下一个整数,将 value 的小数部分舍去取整。floor() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。

    了解更多

    php分页类

    09.16.2008, php分页类已关闭评论, php, by .

    <?php
    //————————-些为PHP网页程序序分页类————————–/
    //说明部分:外部可访问函数:
    //                     【GetPerPage】                            整型                     返回当前页
    //                     【GetTotalPage】                            整型                     返回总页数
    //                     【GetStartEndInfo】                     数组                     返回首尾号
    //                     【GetStart】                                   整型                     返回查询语句开始取数
    //                     【GetPerNextInfo】                     数组                     返回上下页数字
    //                     【GetPerNextMoreInfo】              数组                     返回上几页下几页数字
    //                     【GetPerNext】                            数组                     返回封装的上下页
    //                     【GetPerNextMore】                     数组                     返回封装的上几页下几页
    //                     【GetPageHeader】                     字符串              返回封装分页头信息
    //                     【GetPageBody】                            字符串              返回封装的分页主体信息
    //                     【GetPageEnd】                            字符串              返回封装的分页尾部信息
    //                     【DisplayPageInfo】                     字符串              返回封装的完整分页信息
    //使用说明:
    //       在所使用的文件中Include()本文件,使用New操作符新创建一个本对象。新创建对象时需设置构造函数所需参数
    //  参数说明如下构造函数说明处!
          
    class PageList
    {
           //————————-定义类中用到的全局变量———————-/
           var $PerPage;                            //当前页数
           var $PerLimit;                            //当前每页显示条数
           var $PerPageLimit;                     //当前每页显示页数
           var $TotalNums;                            //当前分页中的总条数
           var $TotalPage;                            //当前分页中总页数
           var $PageUrl;                            //定义当前网页路径
           var $PageStart;                            // 定义当前开始末ID
           var $PageEnd;                            //定义当前结束ID
           var $PageStyle;                            //定主当前分页显示样式
           var $PageHeader;                     //定义显示头部分
           var $PageBody;&nbsp
    ;                           //定义显示主体部分
           var $PageBottom;                     //定义显示尾部分

    //———————————–初始化所有固定变量——————————-/
                  //入口参数:$pageParameter[0]:信息总条数
                         //$pageParameter[1]:每页显示条数
                         //$pageParameter[2]:每页显示页数
                         //$styleParameter[0];动静态风格,true为动态 false为静态 no为不显示
                         //$styleParameter[1];前几页后几页,上一页下一页风格.true为前几页 false为上一页 no为不显示
                         //$styleParameter[2];设置跳转函数true为下拉 false为submit加input no为不显示
                         //$styleParameter[3];设置是否显示主体部分

           //在此说明:如果设置为动态的、上一页下一页风格则为中部动态。如果设置为静态则只能为上几页和下几页
           //           如果设置为动态的、上几页下几页风格则为步进动态

           function  SetVar($pageParameter=array(1,1,1),$styleParameter=array("false","true","true","true")){
                  $this->TotalNums=$pageParameter[0];
                  $this->PerLimit=$pageParameter[1];
                  $this->PerPageLimit=$pageParameter[2];
                  $this->PageStyle=$styleParameter;
                  $this->PerPage=$_GET[page];
                  $this->SetDefaultStyle();
                  $this->SetPageUrl();
                  $this->SetToTalPage();
                  $this->SetPerPage();
                  $this->SetStaticDynamic();
                  $this->SetDisplayPageInfo();
           }

    了解更多