1. PHP / Говнокод #17536

    +160

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    'idate' => 
            array (
              'UNIX' => '1346688000',
              'datetime' => '03.09.2012 19:00',
              'time' => '19:00',
              'hour' => '19',
              'second' => '00',
              'date' => '03.09.2012',
              'datename' => '03 September 2012',
              'year' => '2012',
              'y' => '12',
              'd' => '03',
              'd0' => '3',
              'm' => '09',
              'm0' => '9',
              'month' => 'September',
              'day' => 'Monday',
              'monthr' => 'сентября',
              'datenamer' => '03 сентября 2012',
              'ago' => '2 года назад',
            ),

    Правильное хранение даты. Работаю недавно и сам рак еще тот - но это слишком. Переношу БД из в свой проект и встречаю это...

    VladDelec, 28 Января 2015

    Комментарии (14)
  2. PHP / Говнокод #17522

    +157

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    for($i = 0; $i < $count; ++$i){
      $result_formatted[$i][] = $result[$counter];
      ++$counter;
      $result_formatted[$i][] = $result[$counter];
      ++$counter;
    }

    Magic operations

    alexhifer, 26 Января 2015

    Комментарии (3)
  3. PHP / Говнокод #17513

    +161

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    <?php
    /**
     * Округляет число до заданного количества знаков после запятой.
     * @param float $v	- округляемое число.
     * @param int $prec - количество знаков после запятой (по-умолчанию: 0).
     * @param str $mode - режим округления: ceil | floor | round (по-умолчанию: round).
     * @return float округлённое число.
     */
    function round2($v, $prec = 0, $mode = "round") {
    	for ($k = 1, $i = 0; $i < $prec; $i++, $k *= 10)
    		;
    	switch ($mode) {
    		case "ceil" : $v = ceil($k * $v) / $k;
    			break;
    		case "floor" : $v = floor($k * $v) / $k;
    			break;
    		default : $v = round($k * $v) / $k;
    	}
    
    	return $v;
    }

    kissarat, 24 Января 2015

    Комментарии (7)
  4. PHP / Говнокод #17504

    +157

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    function calcHTime($stt) {
            $secs   = time() - $stt;
            $h      = (int) ($secs / 3600);
            $m      = (int) (($secs - ($h * 3600)) / 60);
            $s      = (int) ($secs - ($h * 3600) - ($m * 60));
            return sprintf("%02d:%02d:%02d", $h, $m, $s);
    }

    dannikonov, 23 Января 2015

    Комментарии (1)
  5. PHP / Говнокод #17496

    +156

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    if ($var==0 && $var3==0 && $var4==0) {
    $qr_result = mysql_query("SELECT type_id, image_plan, section_type, house_number, section_number_real, rooms, prod  FROM  `psp_gen_plan_type` WHERE prod=0 ORDER BY `house_number` ASC" . $db_table_to_show)
    or die(mysql_error());	}
    elseif ($var!=0 && $var3!=0 && $var4!=0){
    $qr_result = mysql_query("SELECT type_id, image_plan, section_type, house_number, section_number_real, rooms, prod  FROM  `psp_gen_plan_type` WHERE house_number='$var' AND prod=0 AND section_type='$var3' AND rooms='$var4'" . $db_table_to_show)
    or die(mysql_error());	}
    elseif ($var!=0 && $var3==0 && $var4==0){
    $qr_result = mysql_query("SELECT type_id, image_plan, section_type, house_number, section_number_real, rooms, prod  FROM  `psp_gen_plan_type` WHERE house_number='$var' AND prod=0 ORDER BY `section_number_real` ASC" . $db_table_to_show)
    or die(mysql_error());	}
    elseif ($var==0 && $var3!=0 && $var4==0){
    $qr_result = mysql_query("SELECT type_id, image_plan, section_type, house_number, section_number_real, rooms, prod  FROM  `psp_gen_plan_type` WHERE section_type='$var3' AND prod=0 ORDER BY `house_number` ASC" . $db_table_to_show)
    or die(mysql_error());	}
    elseif ($var==0 && $var3==0 && $var4!=0){
    $qr_result = mysql_query("SELECT type_id, image_plan, section_type, house_number, section_number_real, rooms, prod  FROM  `psp_gen_plan_type` where rooms='$var4' AND prod=0 ORDER BY `house_number` ASC" . $db_table_to_show)
    or die(mysql_error());	}
    elseif ($var!=0 && $var3==0 && $var4!=0){
    $qr_result = mysql_query("SELECT type_id, image_plan, section_type, house_number, section_number_real, rooms, prod  FROM  `psp_gen_plan_type` where house_number='$var' AND prod=0 AND rooms='$var4' ORDER BY `house_number` ASC" . $db_table_to_show)
    or die(mysql_error());	}
    elseif ($var!=0 && $var3!=0 && $var4==0){
    $qr_result = mysql_query("SELECT type_id, image_plan, section_type, house_number, section_number_real, rooms, prod  FROM  `psp_gen_plan_type` where house_number='$var' AND prod=0 AND section_type='$var3' ORDER BY `section_number_real` ASC " . $db_table_to_show)
    or die(mysql_error());	}
    elseif ($var==0 && $var3!=0 && $var4!=0){
    $qr_result = mysql_query("SELECT type_id, image_plan, section_type, house_number, section_number_real, rooms, prod  FROM  `psp_gen_plan_type` where rooms='$var4' AND prod=0 AND section_type='$var3' ORDER BY `house_number` ASC " . $db_table_to_show)
    or die(mysql_error());	}

    Кинули сайт на доработку. Вот с таким вот фильтром.

    tin, 22 Января 2015

    Комментарии (5)
  6. PHP / Говнокод #17495

    +156

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    public function fillRow($row, $table)
    {
        static $counter;
        global $app_list_strings, $locale;
    
        $counter++;
        $row['counter'] = $counter;
        $row['status_abc_current'] = $row['status_abc_current'];
        $row['status_abc_1_quarter_ago'] = $row['status_abc_1_quarter_ago'];
        $row['status_abc_2_quarter_ago'] = $row['status_abc_2_quarter_ago'];
        $row['status_abc_3_quarter_ago'] = $row['status_abc_3_quarter_ago'];
        $row['status_abc_4_quarter_ago'] = $row['status_abc_4_quarter_ago'];
    
        return $row;
    }

    что-то тут не так

    mr-lekafe, 22 Января 2015

    Комментарии (4)
  7. PHP / Говнокод #17482

    +158

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    $i = 0;
            for ($k = 0; $k<=5; $k++){
                if ($i==5)
                    break;
                foreach (getContent($id) as $content_row) {
                    $i++;
                    $htmlshowcase = $content_row->getShowcase(1, $k);
                    if ($htmlshowcase == '')
                        $i--;
                    else
                        $html .= $htmlshowcase;
                    if ($i==5)
                        break;
                }
            }

    Лучший способ прохода по циклу.

    arkham_vm, 20 Января 2015

    Комментарии (2)
  8. PHP / Говнокод #17480

    +152

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    <?
    require_once('ittable.class.php');
    
    class ITForm extends ITTable
    {
    public function __construct($tbl)
    {
    parent::__construct($tbl);
    
    //путь с скрипту который удаляет файлы по tblname record_id filename
    $this->path2deletephp='/itcms4/ckeditor/delete.php';
    }
    
    
    public function printForm($i, $action, $head, $submitvalue='', $captcha=0)
    {
    $form = $this->generateFormData($i, $action, $head, $submitvalue, $captcha);
    
    print "<h1>$form->head</h1>
    $form->begin
    $form->inputhidden
    
    <table>";
    
    foreach($form->inputs as $k=>$v)
     print "<tr id='itforms_$k'" . ($this->columns[$k]->blockclass?' class="' . $this->columns[$k]->blockclass . '"':'') ."><td>$v->name</td><td>$v->input</td></tr>\n";
     
    print "</table>
    
    $form->files
    
    
    <hr><input type='submit' value='$form->submitvalue'></form>
    
    <script type='text/javascript'>
    var itform;
    $(document).ready(function()
      {
       itform = new ITForm($('#$this->tbl$i->id'));
       $form->js
       });
    </script>
    ";
    }
    
    public function insertForm($head, $submitvalue='', $i=0)
    {
     if(!$i)
      $i = new stdClass();
      
     $copy_id = intval($_GET['copy_id']);
     if($copy_id)
     {
      $r=exec_mysql_query("SELECT * FROM $this->tbl WHERE id='$copy_id'");
      $row=mysql_fetch_array($r);
      foreach($row as $k=>$v)
       if(!isset($i->$k))
        $i->$k = $v;
     }
     //чтобы можно было загрузить файлы делаем временный id в диапазоне от 10^7 до 2*10^9
     $i->id = 'r' . rand(10000000, 2000000000);
     $this->printForm($i, 'insert.php', $head, $submitvalue);
    } 
    
    public function updateForm($id, $head, $submitvalue='')
    {
     $this->printForm($this->getObject($id), 'update.php',  $head, $submitvalue);
    
    if(mysql_numrows(exec_mysql_query("SELECT id FROM it_updatelog WHERE tbl='$this->tbl' AND record_id='$id'")))
     print "<a href=/crm2/log/?w_tbl_2=$this->tbl&w_record_id_1=$id>История изменений</a><br>"; 
    }
    
    public function deleteForm($id, $head, $submitvalue='')
    {
     $this->printForm($this->getObject($id), 'delete.php',  $head, $submitvalue);
    }
    
    public function updateForm4Object($o, $head, $submitvalue='')
    {
     $this->printForm($o, 'update.php',  $head, $submitvalue);
    
    if(mysql_numrows(exec_mysql_query("SELECT id FROM it_updatelog WHERE tbl='$this->tbl' AND record_id='$o->id'")))
     print "<a href=/crm2/log/?w_tbl_2=$this->tbl&w_record_id_1=$o->id>История изменений</a><br>"; 
    }

    Продолжение: http://govnokod.ru/17479

    itgovnokod, 19 Января 2015

    Комментарии (7)
  9. PHP / Говнокод #17479

    +152

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    96. 96
    97. 97
    98. 98
    <?
    require_once('ittable.class.php');
    
    class ITForm extends ITTable
    {
    public function __construct($tbl)
    {
    parent::__construct($tbl);
    
    //путь с скрипту который удаляет файлы по tblname record_id filename
    $this->path2deletephp='/itcms4/ckeditor/delete.php';
    }
    
    
    public function generateFormData($i, $action, $head, $submitvalue='', $captcha=0)
    {
    global $itcmscfg;
    $form->submitvalue=htmlentities($submitvalue?$submitvalue:$head, ENT_NOQUOTES, 'UTF-8');
    $form->tbl = $this->tbl;
    if($this->files) $enctype='enctype="multipart/form-data"';
    $form->head = htmlentities($head, ENT_NOQUOTES, 'UTF-8');
    $form->begin = "<form  name='$this->tbl$i->id' id='$this->tbl$i->id' action='$action' method='post' $enctype>";
    $form->inputhidden .= "<input type='hidden' name='id' value='$i->id'><input type='hidden' name='referer' value='" . ($_SERVER['HTTP_REFERER']?$_SERVER['HTTP_REFERER']:'./') . "'>\n";
    
    if($action=='delete.php') {$form->disabled = 'disabled'; $OPERATION = UPDATABLE;}
    elseif($action=='insert.php') $OPERATION = INSERTABLE;
    elseif($action=='update.php') $OPERATION = UPDATABLE;
    else $OPERATION = UPDATABLE; 
    
      foreach($this->columns as $field=>$ftype)
      {
       $itforms = " $form->disabled name='$field' id='input-$field'" . $this->getITFormsAttr($field);
       $value = isset($i->$field) ? $i->$field : ($_GET[$field] ? $_GET[$field]: $ftype->Default);
       if(!$ftype->htmlcode) $value = htmlentities($value, ENT_QUOTES, 'UTF-8');
       $itcmscfg->cfgfile = intval($itcmscfg->cfgfile);
       if($ftype->Key=='UNI') $itforms .= " data-async='false' data-unique='{cfgfile:$itcmscfg->cfgfile, tblname:\"$this->tbl\", record_id:\"$i->id\"}'";
       
       if($ftype->visibility & HIDDEN){$form->inputhidden .= "<input type='hidden' $itforms value='$value'>"; continue;}
        
        //если поле не RADONLY, то проверить задан ли regexp и разрешена ли операция
       if( !($ftype->visibility & READONLY) )
        if(!$ftype->pattern || !($ftype->visibility & $OPERATION))
         continue;
       
       $form->inputs[$field]->name = "<label for='input-$field' id='label-$field'>$ftype->outputname</label>"; 
       
       //выводим элементы
       if($ftype->options)
       {
        $form->inputs[$field]->input = $ftype->options->html($field, " $form->disabled " . $this->getITFormsAttr($field), $value);
        if($ftype->options->type==OT_BINARY_CHECKBOX)
         $form->js .= "itform.setCheckboxProperty('$field', 'sum', '$ftype->minselected', '$ftype->maxselected');\n";
       }
       elseif($ftype->inputtype=='wysiwyg')
         $form->inputs[$field]->input = "<script src=/include/ckeditor/ckeditor.js></script>
                                         <textarea class='ckeditor' name='$field'>$value</textarea>";
       elseif($ftype->Type=='text')
        $form->inputs[$field]->input = "<textarea rows='10' cols='80' $itforms " . ($form->disabled?' readonly':'') . ">$value</textarea>";
       elseif($ftype->inputtype)
         $form->inputs[$field]->input = "<input type='$ftype->inputtype' value='$value' $itforms>";
       else
        $form->inputs[$field]->input =  "<input type='text' value='$value' $itforms>";  
    
       $f1 = $ftype->func;
       if(isset($f1))
        $form->inputs[$field]->input .=  (' ' . $f1($value));
    
     }//foreach
    
    
    if($this->files)
    {
     foreach($this->files as $k=>$o)
     {
      $itforms = "";
      //это только для action==insert.php можно, при редактирвоании не обязательно файл заливать
      $itforms .= ($o->required && $action=='insert.php') ? " data-min-length='1'" : '';
      $itforms .= $o->validExtentions ? " data-regexp='$o->validExtentions'" : '';
      if($o->help) $itforms .= " data-help='$o->help'";
      if($o->multiple)
       {
        $itforms .= ' multiple';
        $multiple = '[]';
       }
       else
        $multiple='';
     
      $outputname = htmlentities($o->outputname, ENT_NOQUOTES, 'UTF-8');  
      $form->inputs[$k]->name = "<label for='input-$k'>$outputname</label>";
      $form->inputs[$k]->input = "<input type='file' name='$k$multiple' id='input-$k' $itforms>";
     }//foreach
    
     if(intval($i->id))
      $form->files = $itcmscfg->fs->getFiles4TblRecord($this->tbl, $i->id);
    }//if
    
    return $form;
    }//fun

    itgovnokod, 19 Января 2015

    Комментарии (10)
  10. PHP / Говнокод #17474

    +156

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    function watermark_image_name($path) {
        $i_path = explode('/', trim($path, ' /'));
        $i_name = $i_path[2] . '-' . $i_path[3];
        return $i_name;
    }

    Получает имя изображения из пути /path/to/image_name.jpg

    syrex88, 19 Января 2015

    Комментарии (0)