网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

php遍历数据库对象 php循环读取数据库表数据

在PHP中遍历对象用什么?

其实百度一下就知道

创新互联建站-专业网站定制、快速模板网站建设、高性价比石棉网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式石棉网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖石棉地区。费用合理售后完善,10年实体公司更值得信赖。

我们知道,php中,foreach可以很方便地对可迭代结构(例如数组,再如对象)进行迭代操作:

[php] view plaincopy

foreach( $array as $elem){

var_dump($elem);

}

[php] view plaincopy

foreach($obj as $key=$value){

echo "$key=$value".PHP_EOL;

}

因而我们想:如果对于一个实例化对象,对其进行foreach操作,会发生什么事情呢?

首先我们定义的基础类为:

[php] view plaincopy

Class Test{

/* one public variable */

public $a;

public $b;

/* one private variable */

private $c;

public function __construct(){

$this-a = "public";

$this-b = "public";

$this-c = "private";

}

public function traverseInside(){

foreach($this as $key=$value){

echo $key."=".$value.EOL;

}

}

}

然后我们实例化该类,对其进行迭代,并与内部迭代的结果进行比较:

[php] view plaincopy

$test = new Test;

echo "hr";

echo "traverse outside:".EOL;

foreach( $test as $key=$value ){

echo $key."=".$value.EOL;

}

echo "hr";

echo "traverse inside:".EOL;

$test-traverseInside();

迭代的结果为:

可以看出:外部foreach循环的结果,只是将对象的公有属性(public)循环出来了,而对于私有属性(private),外部foreach是无法循环出来的。因而我们如果想要在外部通过foreach循环出类的所有的属性(公有的和私有的),仅仅依靠foreach是不行的,必须要对类进行“改造”。如何对类进行改造呢?如果你了解foreach的实现(参考laruence的博客:),那么可以很轻松地找到相应的方案。另外一方面,《设计模式-可复用面向对象软件设计的基础》中也提到:通过将对象的访问和遍历从对象中分离出来并放入一个迭代器对象中,迭代器模式可以实现以不同的方式对对象进行遍历。我们暂时不去深挖这句话的意思,只要知道,使用迭代器可以对对象进行遍历即可。

PHP手册预定义接口部分指出:要实现迭代器模式,需要在可迭代对象中实现如下接口:

[php] view plaincopy

abstractpublicmixedcurrent( void )

abstractpublicscalarkey( void )

abstractpublicvoidnext( void )

abstractpublicvoidrewind( void )

abstractpublicbooleanvalid( void )

有了这个。实现迭代器模式就很方便了,一个简单的实例如下:

[php] view plaincopy

class TestIterator implements Iterator {

private $point = 0;

private $data = array(

"one","two","three",

);

public function __construct() {

$this-point = 0;

}

function rewind() {

$this-point = 0;

}

function current() {

return $this-data[$this-point];

}

function key() {

return $this-point;

}

function next() {

++$this-point;

}

function valid() {

return isset($this-data[$this-point]);

}

}

$it = new TestIterator;

foreach($it as $key = $value) {

echo $key, $value;

echo "\n";

}

当然,使用了迭代器的对象可以以如下方式进行遍历:

[php] view plaincopy

$it = new TestIterator;

$it-rewind();

while ($it-valid()){

$key = $it-key();

$value = $it-current();

echo "$key=$value";

$it-next();

}

最后附上YII中ListIterator(顾名思义,实现对List的迭代操作的迭代器)的实现:

[php] view plaincopy

?php

/**

* CListIterator class file.

*

* @author Qiang Xue qiang.xue@gmail.com

* @link

* @copyright Copyright © 2008-2011 Yii Software LLC

* @license

*/

/**

* CListIterator implements an interator for {@link CList}.

*

* It allows CList to return a new iterator for traversing the items in the list.

*

* @author Qiang Xue qiang.xue@gmail.com

* @version $Id$

* @package system.collections

* @since 1.0

*/

class CListIterator implements Iterator

{

/**

* @var array the data to be iterated through

*/

private $_d;

/**

* @var integer index of the current item

*/

private $_i;

/**

* @var integer count of the data items

*/

private $_c;

/**

* Constructor.

* @param array $data the data to be iterated through

*/

public function __construct($data)

{

$this-_d=$data;

$this-_i=0;

$this-_c=count($this-_d);

}

/**

* Rewinds internal array pointer.

* This method is required by the interface Iterator.

*/

public function rewind()

{

$this-_i=0;

}

/**

* Returns the key of the current array item.

* This method is required by the interface Iterator.

* @return integer the key of the current array item

*/

public function key()

{

return $this-_i;

}

/**

* Returns the current array item.

* This method is required by the interface Iterator.

* @return mixed the current array item

*/

public function current()

{

return $this-_d[$this-_i];

}

/**

* Moves the internal pointer to the next array item.

* This method is required by the interface Iterator.

*/

public function next()

{

$this-_i++;

}

/**

* Returns whether there is an item at current position.

* This method is required by the interface Iterator.

* @return boolean

*/

public function valid()

{

return $this-_i$this-_c;

}

}

php对mysql数据库遍历操作

既然是遍历,那就将数据库指针先移到第一条记录,逐次取出数据进行运算,下移指针,直到库结束。

通常的代码如下:

mysql_data_seek($result,0);//指针复位

while($row=mysql_fetch_array($result)) { 

//对每行记录进行运算 处理,如 :echo $row['name']."br /"; 

}

php遍历数据库问题

首先你要说你用的是什么数据库。用最普通的mysql数据库来说,php自带了一些操作数据库的函数。

首先你将语句写入一个变量:

$Query = "select * from A_table";

然后用mysql_query这个函数执行这条语句,并将输出结果放在一个变量中:

$Result = mysql_query($Query);

这个$Result变量就是一个资源变量,包含了所有符合条件的结果。要将结果处理,需要用另一个函数

mysql_fetch_assoc:

while($Row = mysql_fetch_assoc($Result))

{

//这里$Row就是遍历了结果的每一行。假设有个字段叫A_field,你要把它输出

echo $Row["A_field"];

//其他操作类似。

}

php对象数组遍历后获取对象中的数据

foreach($project as $item){

echo $item-sample_status; 

}


新闻名称:php遍历数据库对象 php循环读取数据库表数据
网站链接:http://bjjierui.cn/article/ddcsidi.html

其他资讯