I have frequently repeated html code. Is there any way to simplify this with PHP variables? Or something else.

<tr><td height="20" class="g1"><div class="m1">РАЗДЕЛ1</div></td></tr> <tr><td width="160"><a target="_top" href="/pages1.php">Страница1</a></td></tr> <tr><td width="160"><a target="_top" href="/pages2.php">Страница2</a></td></tr> <tr><td width="160"><a target="_top" href="/pages3.php">Страница3</a></td></tr> <tr><td width="160"><a target="_top" href="/pages4.php">Страница4</a></td></tr> <tr><td width="160"><a target="_top" href="/pages5.php">Страница5</a></td></tr> <tr><td width="160"><a target="_top" href="/pages6.php">Страница6</a></td></tr> <tr><td width="160"><a target="_top" href="/pages7.php">Страница7</a></td></tr> <tr><td width="160"><a target="_top" href="/pages8.php">Страница8</a></td></tr> 

There is the same code - repeating.

 <tr><td width="160"><a target="_top" href="/ 

How can this be improved? Thank.

  • Not plowed field for work;) How do you want to optimize it, reduce the code, or generate it all the same? - jkeks
  • Generate will not be easy. The fact is that there are more sections, each section has a different number of pages. I thought to generate, but for now it is too difficult, but possible. First, reduce the code. Then I will try to generate - if possible :) - Monstrs-Inc
  • <tr> <td> <a href="/pages1.php"> Page1 </a> </ td> </ tr> It’s not enough to shrink it, what’s removed is put into styles. There is another option to generate a link from JS, then it will be like this: <tr> <td> Page1 </ td> </ tr> The link will not be indexed. And the last option on the go is to generate a table via JS code. Then the code as such will not be initially available at all, and it will be generated the same on the fly, but it will be generated. It all depends then on your desire to understand some jQuery or Moo. - jkeks
  • So it will not be easier. I was thinking of using PHP variables for repeating html code, and using an array for the link and page title. But I did not succeed. This file is called menu.php, created for it config.php and loaded variables, but for some reason it gives a syntactic error on the line include './config.php'; - Monstrs-Inc
  • Here I conjured and got this code: $ k = 0; $ i = 0; $ n = 'null'; while ($ A [$ k] [$ i]! = $ n): {while ($ A [$ k] [$ i]! = $ n): echo $ A [$ k] [$ i]; $ i ++; endwhile; } $ k ++; $ i = 0; endwhile; - Monstrs-Inc

2 answers 2

There is a line in PHP: <tr> <td width = "160"> <a target="_top" href="/pages1.php"> Page1 </a> </ td> </ tr> Instead of changing elements, insert Tags I came up with such {% a%}:

 <tr><td width="160"><a target="_top" href="/pages{% a %}.php">Страница{% a %}</a></td></tr> 

Then how much do you need to bring them? 100 ? Writing a cycle .. 1..100

 for ($i=1;$i<=100;$i++) 

And instead of labels we insert the variable $ i

 for ($i=1;$i<=100;$i++) echo '<tr><td width="160"><a target="_top" href="/pages'.$i.'.php">Страница'.$i.'</a></td></tr>'; 

that's all. Now for the rest of the sections is similar. I wrote a lot of superfluous and repeated something, just to understand it might be better.

    If this is a php template - something like this:

     <?for($i=1;$i<=8;++$i):?> <tr> <td width="160"> <a target="_top" href="/pages<?=$i?>.php"> Страница<?=$i?> </a> </td> </tr> <?endfor?> 
    • And if you also shove a string into a variable, like $ str = '<tr> <td width = "160"> <a target = "_ top" href = "/'; Won't it be easier? - Monstrs-Inc
    • I think that everything that can be output without php - should be output without it, in fact, this is why the answer looks like this ... - Zowie
    • I'm not familiar with other languages, I'm starting to learn PHP, and naturally the website written in HTML is being rewritten in PHP. The idea was to create loops in the menu.php file using arrays, and in cofig.php enter all variables with html code and arrays where the menu would be written. For example, for the first section, use the array <? Php // Multidimensional $ A ["menu"] = array ("razd" ​​=> "machines", "0" => "p1" "1" => "p2" "8 "=>" null "); ?> etc. - Monstrs-Inc
    • This is not a multi-dimensional array. - Sh4dow
    • there must be another column with the name of the files - the characters did not fit. - Monstrs-Inc