Задать вопрос

Как оптимизировать php код?

Код задает определенную ширину ($img_width) и высоту ($img_height) и класс ($grid_num), определнным блоком ($count_block)

Это все пробегается в цикле, и счетчик ($count_block ) каждый раз увеличивается на один. Как это оптимизировать? Советовать лучше сразу с примером. Спасибо

$img_width = '';
$img_height = '';
$grid_num = '';

if ($count_block == 2 || 
    $count_block == 4 || 
    $count_block == 5 || 
    $count_block == 11 || 
    $count_block == 13 || 
    $count_block == 14 || 
    $count_block == 15 || 
    $count_block == 16 || 
    $count_block == 20 || 
    $count_block == 21 || 
    $count_block == 23 || 
    $count_block == 24 || 
    $count_block == 25 || 
    $count_block == 27 || 
    $count_block == 28 || 
    $count_block == 31 || 
    $count_block == 32 || 
    $count_block == 35 || 
    $count_block == 36 || 
    $count_block == 38 || 
    $count_block == 39 || 
    $count_block == 41) {

$img_width = '100';
$img_height = '100';
$grid_num = '1';


} elseif ($count_block == 1 ||  
        $count_block == 3 ||  
        $count_block == 7 ||  
        $count_block == 8 ||  
        $count_block == 9 ||  
        $count_block == 10 ||  
        $count_block == 12 ||  
        $count_block == 17 ||  
        $count_block == 19 ||  
        $count_block == 22 ||  
        $count_block == 26 ||  
        $count_block == 29 ||  
        $count_block == 30 ||  
        $count_block == 34 ||  
        $count_block == 37 ||  
        $count_block == 42) {

$img_width = '400';
$img_height = '500';
$grid_num = '2';

}
elseif ($count_block == 6 || 
      $count_block == 18 || 
      $count_block == 33 || 
      $count_block == 40) {

$img_width = '430';
$img_height = '240';
$grid_num = '3';

}
  • Вопрос задан
  • 2890 просмотров
Подписаться 4 Оценить Комментировать
Решения вопроса 1
smilingcheater
@smilingcheater
Например так. Задать массив, описывающий подходящие индексы и соответствующие им размеры, искать подходящий индекс через array_search и заполнять нужные переменные:
$img_width = '';
$img_height = '';
$grid_num = '';

$settings = array(
    array(
        'indexes'    => array(2, 4, 5, 11, 13, 14, 15, 16, 20, 21, 23, 24, 25, 27, 28, 31, 32, 35, 36, 38, 39, 41),
        'img_width'  => 100,
        'img_height' => 100,
        'grid_num'   => 1
    ),
    array(
        'indexes'    => array(1, 3, 7, 8, 9, 10, 12, 17, 19, 22, 26, 29, 30, 34, 37, 42),
        'img_width'  => 400,
        'img_height' => 500,
        'grid_num'   => 2
    ),
    array(
        'indexes'    => array(6, 18, 33, 40),
        'img_width'  => 430,
        'img_height' => 240,
        'grid_num'   => 3
    )
);

foreach ($settings as $setting) {
    if (array_search($count_block, $setting['indexes']) !== false) {
        $img_width  = $setting['img_width'];
        $img_height = $setting['img_height'];
        $grid_num   = $setting['grid_num'];
        break;
    }
}
Ответ написан
Пригласить эксперта
Ответы на вопрос 2
как минимум надо использовать switch case
Ответ написан
TekVanDo
@TekVanDo
Javascript Developer
Если числа не связаны никакой логикой, то хотя бы так :
$numbArr["450_500"] = array(1,6,7,15,46,20);
$numbArr["100_100"] = array(5,20);
$numbArr["430_420"] = array(7,20);
if(in_array($count_block,$numbArr["450_500"])){
   $img_width = '400';
   $img_height = '500';
   $grid_num = '2';
}elseif(in_array($count_block,$numbArr["100_100"])){
   $img_width = '100';
$img_height = '100';
$grid_num = '1';
}elseif(in_array($count_block,$numbArr["430_420"])){
  $img_width = '430';
$img_height = '240';
$grid_num = '3';
}
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы