2013-07-27 25 views
0

我創造衛生組織項目的出現決定於一組「模式」(即通過AJAX傳遞)動態菜單。關閉它會創建菜單,禁用和隱藏與該模式無關的圖標。更有效的方式來構建動態菜單

的問題是,在我的實現也有很多的,如果條件。任何人都可以向我展示一種更乾淨的方式來做我想要達到的目標嗎?

我的代碼是:

public function gridMenu() 
    { 
    $mode = Validate::sanitize($_POST['mode']); 

    $modes = array(
     'products' => array('edit', 'delete', 'archive') 
    ); 

    $output = '<div id="hexContainer">'; 
    for($i = 1; $i < 7; $i++) { 
     $img = ''; 
     $output .= '<div class="hex hex' . $i; 
     if($i == 1) 
     { 
     if(in_array('edit', $modes[$mode])) 
     { 
      $output .= ' hex-selectable'; 
      $img = '<img data-option="Edit" src="' . ROOT . 'images/edit.png">'; 
     } else { 
      $output .= ' hex-disabled'; 
     } 
     } 
     if($i == 2) 
     { 
     if(in_array('zzz', $modes[$mode])) 
     { 
      $output .= ' hex-selectable'; 
     } else { 
      $output .= ' hex-disabled'; 
     } 
     } 
     if($i == 3) 
     { 
     if(in_array('delete', $modes[$mode])) 
     { 
      $output .= ' hex-selectable'; 
      $img = '<img data-option="Delete" src="' . ROOT . 'images/delete.png">'; 
     } else { 
      $output .= ' hex-disabled'; 
     } 
     } 
     if($i == 4) 
     { 
     if(in_array('xxx', $modes[$mode])) 
     { 
      $output .= ' hex-selectable'; 
     } else { 
      $output .= ' hex-disabled'; 
     } 
     } 
     if($i == 5) 
     { 
     if(in_array('archive', $modes[$mode])) 
     { 
      $output .= ' hex-selectable'; 
      $img = '<img data-option="Archive" src="' . ROOT . 'images/archive.png">'; 
     } else { 
      $output .= ' hex-disabled'; 
     } 
     } 
     if($i == 6) 
     { 
     if(in_array('zzz', $modes[$mode])) 
     { 
      $output .= ' hex-selectable'; 
     } else { 
      $output .= ' hex-disabled'; 
     } 
     } 

     $output .= '">'; 
     $output .= $img; 
     $output .= '</div>'; 
    } 
    $output .= '<div class="hex hex-mid"></div>'; 
    $output .= '</div>'; 
    echo $output; 
    } 
+0

你不覺得[CodeReview](http://codereview.stackexchange.com)會是一個更好的地方問嗎? –

回答

0

使用switch功能:

switch ($i){ 
    case 1: 
     //code for $i==1 
     break; 
    case 2: 
     // code for $i==2 
     break; 

    //... 

} 
0

這尚未經過測試,但你可以嘗試沿着該線的東西。

基本上是從你的代碼我假設你想只在奇數輪插入圖像。因此,我創建了一個$_modes$_mode變量,它將保存每一輪的值。

有了這些價值觀,我們會自動創建使用你已經在你的代碼設置的規則的HTML類,並在每一輪,如果是奇數,以及陣列中那麼我們還可以添加圖片。

public function gridMenu() { 
    $mode = Validate::sanitize($_POST['mode']); 

    $modes = array(
     'products' => array('edit', 'delete', 'archive') 
    ); 

    $_modes = array(
     false, 
     array('Edit', 'edit'), 
     array('ZZZ', 'zzz'), 
     array('Delete', 'delete'), 
     array('XXX', 'xxx'), 
     array('Archive', 'archive'), 
     array('ZZZ', 'zzz'), 
    ); 

    $output = '<div id="hexContainer">'; 

    for($i = 1; $i < 7; $i++) { 
     $_mode = $_modes[$i]; 

     $img = ''; 
     $classnames = array('hex', 'hex' . $i); 

     if (($i % 2) === 0 && in_array($_mode[1], $modes[$mode])) { 
      $img = '<img data-option="' . $_mode[0] . '" src="' . ROOT . 'images/' . $_mode[1] . '.png">'; 
     } 

     $classnames[] = (in_array($_mode[1], $modes[$mode]) ? 'hex-selectable' : 'hex-disabled'); 

     $output .= '<div class="' . implode(' ', $classnames) . '">'; 
     $output .= $img; 
     $output .= '</div>'; 
    } 

    $output .= '<div class="hex hex-mid"></div>'; 
    $output .= '</div>'; 

    echo $output; 
} 
相關問題