Ali Imran Ali Imran - 5 months ago 10
PHP Question

How to use break in if (Command)

I am using $i string for limit in foreach loop.

$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>';
}

}


I can't want to use LIMIT in SQL query.


And tell me when i use if($i == 5){ break; } then code display just 4
menu name


Tell me how is display menu name as my required.

Answer

Something like that?

$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>';
   }

}