How to add a count in a for each loop
Im trying to figure out how I can add count so that every result can show up like a - b - c etc.. but I don't know what I need to do can you help. To make it clearer im trying to add what google maps has when you search and it shows the marker and an a,b,c by the results.
$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>
//code in question
<?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" /> <strong><?=$item->title?></strong></h2>
</div>
New code works but not as letters
<div style="float:left; width:100%;margin:10px 0px;"><h1 style="font-size:16px; font-weight:bold; border-bottom:1px solid #333;开发者_JS百科">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" /> <strong><? echo $index; ?><?=$item->title?></strong></h2>
</div
>
You could print the results in an ordered list:
<ol type="a">
<? foreach($closeItems as $item ):?>
<li><!-- whatever --></li>
<? endforeach; ?>
</ol>
Use ascii codes combined with chr()
Update the following to your code and check the manual for chr()
, should be what you need.
...
$ascii = 97;
foreach($items as $item) {
$letter = chr($ascii++);
echo $letter;
...
Note: This is a PHP solution if you are using the values. However, if this is strictly for display Jimmy Sawczuk has a good CSS solution.
foreach
has a counter of what itteration you are at:
<?php foreach($closeItems as $index=>$item ):?>
//now just use $index as your counter
try
<?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" /> <strong><? echo chr($ascii) + ': ' + $closeItems[$i]->title; $ascii++; ?></strong></h2>
</div>
<? } ?>
精彩评论