Good day! I have this question. How can I optimize the code presented below (I hope for a weak court, since I’m not engaged in a long time) code.

Made a site on Codeigniter that was required. Print the data from 2 tables (yes, the tables are not brought to 3nf, because the base was made before me and I was not given time to work out the base properly, but only want to increase the functionality). And upon request, 4 mandatory fields were made, which are always displayed and 26 optional, which were made by checkboxes (this is terrible) and now I get this code on the output.

<tr> <th> Станция назначение </th> <th> Станция операции </th> <th> Номер вагона </th> <th> Дата </th> <th> Время </th> <? if($op == "yes"):?> <th> ОП </th> <? endif;?> <? if($dpr == "yes"):?> <th> ДПР </th> <? endif;?> <? if($inpoezd == "yes"):?> <th> Индекс поезда </th> <? endif;?> <? if($gruz == "yes"):?> 

etc.

 <? foreach ($disla as $item):?> <? $x = $item->naznachenie; $q = $item->operation; for($i=0; $i< count($item->naznachenie); $i++) { if ($x != $y) { echo "<tr><td>".$x."</td><td></td></tr>"; $t = true; $y = $x; } else { $t = false; } } for ($i=0; $i< count($item->operation); $i++) { if ($q != $w or $t == true) { echo "<tr><td></td><td>".$q."</td></tr>"; $w = $q; } } ?> <tr> <td></td> <td></td> <td><a href="#" style='text-decoration: none;' id="<?=$item->number_vagona?>" onclick="TellId(this)"><?=$item->number_vagona?></a></td> <? $date_out = date("dmY", strtotime($item->date)); if(strtotime($date11) == strtotime($date_out) or strtotime($date1) == strtotime($date_out) and ($item->time) > $time) { echo "<td style='color: #3caa3c;'><strong>".$date_out."</strong></td>"; } elseif(strtotime($date_out) <= strtotime($date5) and strtotime($date_out) > strtotime($date10)) { echo "<td style='color: orange;'><strong>".$date_out."</strong></td>"; } elseif(strtotime($date_out) <= strtotime($date10)) { echo "<td style='color: red;'><strong>".$date_out."</strong></td>"; } else echo "<td><strong>".$date_out."<strong></td>"; ?> <td><?=$item->time ?></td> <? if($op == "yes"):?> <td><?=$item->op ?></td> <? endif;?> <? if($comment == "yes"):?> <td><input onchange="uodate(<?=$item->number_vagona ?>,'comment',this)" type="text" value="<?=$item->comment ?>" id="noborder"/></td> <? endif;?> 

when displaying, the first two columns are compared, the date is displayed in color.

I of course cut it down very much, imagine 40 columns for the choice of the user. everything seems to be working, but the speed of work is just awful. I understand that in many ways I did wrong. Please write remarks where what can be done better? Thank you very much in advance!

  • Give a little advice! You need to embed html in php and not php in html if you are not using a template engine! ----------------------- This is terrible <td> <? = $ Item-> time?> </ Td> <? if ($ op == "yes"):?> <td> <? = $ item-> op?> </ td> <? endif;?> doesn't it seem so? - Palmervan
  • yes, right, thanks. - kira
  • just when it all started it was not calculated for such a volume there were 6 columns that were quietly displayed. - kira

1 answer 1

in Codeigniter did not work, but we only know this framework in which the MVC pattern is implemented. If you do not know it, then study ahead, otherwise you will not be able to effectively use this and other frameworks. Ie You want to say that this is a piece of code related exclusively to the presentation?

If you have so many optional fields, I would advise you to look in the direction of using the EAV (Entity-Attribute-Value) model you can read here

  • Well, I studied it, and everything was done according to the MVC model, as soon as it was possible (in my view), this piece of code represents the output of the table. And I ask a specific question, how do I optimize my output considering the number of columns, and that the user can adjust them by checkboxes. - kira
  • Thanks, for the link I will study. - kira