H
H
Hancock_8882014-01-31 10:33:59
PHP
Hancock_888, 2014-01-31 10:33:59

How to optimize php code?

Код задает определенную ширину ($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';

}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
smilingcheater, 2014-01-31
@Hancock_888

For example so. Set an array describing the appropriate indexes and their corresponding sizes, search for the appropriate index through array_search and fill in the necessary variables:

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

A
Akmal Kadirov, 2014-01-31
@kadirov

at least you need to use switch case

T
TekVanDo, 2014-01-31
@TekVanDo

If the numbers are not connected by any logic, then at least like this:

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question