2011-07-08 4 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>'; 
    } 

は、私は同じループ上の2つのポイントを得ることができます? 1点と次の点を得るためにどのようにループする必要がありますか?

+0

はあなたの現在のクエリを投稿することができます:(点A、点B、点C、点D、点Eは、私のSQLクエリは、私はそうのよう命じたポイントを与えますか)? –

答えて

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): ''); 

} 
関連する問題