开发者

mysql_fetch_object: get one and object and the next one?

I have a list of items, those are points of a route. This is what I want to achieve:

  <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>

But then again I only know how to get one of them in order once doing this:

   whil开发者_开发技巧e($row=mysql_fetch_object($res)){
     echo'<li>'.$row->title.'</li>';
   }

Can I get the two points on the same loop? How should I loop through that to get one point and the next one? (My sql query gives me the points ordered like so: Point A, Point B, Point C, Point D, Point E)


//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));


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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜