簡體   English   中英

不同類別的php數組列表項

[英]Php Array list items with different class

這是我當前的數組,顯示無序列表中的列表項。

function get_bottle_colors() {
    if(empty($_GET['cap_id'])) return false;
    $constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
    if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
        $output = '<label>Bottle Color</label><ul>';
            foreach ($constructor_img as $key => $image) {
                if(empty($image['image'])) continue;
                $output .= '<li><a href="'.$image['image'].'" data-width="'.$img_size[0].'" data-height="'.$img_size[1].'"';
                $output .= '</a></li>';
            }
        $output .= '</ul>';
    }else{
        $output = '<label>Bottle Color</label><ul></ul>';
    }
    echo $output;
    die();
}

總共將最多生成16個列表項。 我需要每個列表項都有自己的類,例如:list class =“ red”,list class =“ green”等。我知道如何實現這一目標嗎?

感謝Anant找到了解決方案。 不得不像下面這樣聲明類數組。

function get_bottle_colors() {
    if(empty($_GET['cap_id'])) return false;
    $constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
    if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
        $output = '<label>Bottle Color</label><ul>';
        $i = 0;
        $class_array = array("a","b","c","d","e","f","g","h","i","j","k","l","n","m","n","o","p");
            foreach ($constructor_img as $key => $image) {
                if(empty($image['image'])) continue;
                $category = 9;
                $img_size = getimagesize($image['image']);
                $output .= '<li class= "'.$class_array[$i].'"><a href="'.$image['image'].'" data-width="'.$img_size[0].'" 

                data-height="'.$img_size[1].'"';
                $output .= 'data-id="'.$_GET['cap_id'].'_'.$key.'" data-part="#constructor-bottles" class="sub-caps">'.$image['title'];
                $output .= '</a></li>';     
                $i++;
            }
            $output .= '</ul>';
        }else{
            $output = '

<label>Bottle Color</label><ul></ul>';  }   echo $output;   die(); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM