2011-07-08 26 views
0

我有一個項目列表,這些是路線的點。這就是我想實現:mysql_fetch_object:獲取一個和對象,下一個?

<ul> 
     <li>Point A - Point B</li> 
     <li>Point B - Point C</li> 
     <li>Point C - Point D</li> 
     <li>Point D - Point E</li> 
    </ul> 

不過話又說回來,我只知道如何讓他們中的一個,以便一旦這樣做:

while($row=mysql_fetch_object($res)){ 
    echo'<li>'.$row->title.'</li>'; 
    } 

我能在同一迴路中的兩個點?我應該如何循環以獲得一個點和下一個點? (我的SQL查詢給了我命令,像這樣的點:A點,B,點C,點d,e點)

+0

你能張貼您的當前查詢? –

回答

2
//Note that this requires at least two rows to work. 
//Check with mysql_num_rows before running this if you are unsure. 
$first = mysql_fetch_object($res); 
$second = mysql_fetch_object($res); 

do { 

    //do something with the two rows here 
    //in your case: 
    echo'<li>'. $first->title .' - '. $second->title .'</li>'; 

    //Move the second row to be the first 
    $first = $second; 
} while($second = mysql_fetch_object($res)); 
+0

是的,就是這樣,太棒了! –

0
while($row=mysql_fetch_object($res)){ 
    $myresult[] = $row->title; 
    } 

foreach($myresult as $key=>$value){ 

    echo $value->title . (isset($myresult[$key+1]) ? ('-'. $myresult[$key+1]->title): ''); 

}