donglou8371 2015-06-08 22:45
浏览 17
已采纳

PHP - 为每个项目创建唯一的ID号

I have an image array loop, and it successfully loops through my image items. However, I need to give the 'data-slide-index' attribute for each item a unique number, starting from 0, adding 1 for each following item (0 - 9 if there are ten images in total). I realize this is probably simple stuff but I am a PHP newbie so would really appreciate a tip of about the best, simplest way to do this.

<?php $images = get_post_meta(get_the_ID(), "images", true);
$images = unserialize($images);

foreach($images as $image) {
    $ar[] = array("order" => $image['order'], "img_id" => $image['image_id'], "desc" => $image["desc"]);
}

asort($ar);

foreach($ar as $item) {

    $image_id = $item['img_id'];
    $media_med = wp_get_attachment_image_src( $image_id, "medium", false);
    $media_full = wp_get_attachment_image_src( $image_id, "full", false);

    echo "<a data-slide-index='" . $count ."' href=''><img data-title='" . $item["desc"] . "' data-big='". $media_full[0] . "' src='" . $media_med[0] . "'></a>";
} ?>
  • 写回答

1条回答 默认 最新

  • dsfew215211 2015-06-08 22:49
    关注
    <?php $images = get_post_meta(get_the_ID(), "images", true);
    $images = unserialize($images);
    
    foreach($images as $image) {
            $ar[] = array("order" => $image['order'], "img_id" => $image['image_id'], "desc" => $image["desc"]);
        }
    
        asort($ar);
    //Create count variable
    $i=0;    
    foreach($ar as $item) {
    
    $image_id = $item['img_id'];
    $media_med = wp_get_attachment_image_src( $image_id, "medium", false);
    $media_full = wp_get_attachment_image_src( $image_id, "full", false);
    
     //assign the number to the slide index
     echo "<a data-slide-index='".$i."' href=''><img data-title='" . $item["desc"] . "' data-big='". $media_full[0] . "' src='" . $media_med[0] . "'></a>";
    
    //plus up the variable for each loop item
    $i++;
    } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计