简体   繁体   中英

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. 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?

Found the solution thanks to 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(); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM