waplet Posted February 13, 2010 Report Share Posted February 13, 2010 Kā ir labāk taisīt pārslēdzēju starp lapā? 1. ar if($_GET['p'] == "news"){include "news.php"} elseif(.....){} vai 2. switch :? Link to comment Share on other sites More sharing options...
Web Developer Posted February 13, 2010 Report Share Posted February 13, 2010 Switch, protams. Link to comment Share on other sites More sharing options...
anonīms Posted February 13, 2010 Report Share Posted February 13, 2010 Web Developer, kādēļ? Link to comment Share on other sites More sharing options...
2easy Posted February 13, 2010 Report Share Posted February 13, 2010 man patīk if, tāpēc ka (protams, jūs jau visi nojaušat), ka tā ir īsāk :D:D:D salīdziniet paši: if ($_GET['p'] == 'about' { ... } if ($_GET['p'] == 'news' { ... } if ($_GET['p'] == 'contact' { ... } switch ($_GET['p']) { case 'about': ... break; case 'news': ... break; case 'contact': ... break; } 119 baiti vs 133 baiti (un 9 rindiņas vs 11 rindiņas) ok tā atšķirība nav liela, taču if man patīk arī ar to, ka tas ir more lightweight. tipa nevajag nekādas masīvas case/break struktūras, bet ir vnk {} Link to comment Share on other sites More sharing options...
anonīms Posted February 13, 2010 Report Share Posted February 13, 2010 (edited) tieši tā 2easy. + Lai atvieglotu sev dzīvi esmu izveidojis kaut ko tādu: <?php if($_GET['lapa']) { $fails = "_lpps_/".$_GET['lapa'].".php"; if(file_exists($fails)) { include "_lpps_/".$_GET['lapa'].".php"; } else { echo "Notikusi kļūda!"; } } else { include "_lpps_/jaunumi.php"; } ?> un tad vnk metu failus "_lpps_" folderā Edited February 13, 2010 by anonīms Link to comment Share on other sites More sharing options...
2easy Posted February 13, 2010 Report Share Posted February 13, 2010 (edited) es "dzīvi atvieglotu" vēl vairāk ;) if($_GET['lapa']) { $fails = "_lpps_/".$_GET['lapa'].".php"; if(file_exists($fails)) include $fails; else echo "Notikusi kļūda!"; } else include "_lpps_/jaunumi.php"; Edited February 13, 2010 by 2easy Link to comment Share on other sites More sharing options...
anonīms Posted February 13, 2010 Report Share Posted February 13, 2010 lab lab, 3-4 rindiņas mazāk :D Bet jā. Nezinu kapēc neinlūdoju $fails, bet visu garo penteri Link to comment Share on other sites More sharing options...
2easy Posted February 13, 2010 Report Share Posted February 13, 2010 (edited) es jau ļoti labi apzinos, ka vienā atsevišķā koda fragmentā tas ir pilnīgs sīkums, bet kodējot visu lapu, tādi sīkumi pamatīgi uzreizinās. tāpēc pieturos pie tāda askētiska stila, lai kods neizplestos kilometriem tālu :D:D:D lab lab, 3-4 rindiņas mazāk :D tomēr 10x rindiņas mazāk ;) (un tas uz tik nelielu koda fragmentu :D) Edited February 13, 2010 by 2easy Link to comment Share on other sites More sharing options...
anonīms Posted February 13, 2010 Report Share Posted February 13, 2010 nu lab. Tik pat labi es varu to visu sarakstīt vienā rindā :D Lab, beztēma :) Link to comment Share on other sites More sharing options...
2easy Posted February 13, 2010 Report Share Posted February 13, 2010 vajag "zelta vidusceļu" ;) Link to comment Share on other sites More sharing options...
Web Developer Posted February 13, 2010 Report Share Posted February 13, 2010 Web Developer, kādēļ? Jo switch mērķis tieši ir sazarot programmas "tecējumu" (flow) VAIRĀKOS virzienos. If ir nosacījumu konstrukcija, kuras mērķis ir izšķirties starp "boolean" tipa vērtību un rīkoties atkarībā no tās, to nevar nosaukt par "daudzzarošanu". Link to comment Share on other sites More sharing options...
Delfins Posted February 13, 2010 Report Share Posted February 13, 2010 kapec nevar? Visu nosaka pārbaudāmo mainīgo relatīvs daudzums :) if (x==1 && y==0) { page = new SuperPage(); } else if (x==1 && y==-1) { page = new SimplePage(); } else { page = new EmptyPage(); } page->draw(); Link to comment Share on other sites More sharing options...
Grey_Wolf Posted February 13, 2010 Report Share Posted February 13, 2010 es "dzīvi atvieglotu" vēl vairāk ;) if($_GET['lapa']) { $fails = "_lpps_/".$_GET['lapa'].".php"; if(file_exists($fails)) include $fails; else echo "Notikusi kļūda!"; } else include "_lpps_/jaunumi.php"; muljkjiibas... absaluuts kontroles truukums, nekadas parbaudes uz ienakoshiem datiem .. inkludojam lapu ( tadus kuri potenciali var netbilst dotajiem kriterijiem ) -- Principa ir taa ka IF -> else uz nlielam konstrukcijam ( zarojumiem )[2-5] Switch uz lieliem ... Ta tas ir bijis un ta tam buus buut Link to comment Share on other sites More sharing options...
briedis Posted February 13, 2010 Report Share Posted February 13, 2010 es "dzīvi atvieglotu" vēl vairāk ;) if($_GET['lapa']) { $fails = "_lpps_/".$_GET['lapa'].".php"; if(file_exists($fails)) include $fails; else echo "Notikusi kļūda!"; } else include "_lpps_/jaunumi.php"; Protams, šajā gadījumā nevajadzētu aizmirst nodrošīnāties pret to, ka $_GET['lapa'] var saturēt "../configs/passwordlist.txt" :) Tam esmu izmantojis vienkāršu funkciju... function getSafeIncludePath($str){ $invalidChars = array("/",".","\\","\"",";"); return str_replace($invalidChars,"",trim($str)); } Link to comment Share on other sites More sharing options...
bubu Posted February 13, 2010 Report Share Posted February 13, 2010 ne switch, new if. Pareizi ir vai nu ar file_exists, kā anonīms raksta (tik + klāt jāpieliek drošība), vai ar masīvu whitelistotajām lapām: $a = array("news", "topics", "whatever"); Un tad pārbaudi vai GET arguments ir masīvā un tad inclūdo to. Abus var apvieno. Link to comment Share on other sites More sharing options...
Recommended Posts