loops - how to make this php code shorter? -


i have following php code works long , cumbersome reading...

// row  if ($taktarticle[0]['t1position3'] > 3 , $taktarticle[0]['t1position3'] < 7  ) {    $row = "row1";  }   if ($taktarticle[0]['t1position3'] > 7 , $taktarticle[0]['t1position3'] < 12  ) {    $row = "row2";  }   if ($taktarticle[0]['t1position3'] > 12 , $taktarticle[0]['t1position3'] < 17  ) {    $row = "row3";  }   if ($taktarticle[0]['t1position3'] > 17 , $taktarticle[0]['t1position3'] < 22  ) {    $row = "row4";  }   if ($taktarticle[0]['t1position3'] > 22 , $taktarticle[0]['t1position3'] < 27  ) {    $row = "row5";  }   // columns  if ($taktarticle[0]['t1position3'] == 3       or $taktarticle[0]['t1position3'] == 8       or $taktarticle[0]['t1position3'] == 13       or $taktarticle[0]['t1position3'] == 18       or $taktarticle[0]['t1position3'] == 23) {     $col = "col1";  }     if ($taktarticle[0]['t1position3'] == 4       or $taktarticle[0]['t1position3'] == 9       or $taktarticle[0]['t1position3'] == 14       or $taktarticle[0]['t1position3'] == 19       or $taktarticle[0]['t1position3'] == 24) {     $col = "col2";  }   if ($taktarticle[0]['t1position3'] == 5       or $taktarticle[0]['t1position3'] == 10       or $taktarticle[0]['t1position3'] == 15       or $taktarticle[0]['t1position3'] == 20       or $taktarticle[0]['t1position3'] == 25) {     $col = "col3";  }   if ($taktarticle[0]['t1position3'] == 6       or $taktarticle[0]['t1position3'] == 11       or $taktarticle[0]['t1position3'] == 16       or $taktarticle[0]['t1position3'] == 21       or $taktarticle[0]['t1position3'] == 26) {     $col = "col4";  }   if ($taktarticle[0]['t1position3'] == 7       or $taktarticle[0]['t1position3'] == 12       or $taktarticle[0]['t1position3'] == 17       or $taktarticle[0]['t1position3'] == 22       or $taktarticle[0]['t1position3'] == 27) {     $col = "col5";  } 

now... have repeat ($taktarticle[0]['t1position3'] until ($taktarticle[0]['t1position11']

as understand, code become huge...anyone have idea how code can shortened?

regards, john

you can create functions clean code. there obvious patterns in code, looking patterns , generalizing patterns key requirement cleaning code. i'm sure php guru find more concise way accomplish this, basic example like:

function get_row($position) {   $row_ranges = array(     array(3, 7),     array(7, 12),     // etc   );         foreach ($row_ranges $row_index => $range) {     if ($range[0] < $position && $position < $range[1]) {        return sprtintf('row%s', $row_index + 1)     }   }  } 

all row ranges kept in centralized location inside of function, , there no more repeated conditionals

function get_column($value) {   // looks starting @ 3 , have increments of 5    // 3, 8, 13, 18   // loop through , calculate these, or hardcode them in   //  use `in_array` clean multiple or statements   if (in_array($value, array(3, 8, 13, 18))) {    } } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -