Show divs based on the amount of arrays

后端 未结 2 1781
醉话见心
醉话见心 2021-01-20 14:43

Here is my function

function yyy_hero_image_option_callback() {
$hero_options = get_option( \'hero_options\' ); 
$count=count($hero_options);
$totalimg=$coun         


        
相关标签:
2条回答
  • 2021-01-20 14:52

    try:

    function yyy_hero_image_option_callback() {
    
      $hero_options = get_option( 'hero_options' ); 
      $i=1;
      $html = '<div id="upload_zzz_sets">';
    
      foreach($hero_options as $key => $values){
        if (strpos($key, 'zzz')) { 
            $html .= '<div id="zzzclonedInput'.$i.'" class="zzzclonedInput">';
        $html .= '<input id="cs_product_menu_zzz_src_'.$i.'" type="text" size="36" name="hero_options[upload_zzz_link_'.$i.']" value="' . $values . '" /> <input id="cs_product_menu_zzz_src_'.$i.'_zzzbutton" type="button" value="Add / Change" class="button-secondary zzz-upload-button"  /> <div class="button zzzremove">-</div>';
        $html .= '</div>';
         $i++;
    
        }
    
    
      }
    
      $html .= '</div><div class="button zzzclone">Add an Image</div>';
    
      echo $html; 
    }
    
    0 讨论(0)
  • 2021-01-20 15:09

    The way you changed your code, $i is not set anymore, try this :

    foreach ($hero_options as $key => $value) {
        if (strpos($key, '_zzz_')) {
            $i = substr($key, -1); // This will retrieve the last char of $key, which is corresponding to your previous $i
            $html = '<div id="upload_zzz_sets">';
            $html .= '<div id="zzzclonedInput'.$i.'" class="zzzclonedInput">';
            $html .= '<input id="cs_product_menu_zzz_src_'.$i.'" type="text" size="36" name="' . $value . '" value="' . $value . '" /> <input id="cs_product_menu_zzz_src_'.$i.'_zzzbutton" type="button" value="Add / Change" class="button-secondary zzz-upload-button"  /> <div class="button zzzremove">-</div>';
            $html .= '</div>';
            $html .= '</div>';
            echo $html;
        }
    } 
    

    I also replaced the $hero_options[key] parts by $value, since you already get it from the foreach.

    0 讨论(0)
提交回复
热议问题