mysql_fetch_field()取出該表的字段的信息啊,取一個(gè),mysql_fetch_field()內(nèi)部對(duì)偏移量+1,然后取下個(gè)字段的信息,取到最后一個(gè)了,再運(yùn)行時(shí)mysql_fetch_field就返回false拉,然后就跳出循環(huán)啊
成都創(chuàng)新互聯(lián)公司主營(yíng)雞西梨樹(shù)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā),雞西梨樹(shù)h5小程序開(kāi)發(fā)搭建,雞西梨樹(shù)網(wǎng)站營(yíng)銷推廣歡迎雞西梨樹(shù)等地區(qū)企業(yè)咨詢
foreach($project?as?$item){
echo?$item-sample_status;?
}
?php
session_start();
include ("CartItem.class.php");
$cart = array ();
$cart = $_SESSION['gouwu'];
$id = $_GET['id'];
$name = $_GET['name'];
$price = $_GET['price'];
if ($cart == null) {
$pin = new CartItem();
$pin-setId($id);
$pin-setName($name);
$pin-setPrice($price);
$cart = $pin;
$_SESSION['gouwu'] = $cart;
?
table
tr
td商品/tdtd數(shù)量/tdtd價(jià)錢(qián)/td
?php
for ($i = 0; $i count($cart); $i++) {
$cart[$i]-getId();
?
trtd?=$pin-getId() ?/tdtd?=$pin-getName() ?/tdtd?=$pin-getPrice() ?/td /tr
?php
}}
?
/tr
什么錯(cuò)誤不貼出來(lái),只能猜著改了
不能使用數(shù)組類型的對(duì)象__PHP_Incomplete_Class
很簡(jiǎn)單,$cart是數(shù)組,怎么下面來(lái)obj那?直接用數(shù)組循環(huán)出來(lái)試試 我太明白你寫(xiě)的代碼的意思 有點(diǎn)亂
foreach
語(yǔ)法結(jié)構(gòu)提供了遍歷數(shù)組的簡(jiǎn)單方式。foreach
僅能夠應(yīng)用于數(shù)組和對(duì)象,如果嘗試應(yīng)用于其他數(shù)據(jù)類型的變量,或者未初始化的變量將發(fā)出錯(cuò)誤信息。有兩種語(yǔ)法:
foreach
(array_expression
as
$value)
statement
foreach
(array_expression
as
$key
=
$value)
statement
第一種格式遍歷給定的
array_expression
數(shù)組。每次循環(huán)中,當(dāng)前單元的值被賦給
$value
并且數(shù)組內(nèi)部的指針向前移一步(因此下一次循環(huán)中將會(huì)得到下一個(gè)單元)。
第二種格式做同樣的事,只除了當(dāng)前單元的鍵名也會(huì)在每次循環(huán)中被賦給變量
$key。
范例:
$a
=
array(
"one"
=
1,
"two"
=
2,
"three"
=
3,
"seventeen"
=
17);foreach
($a
as
$k
=
$v)
{
echo
"\$a[$k]
=
$v.\n";}
可以將循環(huán)輸出的東西,賦值給一個(gè)數(shù)組,并將該數(shù)組返回;
class name{
function name(){
while($result=$conf-fetch_array($query){
$two_array[]=$result;
}
return $two_array;
}
}
這樣就把要循環(huán)的所有數(shù)據(jù) 賦值給二為數(shù)組 $two_array; 并返回該數(shù)組的值.然后把在其他頁(yè)引用該數(shù)組;
?php
include_once("上面的類")
$name = new name;
$define_array_name = $name-name();
用$define_array_name[0][1]的方式顯示到表格中;
?
其實(shí)百度一下就知道
我們知道,php中,foreach可以很方便地對(duì)可迭代結(jié)構(gòu)(例如數(shù)組,再如對(duì)象)進(jìn)行迭代操作:
[php] view plaincopy
foreach( $array as $elem){
var_dump($elem);
}
[php] view plaincopy
foreach($obj as $key=$value){
echo "$key=$value".PHP_EOL;
}
因而我們想:如果對(duì)于一個(gè)實(shí)例化對(duì)象,對(duì)其進(jìn)行foreach操作,會(huì)發(fā)生什么事情呢?
首先我們定義的基礎(chǔ)類為:
[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;
}
}
}
然后我們實(shí)例化該類,對(duì)其進(jìn)行迭代,并與內(nèi)部迭代的結(jié)果進(jìn)行比較:
[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();
迭代的結(jié)果為:
可以看出:外部foreach循環(huán)的結(jié)果,只是將對(duì)象的公有屬性(public)循環(huán)出來(lái)了,而對(duì)于私有屬性(private),外部foreach是無(wú)法循環(huán)出來(lái)的。因而我們?nèi)绻胍谕獠客ㄟ^(guò)foreach循環(huán)出類的所有的屬性(公有的和私有的),僅僅依靠foreach是不行的,必須要對(duì)類進(jìn)行“改造”。如何對(duì)類進(jìn)行改造呢?如果你了解foreach的實(shí)現(xiàn)(參考laruence的博客:),那么可以很輕松地找到相應(yīng)的方案。另外一方面,《設(shè)計(jì)模式-可復(fù)用面向?qū)ο筌浖O(shè)計(jì)的基礎(chǔ)》中也提到:通過(guò)將對(duì)象的訪問(wèn)和遍歷從對(duì)象中分離出來(lái)并放入一個(gè)迭代器對(duì)象中,迭代器模式可以實(shí)現(xiàn)以不同的方式對(duì)對(duì)象進(jìn)行遍歷。我們暫時(shí)不去深挖這句話的意思,只要知道,使用迭代器可以對(duì)對(duì)象進(jìn)行遍歷即可。
PHP手冊(cè)預(yù)定義接口部分指出:要實(shí)現(xiàn)迭代器模式,需要在可迭代對(duì)象中實(shí)現(xiàn)如下接口:
[php] view plaincopy
abstractpublicmixedcurrent( void )
abstractpublicscalarkey( void )
abstractpublicvoidnext( void )
abstractpublicvoidrewind( void )
abstractpublicbooleanvalid( void )
有了這個(gè)。實(shí)現(xiàn)迭代器模式就很方便了,一個(gè)簡(jiǎn)單的實(shí)例如下:
[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";
}
當(dāng)然,使用了迭代器的對(duì)象可以以如下方式進(jì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(顧名思義,實(shí)現(xiàn)對(duì)List的迭代操作的迭代器)的實(shí)現(xiàn):
[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;
}
}