简体   繁体   English

不同类别的php数组列表项

[英]Php Array list items with different class

This is my current array that displays list items in a unordered list. 这是我当前的数组,显示无序列表中的列表项。

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();
}

In total there will be up to 16 list items generated by this. 总共将最多生成16个列表项。 I need each list item to have its own class eg: list class="red", list class="green", etc. Any idea how i go about achieving this? 我需要每个列表项都有自己的类,例如:list class =“ red”,list class =“ green”等。我知道如何实现这一目标吗?

Found the solution thanks to Anant. 感谢Anant找到了解决方案。 Had to declare the class array like below. 不得不像下面这样声明类数组。

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