0
votes

Comment puis-je éviter d'utiliser trop d'IFS dans ce cas?

Cela fonctionne mais ne semble pas que la bonne façon de coder

Je n'ai aucune idée de quoi faire P>

foreach ($data as $tag) {
    if ($tag==1) echo "<a onclick=\"window.location.href='/buscatag/1'\">Coméda</a>,\n";
    if ($tag==2) echo "<a onclick=\"window.location.href='/buscatag/2'\">Ecchi</a>,\n";
    if ($tag==3) echo "<a onclick=\"window.location.href='/buscatag/3'\">Peitos grandes</a>,\n";
    if ($tag==4) echo "<a onclick=\"window.location.href='/buscatag/4'\">Nudez</a>,\n";
    if ($tag==5) echo "<a onclick=\"window.location.href='/buscatag/5'\">Seinen</a>,\n";
    if ($tag==6) echo "<a onclick=\"window.location.href='/buscatag/6'\">Violência<a/>,\n";
    if ($tag==7) echo "<a onclick=\"window.location.href='/buscatag/7'\">Vida Cotidiana</a>,\n";
    if ($tag==8) echo "<a onclick=\"window.location.href='/buscatag/8'\">Harém</>,\n";
    if ($tag==9) echo "<a onclick=\"window.location.href='/buscatag/9'\">Ação</a>,\n";
    if ($tag==10) echo "<a onclick=\"window.location.href='/buscatag/10'\">Shonen</a>,\n";
    if ($tag==11) echo "<a onclick=\"window.location.href='/buscatag/11'\">Super Poderes</a>,\n";
    if ($tag==12) echo "<a onclick=\"window.location.href='/buscatag/12'\">Aventura</a>,\n";
    if ($tag==13) echo "<a onclick=\"window.location.href='/buscatag/13'\">Fantasia</a>,\n";
    if ($tag==14) echo "<a onclick=\"window.location.href='/buscatag/14'\">Loli</a>,\n";
}


0 commentaires

3 Réponses :


1
votes

Lorsque vous allez tester la même variable plusieurs fois, vous pouvez utiliser un commutateur pour le rendre meilleur:

foreach ($data as $tag) {
    switch ($tag) {
        case 1:
            echo "<a onclick=\"window.location.href='/buscatag/1'\">Coméda</a>,\n";
            break;
        case 2:
            echo "<a onclick=\"window.location.href='/buscatag/2'\">Ecchi</a>,\n";
            break;
    }
}


0 commentaires

2
votes

dans le code, vous pouvez immédiatement mettre le numéro dans une balise

Essayez xxx


3 commentaires

En fait mieux pour cette situation que ce que j'avais proposé


Et cela résout quoi? Comment vous distinct comeda , ECCHI etc.?


Votre tableau de noms est 0 indexé et les balises commencent par 1. Il ne correspondra pas



4
votes
// Define $lookup as 
$lookup = [1 => 'Coméda', 2 => 'Ecchi',]; // etc
// Note the relation between key and value

foreach ($data as $tag) {
    echo "<a onclick=\"window.location.href='/buscatag/{$tag}'\">" . $lookup[$tag] . "</a>,\n";
}

0 commentaires