2016-06-17 4 views
-5

私は$ i foreachループの制限の文字列を使用しています。if(コマンド)でブレークを使用する方法

$i = 0; 
foreach($string as $menu){ 
    $i++; 
    if($i == 6){ 
    break; 
    } 
    $menu_name = $menu['name']; 
    $menu_style = $menu['style']; 

    // i want to show total 5 menu names. 


    if($menu_style == 'magazine'){ 
    // i have 5+ menus names (magazine style) 
    // butt here i want to show just one last magazine 
    echo $menu_name; 
    } 

    if($menu_style == 'normal'){ 
    // i have 5+ names menus (normal style) 
    // butt here i want to show 4 last normal style 
    echo $menu_name.'<br>'; 
    } 

} 

SQLクエリでLIMITを使用することはできません。

($ i == 5){ブレーク; }その後、コード表示だけで4 メニュー名

私の必要に応じて、表示メニュー名であるかを教えてください。

+0

ここで問題となるのは正確ですか? 5回反復したい場合は、すでにコード内で行っている 'if($ i == 6)'を使ってください。あなたが求めていることははっきりしていません。 – David

+0

if($ menu_style == 'magazine')を選択して、表示名を1つだけ表示したいとします。 –

+0

とif($ menu_style == 'normal')ので、表示名4の名前です。 –

答えて

0

これは何ですか?

$i = 0; 

foreach($string as $menu){ 
    $i++; 
    if($i == 6){ 
    break; 
    } 
    $menu_name = $menu['name']; 
    $menu_style = $menu['style']; 

    // i want to show total 5 menu names. 


    if($menu_style == 'magazine' && $i <= 5){ 
    // i have 5+ menus names (magazine style) 
    // butt here i want to show just one last magazine 
     echo $menu_name; 
    } else if($menu_style == 'normal' && $i <= 4){ 
    // i have 5+ names menus (normal style) 
    // butt here i want to show 4 last normal style 
     echo $menu_name.'<br>'; 
    } 

} 
+0

ありがとうございます..あなたは私の問題を解決しました –

関連する問題