Hello! I am new to php, I will hope for your prudence. There is a code:

<? if ( (($match['live']) == 1 && !empty($match['english'])) || (($match['live']) == 1 && !empty($match['russian'])) || (($match['live']) == 1 && !empty($match['polish'])) || (($match['live']) == 1 && !empty($match['bulgarian'])) || (($match['live']) == 1 && !empty($match['french'])) ){ echo '<div id="stream"> <div class="tabselect" > <div class="tab" style="width: 100%; border-radius: 0px 0px 5px 5px; position: relative; z-index: 10;"> English Stream <div class="submenu" style="width: 810px;"> <a class="tab" style="width: 100%;text-align:center; padding:0; margin:0;" '.$match['id'].' ,Bulgarian)">Bulgarian Stream</a> <a class="tab" style="width: 100%;text-align:center; padding:0; margin:0;" '.$match['id'].' ,French)">French Stream</a> <a class="tab" style="width: 100%;text-align:center; padding:0; margin:0;" '.$match['id'].' ,Russian)">German Stream</a> <a class="tab" style="width: 100%;text-align:center; padding:0; margin:0;" '.$match['id'].' ,Polish)">Polish Stream</a> </div> </div> </div>' ; }elseif ( (($match['live']) == 1 && !empty($match['english'])) || (($match['live']) == 1 && !empty($match['russian'])) || (($match['live']) == 1 && !empty($match['polish'])) || (($match['live']) == 1 && !empty($match['bulgarian'])) || (($match['live']) == 1 && !empty($match['french'])) && (!empty($match['english'])) { echo '<div id="mainstream" style="position: relative; z-index:5;"> <div class="video-container"> <iframe src="//player.twitch.tv/?channel='.$match['english'].'&autoplay=false" height="480" width="832" frameborder="0" scrolling="no" allowfullscreen="" style="display: inline-block;"></iframe> </div> <iframe frameborder="1" scrolling="no" id="chat_embed" src="//www.twitch.tv/'.$match['english'].'/chat?popout=" height="550" width="100%" style="display: inline-block; z-index:10; position: relative; float: right;"></iframe> </div> ' ;}elseif ( (($match['live']) == 1 && !empty($match['english'])) || (($match['live']) == 1 && !empty($match['russian'])) || (($match['live']) == 1 && !empty($match['polish'])) || (($match['live']) == 1 && !empty($match['bulgarian'])) || (($match['live']) == 1 && !empty($match['french'])) && (!empty($match['russian'])) { echo '<div id="mainstream" style="position: relative; z-index:5;"> <div class="video-container"> <iframe src="//player.twitch.tv/?channel='.$match['russian'].'&autoplay=false" height="480" width="832" frameborder="0" scrolling="no" allowfullscreen="" style="display: inline-block;"></iframe> </div> <iframe frameborder="1" scrolling="no" id="chat_embed" src="//www.twitch.tv/'.$match['russian'].'/chat?popout=" height="550" width="100%" style="display: inline-block; z-index:10; position: relative; float: right;"></iframe> </div> ' ;}elseif ( (($match['live']) == 1 && !empty($match['english'])) || (($match['live']) == 1 && !empty($match['russian'])) || (($match['live']) == 1 && !empty($match['polish'])) || (($match['live']) == 1 && !empty($match['bulgarian'])) || (($match['live']) == 1 && !empty($match['french'])) && (!empty($match['polish'])) { echo '<div id="mainstream" style="position: relative; z-index:5;"> <div class="video-container"> <iframe src="//player.twitch.tv/?channel='.$match['polish'].'&autoplay=false" height="480" width="832" frameborder="0" scrolling="no" allowfullscreen="" style="display: inline-block;"></iframe> </div> <iframe frameborder="1" scrolling="no" id="chat_embed" src="//www.twitch.tv/'.$match['polish'].'/chat?popout=" height="550" width="100%" style="display: inline-block; z-index:10; position: relative; float: right;"></iframe> </div> ' ;}elseif ( (($match['live']) == 1 && !empty($match['english'])) || (($match['live']) == 1 && !empty($match['russian'])) || (($match['live']) == 1 && !empty($match['polish'])) || (($match['live']) == 1 && !empty($match['bulgarian'])) || (($match['live']) == 1 && !empty($match['french'])) && (!empty($match['french'])) { echo '<div id="mainstream" style="position: relative; z-index:5;"> <div class="video-container"> <iframe src="//player.twitch.tv/?channel='.$match['french'].'&autoplay=false" height="480" width="832" frameborder="0" scrolling="no" allowfullscreen="" style="display: inline-block;"></iframe> </div> <iframe frameborder="1" scrolling="no" id="chat_embed" src="//www.twitch.tv/'.$match['french'].'/chat?popout=" height="550" width="100%" style="display: inline-block; z-index:10; position: relative; float: right;"></iframe> </div> ' ;}elseif ( (($match['live']) == 1 && !empty($match['english'])) || (($match['live']) == 1 && !empty($match['russian'])) || (($match['live']) == 1 && !empty($match['polish'])) || (($match['live']) == 1 && !empty($match['bulgarian'])) || (($match['live']) == 1 && !empty($match['french'])) && (!empty($match['bulgarian'])) { echo '<div id="mainstream" style="position: relative; z-index:5;"> <div class="video-container"> <iframe src="//player.twitch.tv/?channel='.$match['bulgarian'].'&autoplay=false" height="480" width="832" frameborder="0" scrolling="no" allowfullscreen="" style="display: inline-block;"></iframe> </div> <iframe frameborder="1" scrolling="no" id="chat_embed" src="//www.twitch.tv/'.$match['bulgarian'].'/chat?popout=" height="550" width="100%" style="display: inline-block; z-index:10; position: relative; float: right;"></iframe> </div> ' ;} '</div>'; } ?> 

It must be converted to the choseStream() function, which will contain the values: <?match['id']?> And <?match['english, russian, bulgarian, french etc'] type choseStream($match['id'], $match['english']) and so that when calling the onclick function, the stream is loaded, for example:

 `<a class="tab" style="width: 100%;" onclick="choseStream(<?=$match['id']?>,<?=$match['english']?>)">English Stream</a> <a class="tab" style="width: 100%;" onclick="choseStream(<?=$match['id']?>,<?=$match['russian']?>)">Russian Stream</a> <a class="tab" style="width: 100%;" onclick="choseStream(<?=$match['id']?>,<?=$match['french']?>)">French Stream</a> <a class="tab" style="width: 100%;" onclick="choseStream(<?=$match['id']?>,<?=$match['bulgarian']?>)">Bulgarian Stream</a>` 

Closed due to the fact that the question is too general for the participants rjhdby , Vadim Ovchinnikov , user194374, fori1ton , aleksandr barakin 18 Jan '17 at 21:32 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Separate logic from presentation, google MVC - korytoff

1 answer 1

Since you generate text, there is a need to reduce it, the reduction can be obtained in three ways:

  1. Use automatic substitution algorithm
  2. Use loops
  3. Use templates

The first method is used if there is a constant pattern of change. For example, we have the following text:

 word1 word2 word3 word1 word2 word3 

As it is easy to guess, we have the ending for word word from 1 to 3, and this combination is repeated cyclically two times. So we have an algorithm:

 СТАРТ АЛГОРИТМА менять окончание от 1 до 3 повторить 1 раз КОНЕЦ АЛГОРИТМА 

The second method is used when the text is repeated. For example, we have such a text.

 word_word word_word word_word word_word 

Instead of printing the same thing four times, we can use cycles:

 СТАРТ АЛГОРИТМА запуск цикла который напечатает слово word_word четыре раза КОНЕЦ АЛГОРИТМА 

But here it is necessary to understand that during repetition it happens so that only some elements change. For example, we have the following text:

 word_(a)_word_(b) word_(c)_word_(d) word_(e)_word_(f) 

Here you can apply a template with the parameter / mi (this is the third option):

 ШАБЛОН ПАРАМЕТР1 ПАРАМЕТР2 word_(ПАРАМЕТР1)_word_(ПАРАМЕТР2) КОНЕЦ ШАБЛОНА 

Now this template can be run three times with different parameters:

 ШАБЛОН(a, b) ШАБЛОН(c, d) ШАБЛОН(e, f) 

As you can see from the example, the code is significantly reduced.

It is necessary to add that each of the above examples is easy to implement as ordinary functions.