Как добавить счетчик для каждого цикла - PullRequest
1 голос
/ 10 мая 2011

Я пытаюсь выяснить, как я могу добавить счетчик, чтобы каждый результат мог отображаться как -b-c и т. Д., Но я не знаю, что мне нужно сделать, вы можете мне помочь.Чтобы было понятнее, я пытаюсь добавить то, что есть у Google Maps при поиске, и он показывает маркер и a, b, c по результатам.

$xmlString = file_get_contents($mapURL);
$xmlString = str_replace('georss:point','point',$xmlString);

$xml = new SimpleXMLElement($xmlString);
$items = $xml->xpath('channel/item');
$closeItems = array();
foreach($items as $item)
{
    $latlng = explode(' ',trim($item->point));

    //calculate distance
    $dist = calc_distance(array('lat'=>$zlat,'long'=>$zlng),array('lat'=>$latlng[0],'long'=>$latlng[1]));
    if($dist<={segment_4})$closeItems[] = $item;
}

?>
<div style="float:left; width:100%;margin:10px 0px;"><h1 style="font-size:16px; font-weight:bold; border-bottom:1px solid #333;">Servicer Results (within {segment_4} miles)  of Your selected area</h1></div>

// код вопроса

 <?php foreach($closeItems as $item ):?> 
            <div style="float:left; width:275px;height:auto;margin:20px 5px;margin-left:40px; font-family: Arial,Helvetica;font-size:12px">
        <h2 style="font-size:16px;color: #499AAE; ;"><img src="http://winstonind.com/images/mapicon.png" alt="map icon" />&nbsp;<strong><?=$item->title?></strong></h2>
    </div>

Новый код работает, но не как буквы

 <div style="float:left; width:100%;margin:10px 0px;"><h1 style="font-size:16px; font-weight:bold; border-bottom:1px solid #333;">Servicer Results (within {segment_4} miles)</h1></div>
<?php foreach($closeItems as $index=>$item ):?> 

    <div style="float:left; width:275px;height:auto;margin:20px 5px;margin-left:40px; font-family: Arial,Helvetica;font-size:12px">
    <h2 style="font-size:16px;color: #499AAE; ;"><img src="http://winstonind.com/images/mapicon.png" alt="map icon" />&nbsp;<strong><? echo $index; ?><?=$item->title?></strong></h2>
</div

>

Ответы [ 4 ]

2 голосов
/ 10 мая 2011

Вы можете распечатать результаты в упорядоченном списке:

<ol type="a">
    <? foreach($closeItems as $item ):?> 
        <li><!-- whatever --></li>
    <? endforeach; ?>
</ol>
1 голос
/ 10 мая 2011

Используйте коды ascii в сочетании с chr ()

Обновите следующий код и проверьте руководство по chr(), должно быть то, что вам нужно.

...
$ascii = 97;
foreach($items as $item) {
  $letter = chr($ascii++);
  echo $letter;
  ...

Примечание: Это решение PHP, если вы используете значения.Однако, если это строго для отображения Джимми Савчук имеет хорошее решение CSS.

0 голосов
/ 10 мая 2011

попробуй

 <?php 
      $ascii = 65;
      for($i = 0; $i < sizeof($closeItems); ++$i){ ?> 
    <div style="float:left; width:275px;height:auto;margin:20px 5px;margin-left:40px; font-family: Arial,Helvetica;font-size:12px">
        <h2 style="font-size:16px;color: #499AAE; ;"><img src="http://winstonind.com/images/mapicon.png" alt="map icon" />&nbsp;<strong><? echo chr($ascii) + ': ' + $closeItems[$i]->title; $ascii++; ?></strong></h2>
    </div>
 <? } ?>
0 голосов
/ 10 мая 2011

foreach имеет счетчик того, на чём вы находитесь:

<?php foreach($closeItems as $index=>$item ):?> 
//now just use $index as your counter
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...