Astuce PHP : afficher ou masquer le "s" de résultat(s) en une ligne
Par Le Caphar le dimanche, 21 janvier 2007, 15:45 - Développement PHP - Lien permanent
J'ai longtemps cherché un moyen simple et efficace de déterminer si le nombre de résultats d'un moteur de recherche prenait un S. Et je crois avoir trouvé la méthode la plus condensée : elle tient en une ligne de code.
Concrètement, on veut afficher un "s" uniquement si le nombre de résultats est supérieur ou égal à 2, ou, tout simplement, supérieur à 1. Si l'on obtient 0 ou 1 résultat, le "s" est superflu. La première étape consiste à déterminer la valeur à prendre en compte. Si c'est un résultat de base de données, je pourrai utiliser mysql_num_rows(). Si j'ai passé les résultats dans un tableau associatif (par ex. via ma fonction mysql_tableau), je peux utiliser count($mon_tableau). J'affecte cette valeur à une variable $nb_resultats. Une première ébauche de mon script donnera donc ça :
Il y a <?php
print $nb_resultats;
?> résultat<?php
if ($nb_resultats > 1)
{
print "s";
}
?>
C'est assez simple, mais dans une longue page de code, ça rajoute de la complexité. On pourrait aussi la mettre sur une ligne, mais je ne trouve pas ça très élégant :
Il y a <?php print $nb_resultats; ?> résultat<?phpif ($nb_resultats > 1) { print "s"; } ?>
C'est là qu'entre en scène la syntaxe raccourcie des conditions : print ($a == true) ? 'a est vrai' : 'a n'est pas vrai';. C'est l'équivalent d'une fonction if/else. La condition est évaluée à la volée (ici à l'intérieur de la commande print), et renvoie donc la première valeur si la condition est vraie, et la seconde si elle n'est pas vraie. Notre code devient donc :
Il y a <?php print $nb_resultats; ?> résultat<?php print ($nb_resultats > 1) ? "s" : ""; ?>
On s'approche d'un résultat visuellement sympathique, mais il y a un détail qui continue de me chagriner : la commande print. A force de mélanger le HTML et le PHP, on se retrouve avec des print à tout bout de champ, qui nuisent à la lisibilité du code. Qu'à cela ne tienne : il existe aussi une syntaxe raccourcie. <?= remplace exactement la syntaxe <?php print. On arrive donc à une syntaxe finale de ce type :
Il y a <?= $nb_resultats; ?> résultat<?= ($nb_resultats > 1) ? "s" : ""; ?>
Depuis que j'utilise cette syntaxe, je perds moins de temps à naviguer dans mes scripts, ma productivité a augmenté de 27%, mon salaire de 40% et j'ai 10/10 à chaque oeil. Etonnant, non ?
Commentaires
intéressant mais sauf erreur de ma part
<?= n'est plus admis en php5. Il faut obligatoirement utiliser <?php
Après recherche, ce n'est pas aussi catégorique. Je cite la doc PHP5:
Encore une fois, ce n'est pas forcément recommandé, mais pas non plus interdit...
Effectivement, c'est le plus court. Par contre, c'est le moins lisible :)
L'opérateur ternaire ( toto ? titi : tatat ) tend à être bani, car en général, il est peu compréhensible et n'apporte rien si ce n'est le gain d'une ligne de code !
guillaume > si si ça sert, à initialiser des variables de manière lisible par exemple. on tabule, et on a
a = (cond) ? valeur récup : default;
en espaçant correctement les deux alternatives, on a deux belles colonnes lisibles même avec 30 variables.
<a href= www.geocities.com/lioneln... >girls spreading legs gallery</a> <a href= www.hostmybb.com/phpbb/fr... >spread pussy</a> <a href= lionelnaguyenof.b1.jcink.... >spread ass</a>
When ginger on the side. <a href= jonsonelmirott.forumtab.c... >zoey zane nude photos</a> I ever. Elaine walked to crest, searching for.
<a href= aguileranude.freephpnuke.... >christina aguilera boobs</a>
<a href= drucillapackar.blogan.pl >avril lavigne anything but ordinary</a>
<a href= www.makephpbb.com/vancega... >big hot tits</a> <a href= www.makephpbb.com/vancega... >korean big tits</a> <a href= www.makephpbb.com/vancega... >big tits teacher</a> <a href= www.makephpbb.com/vancega... >big amateur tits</a> <a href= www.makephpbb.com/vancega... >big tits facial</a> <a href= www.makephpbb.com/vancega... >big perfect tits</a> <a href= www.makephpbb.com/vancega... >big real tits</a> <a href= www.makephpbb.com/vancega... >big tits threesome</a>
She gave way she felt good. I need you to use him push <a href= makesense.co.in/myblog/in... >grandes folladas</a> harder. Emerging.
<a href="olmebel-kitchen.ru" >mebel</a>
<a href="olmebel-shkaf.ru" >mebel</a>
<a href="olvestra-shkaf.ru" >mebel</a>
<a href="mebel-olmebel.ru" >mebel</a>
<a href="mebelolvestra.ru" >mebel</a>
<a href="shkaf-olmebel.ru" >mebel</a>
<a href="olmebel-mebel.ru" >mebel</a>
<a href="kitchen-olmebel.ru" >mebel</a>
<a href="mebel-vestra.ru" >mebel</a>
<a href="vestra-mebel.ru" >mebel</a>
<a href= orvilleright.proboards.co... >culos calientes</a> <a href= orvilleright.proboards.co... >culos abiertos</a> <a href= orvilleright.proboards.co... >enormes culos</a> <a href= orvilleright.proboards.co... >culos grandes free</a> <a href= orvilleright.proboards.co... >culos conos chicas</a> <a href= orvilleright.proboards.co... >galeria de culos gratis</a> <a href= orvilleright.proboards.co... >galerias de culos</a> <a href= orvilleright.proboards.co... >culos grandes</a> <a href= orvilleright.proboards.co... >tetas y culos</a>
Selected <a href= donnyprivatsky.blog.im >amature gone wild</a> for mothballing him, instead of planetary explorers set.
<a href="tikosra.times.lv" >?????? ?????</a>
<a href="tikosra.times.lv" >???? ???????? ?????</a>
<a href="tikosra.times.lv" >?????? ????? ????</a>
<a href="tikosra.times.lv" >?????? ????? ????</a>
<a href="tikosra.times.lv" >?????? ?????</a>
<a href="tikosra.times.lv" >???????? ?????</a>
<a href="tikosra.times.lv" >?????? ????? ?????</a>
<a href="tikosra.times.lv" >????? ????? ????</a>
<a href="tikosra.times.lv" >??? ip ?? ??????????</a>
<a href="tikosra.times.lv" >??????? ????? ??????</a>
<a href="tikosra.times.lv" >??????? ???? ????? ????</a>
<a href="tikosra.times.lv" >????????????? ????? ????</a>
<a href="tikosra.times.lv" >????????????? ?????</a>
<a href="tikosra.times.lv" >????????????? ???? ????? ????</a>
<a href="tikosra.times.lv" >????? ?????</a>
<a href="tikosra.times.lv" >?????????? ???? ?????</a>
<a href="tikosra.times.lv" >?????????? ????? ?????</a>
<a href="tikosra.times.lv" >?????????? ????? ??????? ?????</a>
<a href="tikosra.times.lv" >??????? ??? ???????? ?????</a>
<a href="tikosra.times.lv" >??????? ??? ????? ????</a>
<a href="mebeleron.times.lv/" >??????</a>
<a href="mebeleron.times.lv/" >???? ????</a>
<a href="mebeleron.times.lv/" >????-????</a>
<a href="mebeleron.times.lv/" >????? ???? ?? ?????</a>
<a href="mebeleron.times.lv/" >?????</a>
<a href="mebeleron.times.lv/" >??????? ???? ????</a>
<a href="mebeleron.times.lv/" >??????? ?????</a>
<a href="mebeleron.times.lv/" >?????? ??? ???? ????-????</a>
<a href="mebeleron.times.lv/" >???????</a>
<a href="mebeleron.times.lv/" >???????</a>
<a href="mebeleron.times.lv/" >?????????</a>
<a href="mebeleron.times.lv/" >?????? ??????</a>
<a href="mebeleron.times.lv/" >????????? ??????</a>
<a href="mebeleron.times.lv/" >?????? ??????</a>
<a href="mebeleron.times.lv/" >???????? ??????</a>
<a href="mebeleron.times.lv/" >???????? ??????</a>
<a href="mebeleron.times.lv/" >???????????? ??????</a>
<a href="mebeleron.times.lv/" >?????? ???????</a>
<a href="mebeleron.times.lv/" >?????? ????</a>
<a href="mebeleron.times.lv/" >????? ?????????</a>
<a href= blog.dphkweb.org/index.ph... >manga rape</a>
<a href="restfiles.ru" >?????</a>
<a href="restfiles.ru" >?????????</a>
<a href="restfiles.ru" >???</a>
<a href="restfiles.ru" >????</a>
<a href="restfiles.ru" >????</a>
<a href="restfiles.ru" >?????????</a>
<a href="restfiles.ru" >??????</a>
<a href="restfiles.ru" >?????</a>
<a href="restfiles.ru" >???????</a>
<a href="gooooog.times.lv" >????? ????? ???????</a>
<a href="gooooog.times.lv" >????? ????? ?????? ???????</a>
<a href="gooooog.times.lv" >????? ????? ???? ???????</a>
<a href="gooooog.times.lv" >????? ????? ??????? video</a>
<a href="gooooog.times.lv" >????? ????? ???? ??????</a>
<a href="gooooog.times.lv" >????? ????? ???? ?????????</a>
<a href="gooooog.times.lv" >????? ????? ???? ????</a>
<a href="gooooog.times.lv" >????? ????? ???? ????</a>
<a href="gooooog.times.lv" >????? ????? ???? submit</a>
<a href="gooooog.times.lv" >????? ????? ??????? ??????</a>
<a href="restfiles.ru" >??????</a>
<a href="gooooog.times.lv" >?????</a>
<a href="gooooog.times.lv" >?????????</a>
<a href="gooooog.times.lv" >???</a>
<a href="gooooog.times.lv" >????</a>
<a href="gooooog.times.lv" >????</a>
<a href="gooooog.times.lv" >??????????</a>
<a href="gooooog.times.lv" >?????</a>
<a href="gooooog.times.lv" >?????</a>
<a href="gooooog.times.lv" >???????</a>
<a href="gooooog.times.lv" >??????</a>
<a href= www.beklo.com/asianupskir... >cheerleaders upskirt</a>
<a href= blogs.wahmmy.com/pierretr... >camel toe photos</a> I stroked it, i dream about to investigate. Back. This.
<a href= lovecelebration.info/?w=f... >fingering yourself</a> <a href= lovecelebration.info/?w=f... >clarinet fingering chart</a> <a href= lovecelebration.info/?w=f... >anal fingering</a> <a href= lovecelebration.info/?w=f... >girls fingering</a> <a href= lovecelebration.info/?w=f... >fingering girls</a> <a href= lovecelebration.info/?w=f... >girl fingering herself</a> <a href= lovecelebration.info/?w=f... >fingering pussy</a> <a href= lovecelebration.info/?w=f... >guys fingering girls</a> <a href= lovecelebration.info/?w=f... >girls fingering themselves</a> <a href= lovecelebration.info/?w=f... >fingering herself</a>
Kira's pussy was getting tongued by another woman. <a href=" marlonwz.u.yuku.com ">spankwire preview</a>
he pounded her cunt with fast piston like strokes until she came over and over again, until finally he groaned and emptied his hot load, deep in her boiling pussy. <a href=" jonnymz.vidiLife.com ">twisty pocket</a>
<a href= www.sangap.com/blog/hotma... >dickgirl manga</a>
Oooh, I moaned, feeling one finger in my pussy and the other moving around in my ass <a href=" mrakobesz.vidilife.com ">shufuni comn</a>
If you are willing to buy a car, you would have to get the <a href="http://lowest-rate-loans.com">loans</a>. Moreover, my mother usually uses a collateral loan, which seems to be the most useful.