There are 30 variables, each of them stores values:

$level1 = 36; $level2 = 72; $level3 = 144; $level4 = 288; 

There is a scenario that looks at the amount of experience (value of variables), if the experience is suitable, then it sends to the database level. Scenario:

 if ($experience <= $level1) { $result = mysql_query("UPDATE $db_table SET level = 1 WHERE id ='$id'"); } else { if ($experience <= $level2) { $result = mysql_query("UPDATE $db_table SET level = 1 WHERE id ='$id'"); } else { if ($experience <= $level3) { $result = mysql_query("UPDATE $db_table SET level = 2 WHERE id ='$id'"); } else { if ($experience <= $level4) { $result = mysql_query("UPDATE $db_table SET level = 3 WHERE id ='$id'"); } else { if ($experience <= $level5) { $result = mysql_query("UPDATE $db_table SET level = 4 WHERE id ='$id'"); } } } } } 

As can be seen from the script, each action is almost repeated, the code is long, can it be reduced?

    4 answers 4

    In three lines:

     $levels = array( $level1, 72, 144, 288, ... , $level30, $experience); sort( $levels); $result = mysql_query( sprintf("UPDATE %s SET level=%d WHERE id='%s'", $db_table, 1 + array_search( $experience, $levels), $id)); 
       $levels = array($level1, $level2, $level3, ..., $level30); foreach($levels as $key=>$level) { if ($experience <= $level) { $result = mysql_query ("UPDATE $db_table SET level = $key WHERE id =$id"); break; } } 
      • one
        ..and we miraculously appear level zero! :) - Sergiks

      Tolley I'm a pervert, or everything around:

       for($a=36,$i=1;(($experienced-$a)>=0);$a*=2,$i++); //<--- Внимание: точка с зпт print ("Ваш уровень: $i\n"); print ("следующий ап на: $a\n"); print ("сейчас опыта: $experienced\n"); 

      I hope how to sew it with the sql query myself think up, it's not so difficult.

      UPD:
      I'm a weirdo:

       for($a=36,$i=1;(($experienced-$a)>=0);$a*=2,$i++); $result = mysql_query("UPDATE $db_table SET level = $i WHERE id ='$id'"); 

      Oh, what a speed will be, but I broke my brain.

      • cool with multiple statements in if() ! I forgot that you can. Thank. - Sergiks
      • yes not for that. I read the book on Si. There such example all the time. - zenith

      Get a level table. The level for each player is not worth keeping. You can get it at any time by experience. This is excessive data storage. If you have expo levels change, you will have to update the entire database.

      Here is an example of the structure and query in SQLFiddle .

      • A million players, at the entrance of each new, it should be sent to the "room" with the peppers of his own "level". Which is faster: count each time, again and again, or select by index? The question is not “how to live correctly”, but how to optimize a specific php code. Would you recommend to go to Python) - Sergiks
      • If you pay attention to your answer, in order to get a quick sample of the index, you will have to update each of the million players with each update of the exp. So count how many updates you will have every millisecond for the sake of one index query every half hour. Would you advise hard-coded players? =) - Yura Ivanov
      • How exciting to discuss the device of something there, with "would", yeah .. Waiting for an answer to the question, rather than a thought on the tree. - Sergiks
      • @sergiks, correctly "by the tree". Your "optimization" is no better than the original listing. In short, yes, but not optimally. The fact that you answer a novice in the style of "what the question is the answer" is unlikely to be beneficial in the long run. And judging by the fact that your answer was accepted, the optimization is just beginning. Thanks, by the way for a minus, I collect them. - Yura Ivanov