php while 语法与实例
while语句执行的代码块,如果只要在指定条件计算为true。如果条件变得假,在循环
中的语句停止执行和控制传递给语句后面的循环。 while循环的语法如下:
while (condition)
{
code to be executed;
}
在相关的声明,但始终是在封闭(开幕式和闭幕式的大括号)代码块符号告诉PHP代码
清楚哪些行应该通过循环。
while循环是最常用的递增一个列表,没有已知的准则,循环迭代次数。例如:
while (there are still rows to read from a database)
{
read in a row;
move to the next row;
}
让我们来的例子。第一个例子定义了一个循环,开始与i = 0。循环将继续运行,只要
变量i小于或等于10。我将增加1每次循环运行:
<?php
$i=0;
while ($i <= 10) { // Output values from 0 to 10
echo "The number is ".$i."<br />";
$i++;
}
?>
现在,让我们考虑一个更有用的例子,创建下拉几天,几个月或几年清单。您可以使
用此登记表的代码,例如。
<?php
$month_array = array( "January", "February", "March", "April", "May",
"June",
"July", "August", "September", "October", "November",
"December");
echo "<select name="day">";
$i = 1;
while ( $i <= 31 ) {
echo "<option value=".$i.">".$i."</option>";
$i++;
}
echo "</select>";
echo "<select name="month">";
$i = 0;
while ( $i <= 11 ) {
echo "<option value=".$i.">".$month_array[$i]."</option>";
$i++;
}
echo "</select>";
echo "<select name="year">";
$i = 1900;
while ( $i <= 2007 ) {
echo "<option value=".$i.">".$i."</option>";
$i++;
}
echo "</select>";
?>
在做... while语句类似的声明时,除了测试条件在每次迭代结束,而不是在开始。这
意味着你... While循环是保证至少运行一次。在做... While循环的语法如下:
do
{
code to be exected;
}
while (condition);
下面将增加i的值至少一次的例子,它将继续递增,只要它有一个变量i的值小于或等
于10:
<?php教程
$i = 0;
do {
echo "The number is ".$i."<br/>";
$i++;
}
while ($i <= 10);
?>
<?php
class My_Lib_Functions {
# ********************************************
# 对象转数组
# ********************************************
function object2array ($obj) {
foreach ($obj as $k => $v) {
$arr[$k] = $v;
unset($v);
}
return $arr;
}
// ********************************************
// 获取限定区间内的图片宽高
//$mx :图像在页面上显示的宽度
//$my :图像在页面上显示的高度
//此处缩略图按比例缩放的原理为x/y = $mx / $my;
// **********************************************
function getPicXY($picurl, $mx = 0, $my = 0){
if (!$size = @GetImageSize($picurl)) return false; //GetImageSize用来测定任何图象大小并返回图象的尺寸以及文件类型和一个用于普通HTML文件中<IMG>标记中的height/width文本字符串
if ($mx == 0 && $my == 0) return $size; //当$mx与$my参数的值为零时,返回图象的大小
if ($mx * $size[1] / $size[0] > $my){ //判断当$mx乘以图象本身的高度再除以图象的宽度大于在页面上显示的图象宽度时
$newXY[0] = $my * $size[0] / $size[1]; //设置产生新图像的宽度为$mx的值乘以图象本身的宽度再除以图象本身的高度
$newXY[1] = $my; //设置产生的新图像的高度为页面图象显示的高度
}else{ //判断当$mx乘以图象本身的高度再除以图象的宽度不大于在页面上显示的图象宽度时
$newXY[0] = $mx; //设置产生的新图像的宽度为页面图象显示的宽度
$newXY[1] = $mx * $size[1] / $size[0]; //设置产生新图像的宽度为$mx的值乘以图象本身的高度再除以图象本身的宽度
}
if ($size[0] <= $my and $size[1] <= $mx){ //判断当图像本身的宽度小于等于页面上显示图像的宽度且图像本身的高度小于等于页面上显示图像的高度
$newXY[0] = $size[0]; //设置图像新产生的宽度为图像本身的宽度
$newXY[1] = $size[1]; //设置图像新年产的高度为图像本身的高度
}
return $newXY; //返回图像的新比例
}
function getNewImageWH2 ($imageUrl, $maxWidth = 0, $maxHeight = 0, &$newWidth = 0, &$newHeight = 0){
if (!$imageSize = @GetImageSize($imageUrl)) return false;
if ($maxWidth == 0 || $maxHeight == 0) {
$newWidth = $imageSize[0];
$newHeight = $imageSize[1];
} else {
$newWidth = $maxWidth;
$newHeight = $newWidth * $imageSize[1] / $imageSize[0];
if ($newHeight < $maxHeight) {
$newHeight = $maxHeight;
$newWidth = $newHeight * $imageSize[0] / $imageSize[1];
}
$imageSize[0] = $newWidth;
$imageSize[1] = $newHeight;
}
return $imageSize;
}
}
?>
<?php
class My_Lib_page{
//当前页
var $page = 1;
//每页显示条数
var $size = 20;
//总记录数
var $count;
//每栏显示条数
var $lun = 10;
//参数绑订
var $param;
//跳转
var $run;
//name=admin&pwd=2132&sex=1
//$_GET['name'] = admin;
private function setParam(){
$param = $this->param;
unset($param['page']);
if(is_array($param) && !empty($param)){
foreach($param as $key=>$value){
$data[] = $key."/".$value;
}
$this->url = implode("/",$data);
$this->param = $this->url;
$this->url = "/".$this->url;
}else{
if(!empty($param)){
$this->url = $param;
$this->param = $this->url;
$this->url = "/".$this->url;
}
}
}
function pageDate(){
$this->setParam();
$num = ceil($this->lun/2);
$this->page = intval($this->page);
$this->page = $this->page <= 0 ? 1 : $this->page;
$cpage['countPage'] = ceil($this->count/$this->size); //总页数
$cpage['page'] = $this->page > $cpage['countPage'] ? $cpage['countPage']:$this->page ;
//当前页
//显示总数
$cpage['lun'] = $cpage['countPage'] < $this->lun ? $cpage['countPage'] : $this->lun;
//开始页码
$cpage['start'] = $this->page > $num-1 ? $this->page-$num+1 : 1;
$cpage['start'] = ($cpage['start']+$cpage['lun'])>=$cpage['countPage'] ? $cpage['countPage']-$cpage['lun']+1 : $cpage['start'];
//参数显示
$cpage['url'] = $this->url;
//控制跳转
$cpage['run'] = $this->run ? 1 : 0;
$cpage['param'] = is_array($this->param) ? '': "?".$this->param;
return $cpage;
}
}
/*include "../inc/smarty.php";
$cp = new page;
empty($_REQUEST['page']) || $cp->page = $_REQUEST['page'];
$cp->count = 70000;
$cp->param = $_GET;
$cp->size = 100;
$cp->lun = 7;
$cp->run = true;
$data = $cp->pageDate();
$smarty->assign("page",$data);
$smarty->display("cpage.html");
*/
?>
<?php
class My_Lib_pages{
private $each_disNums;//每页显示的条目数
private $nums;//总条目数
var $current_page;//当前被选中的页
private $sub_pages;//每次显示的页数
private $pageNums;//总页数
private $page_array = array();//用来构造分页的数组
private $subPage_link;//每个分页的链接
private $subPage_type;//显示分页的类型
/*
__construct是SubPages的构造函数,用来在创建类的时候自动运行.
@$each_disNums 每页显示的条目数
@nums 总条目数
@current_num 当前被选中的页
@sub_pages 每次显示的页数
@subPage_link 每个分页的链接
@subPage_type 显示分页的类型
当@subPage_type=1的时候为普通分页模式
example: 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
当@subPage_type=2的时候为经典分页样式
example: 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
*/
function __construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_link,$subPage_type){
$this->each_disNums=intval($each_disNums);
$nums = $nums==0 ? 1: $nums;
$this->nums=intval($nums);
$this->pageNums=ceil($nums/$each_disNums);
$this->current_page=intval($current_page);
$this->current_page= $this->current_page<=0 ? 1: $this->current_page;
$this->current_page= $this->current_page > $this->pageNums ? 1 : $this->current_page;
$this->sub_pages=intval($sub_pages);
$this->subPage_link=$subPage_link;
$this->show_SubPages($subPage_type);
//echo $this->pageNums."--".$this->sub_pages;
}
/*
__destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
*/
function __destruct(){
unset($each_disNums);
unset($nums);
unset($current_page);
unset($sub_pages);
unset($pageNums);
unset($page_array);
unset($subPage_link);
unset($subPage_type);
}
/*
show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页
*/
function show_SubPages($subPage_type){
if($subPage_type == 1){
$this->subPageCss1();
}elseif ($subPage_type == 2){
$this->subPageCss2();
}
}
/*
用来给建立分页的数组初始化的函数。
*/
function initArray(){
for($i=0;$i<$this->sub_pages;$i++){
$this->page_array[$i]=$i;
}
return $this->page_array;
}
/*
construct_num_Page该函数使用来构造显示的条目
即使:[1][2][3][4][5][6][7][8][9][10]
*/
function construct_num_Page(){
if($this->pageNums < $this->sub_pages){
$current_array=array();
for($i=0;$i<$this->pageNums;$i++){
$current_array[$i]=$i+1;
}
}else{
$current_array=$this->initArray();
if($this->current_page <= 3){
for($i=0;$i<count($current_array);$i++){
$current_array[$i]=$i+1;
}
}elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
for($i=0;$i<count($current_array);$i++){
$current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
}
}else{
for($i=0;$i<count($current_array);$i++){
$current_array[$i]=$this->current_page-2+$i;
}
}
}
return $current_array;
}
/*
构造普通模式的分页
共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
*/
function subPageCss1(){
$subPageCss1Str="";
$subPageCss1Str.="共".$this->nums."条记录,";
$subPageCss1Str.="每页显示".$this->each_disNums."条,";
$subPageCss1Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
if($this->current_page > 1){
$firstPageUrl=$this->subPage_link."1";
$prewPageUrl=$this->subPage_link.($this->current_page-1);
$subPageCss1Str.="[<a href='$firstPageUrl'>首页</a>] ";
$subPageCss1Str.="[<a href='$prewPageUrl'>上一页</a>] ";
}else {
$subPageCss1Str.="[首页] ";
$subPageCss1Str.="[上一页] ";
}
if($this->current_page < $this->pageNums){
$lastPageUrl=$this->subPage_link.$this->pageNums;
$nextPageUrl=$this->subPage_link.($this->current_page+1);
$subPageCss1Str.=" [<a href='$nextPageUrl'>下一页</a>] ";
$subPageCss1Str.="[<a href='$lastPageUrl'>尾页</a>] ";
}else {
$subPageCss1Str.="[下一页] ";
$subPageCss1Str.="[尾页] ";
}
return $subPageCss1Str;
}
/*
构造经典模式的分页
当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
*/
function subPageCss2(){
$subPageCss2Str="";
$subPageCss2Str.="当前第<span style=color:red;>".$this->current_page."</span>/".$this->pageNums."页 ";
if($this->current_page > 1){
$firstPageUrl=$this->subPage_link."1";
$prewPageUrl=$this->subPage_link.($this->current_page-1);
$subPageCss2Str.="<a href='$firstPageUrl'>首页</a> ";
$subPageCss2Str.="<a href='$prewPageUrl'><<</a>";
}else {
$subPageCss2Str.="<span class=disabled>首页</span>";
$subPageCss2Str.="<span class=disabled><<</span>";
}
$a=$this->construct_num_Page();
for($i=0;$i<count($a);$i++){
$s=$a[$i];
if($s == $this->current_page ){
$subPageCss2Str.="<span style='color:red;font-weight:bold;'>".$s."</span>";
}else{
$url=$this->subPage_link.$s;
$subPageCss2Str.="<span class=current><a href='$url'>".$s."</a></span>";
}
}
if($this->current_page < $this->pageNums){
$lastPageUrl=$this->subPage_link.$this->pageNums;
$nextPageUrl=$this->subPage_link.($this->current_page+1);
$subPageCss2Str.=" <a href='$nextPageUrl'>>></a> ";
$subPageCss2Str.="<a href='$lastPageUrl'>尾页</a> ";
}else {
$subPageCss2Str.="<span class=disabled>>></span>";
$subPageCss2Str.="<span class=disabled>尾页</span>";
}
return $subPageCss2Str;
}
}
?>
相关文章
- 这篇文章主要介绍了Vue 3.0 中 jsx 语法使用,帮助大家更好的理解和使用vue框架,感兴趣的朋友可以了解下...2020-11-13
- 这篇文章主要介绍了C#中while循环语句用法,以实例形式详细分析了while语句的用法,并对return,continue,break的区别做了进一步的分析,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了shell脚本实战-while循环语句,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-09
- 本篇文章主要讲解C语言 基本语法,这里提供简单的示例和代码来详细讲解C语言的基本语法,开始学习C语言的朋友可以看一下,希望能够给你带来帮助...2021-09-18
- 主键与外键的关系,通俗点儿讲,我现在有一个论坛,有两张表,一张是主贴 thread,一张是回帖 reply先说说主键,主键是表里面唯一识别记录的字段,一般是帖子id,体现在访问的时候,例如是 thread.php?id=1 表示我要访问的是帖子...2015-11-24
- 一、数据访问对象 (DAO)YiiDAO 基于 PHP Data Objects (PDO) 构建。它是一个为众多流行的DBMS提供统一数据访问的扩展,这些 DBMS 包括MySQL, PostgreSQL 等等。因此,要使用 Yii DAO,PDO 扩展和特定的 PDO 数据库驱动(例如...2015-11-24
- 这篇文章主要介绍了c#接口使用的实例,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-17
- 这篇文章主要介绍了Python While循环语句实例演示及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-04-30
- C#中方括号[]可用于数组,索引、属性,更重要的是用于外部DLL类库的引用。...2020-06-25
- JavaScript语言的基本语法要求有哪些呐?下面将为大家一一解答: 一、区分大小写 JavaScript语言区分字符大小写,两个字符串相同大小写不同,被认为是不同的字符串。JavaScript语言的关键字也区分大小写,按语法要求应...2015-11-24
- 这篇文章主要介绍了JavaScript的基础语法和数据类型,保姆级的详细教程,万字长文详细的列出了JavaScript的各种语法,建议收藏系列,希望可以有所帮助...2021-09-29
- 这篇文章主要介绍了C#中的IEnumerable简介及简单实现实例,本文讲解了IEnumerable一些知识并给出了一个简单的实现,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C#语法相比其它语言比较独特的地方(一),本文讲解了switch语句可以用来测试string型的对象、多维数组、foreach语句、索引器和Property等内容,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了Vue实例的选项有哪些,文中讲解非常细致,代码帮助大家更好的学习,感兴趣的朋友可以了解下...2020-06-10
- 下面小编就为大家带来一篇javascript学习笔记_浅谈基础语法,类型,变量。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-03
- 在这一章里你将了解到迷人而又让人容易糊涂的套接字(Sockets)。Sockets在PHP中是没有充分利用的功能。今天你将看到产生一个能使用客户端连接的服务器,并在客户端使用sock...2016-11-25
- 这篇文章主要介绍了C#基础语法:Base关键字学习笔记,本文讲解了它的一些基础知识以及测试代码,需要的朋友可以参考下...2020-06-25
php file_exists 函数与 file_exists语法
其实于php file_exists 函数与 file_exists语法我们早就讲过了,下面我们来看看一下关于它的使用方法与实例吧 bool file_exists ( string filename ) 如果由 filen...2016-11-25- 这篇文章主要为大家详细介绍了Vue实例的一些简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-01-23
- 这篇文章主要介绍了利用C++内核对象封装的类,程序只能运行单个实例,可防止多次启动,大家参考使用吧...2020-04-25