How to optimize the writing of multiple ?

WBOY
Release: 2023-03-03 09:22:01
Original
1519 people have browsed it

Multiple:

<code><?php if ( in_category('10') ) echo'<p class="class1"><span class="icon-home"></span></p>'; ?>
<?php if ( in_category('25') ) echo'<p class="class2"><span class="icon-cat"></span></p>'; ?>
<?php if ( in_category('38') ) echo'<p class="class3"><span class="icon-dog"></span></p>'; ?>
<?php if ( in_category('44') ) echo'<p class="class4"><span class="icon4"></span></p>'; ?>
<?php if ( in_category('50') ) echo'<p class="class5"><span class="icon5"></span></p>'; ?>
<?php if ( in_category('61') ) echo'<p class="class6"><span class="icon6"></span></p>'; ?>
</code>
Copy after login
Copy after login

How to optimize writing? Thank you

* Edited again, in_category and the following class are actually irregular.

Reply content:

Multiple:

<code><?php if ( in_category('10') ) echo'<p class="class1"><span class="icon-home"></span></p>'; ?>
<?php if ( in_category('25') ) echo'<p class="class2"><span class="icon-cat"></span></p>'; ?>
<?php if ( in_category('38') ) echo'<p class="class3"><span class="icon-dog"></span></p>'; ?>
<?php if ( in_category('44') ) echo'<p class="class4"><span class="icon4"></span></p>'; ?>
<?php if ( in_category('50') ) echo'<p class="class5"><span class="icon5"></span></p>'; ?>
<?php if ( in_category('61') ) echo'<p class="class6"><span class="icon6"></span></p>'; ?>
</code>
Copy after login
Copy after login

How to optimize writing? Thank you

* Edited again, in_category and the following class are actually irregular.

<code>for($i=1; $i<7; $i++) {
    if(in_category($i)) {
        echo "<p class=\"class{$i}\"></p>";
    }
}</code>
Copy after login

I don’t have much insights into code optimization, I’ll leave it to other friends to add


Update:
Optimized again

<code>$arr = array(
    10=>'<p class="class1"><span class="icon-home"></span></p>',
    25=>'<p class="class2"><span class="icon-cat"></span></p>', 
    38=>'<p class="class3"><span class="icon-dog"></span></p>', 
    44=>'<p class="class4"><span class="icon4"></span></p>', 
    50=>'<p class="class5"><span class="icon5"></span></p>', 
    61=>'<p class="class6"><span class="icon6"></span></p>'
);
function inCatToHtml($arr) {
    while (list($key, $value) = each($arr)) {
        if(in_category($key)) {
            echo $arr[$key];
        }
    }
}
inCatToHtml($arr);</code>
Copy after login

echo can be optimized according to the actual situation, and finally returns an in_category array HTML, and then processes it uniformly

<code><?php

foreach(range(1,6) as $categoryNum) {
    if(in_category($categoryNum)) {
        echo '<p class="class"'.$categoryNum.'"></p>';
    }
}</code>
Copy after login

But I feel that mixing php in html is very low...

Recommended

  1. php template engine.

  2. Front-end single page or a simple js engine.

======== Phew, the author of the question has changed it, I will also change it ==========

<code><?php

$categoryIcon = [
    10 => 'icon-home',
    ....
];

具体就不写了...和上面一样。</code>
Copy after login

<code><?php
 
$num = count(['1','2','3','4','5','6']);

for( $i=0; $i<$num; $i++ )
{
    if(in_category($num[$i]))
    {
        echo "<p class=class".$i."></p>";
    }
}
?></code>
Copy after login

<code><?php if ($currentCategory = Category::currentCategory()): ?>
<p class="<?=$currentCategory->labelClass?>"><span class="<?=$currentCategory->iconClass?>"></span></p>
<?php endif; ?></code>
Copy after login

The presentation layer only cares about performance. As for how many categories there are in total, how to determine the category of the current page, and what the class of each category is, whether you plan to put a DB or write an array configuration, they are all encapsulated into the Category class. Go inside.

With so much written above, I still feel that the original writing method of the original poster is clear at a glance

I think it is more appropriate for the poster to directly use switch for the multiple if judgment conditions. I don’t know what the code in the in_category method of the original poster looks like, so just boldly change this method. Use switch in the method:

<code>function in_category( $num ) {
    switch ($num) {
        case '10':
            echo'<p class="class1"><span class="icon-home"></span></p>';
            break;
        case '25':
            echo'<p class="class2"><span class="icon-cat"></span></p>';
            break;
        case '38':
            echo'<p class="class3"><span class="icon-dog"></span></p>';
            break;
        case '44':
            echo'<p class="class4"><span class="icon4"></span></p>';
            break;
        default:
            echo '';
            break;
    }
}</code>
Copy after login

Then directly in_category(10)Is it okay?

Assume that the questioner is trying to determine which category the article belongs to and output an ICON
Then you can first define the category,

<code class="php">$categroies = [
    10=>['class'=>'icon-home'],
    25=>['class'=>'icon-dog'],
    38=>['class'=>'icon-cat'],
    44=>['class'=>'icon4']
    //当然这里的数据可以是从数据库里封装好的
];
//等待输出的html
$html = '';
$class_i = 1;
foreach($categories as $key => $category) {
    if (in_category($key)) {
        $html .= '<p class="class' . ($class_i++) . '"><span class="' . $category['class'] . '"></span></p>';
    }
}
echo $html;</code>
Copy after login

I don’t know the purpose of in_category here. If it is to determine whether an article is in a certain category and needs to traverse all categories, this method is quite illogical.

Separation of front and back ends is the best

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!