Dio Cane
Dio Cane

Reputation: 83

PHP Counter: Incremental ID for each item in foreach

I have a foreach, I'd like to have an incremental id="X" (X starting from 1 to - say - 6 if 6 items are present) for every item.

Here's the code:

<?php
function blahblah(){
    $url = 'THE_URL_WHERE_I_RETRIEVE_JSON';
    $cache = './BLAH/'.sha1($url).'.json';
    if(file_exists($cache) && filemtime($cache) > time() - 1000){
        $jsonData = json_decode(file_get_contents($cache));
    } else {
         $jsonData = json_decode((file_get_contents($url)));
        file_put_contents($cache,json_encode($jsonData));
    }
    $result = '<div id="my_div">'.PHP_EOL;
     if(is_array($jsonData->data)){
        // Do the for each
    } else {
        // It wasn't an array so do something else
    }
     foreach ($jsonData->data as $key=>$value) {
        $title = (!empty($value->caption->text))?' '.$value->caption->text:'...';
        $location = (!empty($value->location->name))?' at '.$value->location->name:null;
        $result .= "\t".'<a href="'.$value->images->standard_resolution->url.'">BLAHBLAH</a>'.PHP_EOL;
    }
    $result .= '</div>'.PHP_EOL;
     return $result;
}
echo get_instagram();
?>

For example I'd like that id="X" to be here <a ID="" href="">

Upvotes: 1

Views: 8767

Answers (4)

Rajeev Ranjan
Rajeev Ranjan

Reputation: 4142

use may use a counter for this

    <?php
    function blahblah(){
     $url = 'THE_URL_WHERE_I_RETRIEVE_JSON';
    $cache = './BLAH/'.sha1($url).'.json';
      if(file_exists($cache) && filemtime($cache) > time() - 1000){
      $jsonData = json_decode(file_get_contents($cache));
    } else {
     $jsonData = json_decode((file_get_contents($url)));
     file_put_contents($cache,json_encode($jsonData));
    }
    $result = '<div id="my_div">'.PHP_EOL;
     if(is_array($jsonData->data)){
        // Do the for each
    } else {
    // It wasn't an array so do something else
    }
    $i=1;//use as counter
    foreach ($jsonData->data as $key=>$value) {
    $title = (!empty($value->caption->text))?' '.$value->caption->text:'...';
    $location = (!empty($value->location->name))?' at '.$value->location->name:null;
    $result .= "\t".'<a '.'id="'.$i.'" href="'.$value->images->standard_resolution-   >url.'">BLAHBLAH</a>'.PHP_EOL;
  $i++;
}
$result .= '</div>'.PHP_EOL;
 return $result;
}
  echo get_instagram();
?>

Upvotes: 0

baldrs
baldrs

Reputation: 2161

If there's a reason to don't use for loop, use counter variable

$c = 1;
foreach($data as $item) {
   // code
  $c++; //increment the counter at the end of loop
}

You can freely use $c inside your foreach loop after that.

Upvotes: 1

Robert
Robert

Reputation: 20286

I don't see any problem in this just add 1 variable which will work as counter

 $i = 0;
 foreach ($jsonData->data as $key=>$value) {
    $title = (!empty($value->caption->text))?' '.$value->caption->text:'...';
    $location = (!empty($value->location->name))?' at '.$value->location->name:null;
    $result .= "\t".'<a id="'.++$id.'" href="'.$value->images->standard_resolution->url.'">BLAHBLAH</a>'.PHP_EOL;
 }

Upvotes: 1

Nikhil Mohan
Nikhil Mohan

Reputation: 891

$idx = 1;
foreach ($jsonData->data as $key=>$value) {
        $title = (!empty($value->caption->text))?' '.$value->caption->text:'...';
        $location = (!empty($value->location->name))?' at '.$value->location->name:null;
        $result .= "\t".'<a id="'.$idx++ .'" href="'.$value->images->standard_resolution->url.'">BLAHBLAH</a>'.PHP_EOL;
}

Upvotes: 0

Related Questions