fesan Posted January 12, 2009 Share Posted January 12, 2009 Hello.... I'm making a page that lists out data from my database. It is working that's not the issue but as i was programming i thought that it must be an easier way to do this. First page is some links: <a href="index.php?page=list_produkter&produkt_id=mac_600" /> <a href="index.php?page=list_produkter&produkt_id=mac_300" /> and so on.... At the page "list_produkter" I want to add some different codes for the different "produkt_id". I solved this by doing the following: <?php $produkt_id = $_GET[produkt_id]; if($produkt_id=="mac_250") { echo "250 Liste"; } else { if($produkt_id=="mac_300") { echo "300 Liste"; } else { if($produkt_id=="mac_600") { echo "600 Liste"; }}} ?> Is there a more clever way to do this? Link to comment https://forums.phpfreaks.com/topic/140576-solved-if-else-if-else/ Share on other sites More sharing options...
ratcateme Posted January 12, 2009 Share Posted January 12, 2009 you can use the if else like this if($produkt_id=="mac_250") { echo "250 Liste"; } elseif($produkt_id=="mac_300") { echo "300 Liste"; } elseif($produkt_id=="mac_600") { echo "600 Liste"; } you could use a switch switch($produkt_id){ case "mac_250": echo "250 Liste"; break; case "mac_300": echo "300 Liste"; break; case "mac_600": echo "600 Liste"; break; } or if you just want the number echo substr($produkt_id,4) . " Liste"; Scott. Link to comment https://forums.phpfreaks.com/topic/140576-solved-if-else-if-else/#findComment-735661 Share on other sites More sharing options...
Mchl Posted January 12, 2009 Share Posted January 12, 2009 switch might look better, as long as you keep it simple. For long lists of items, a lookup table will be better. $lookupNames = array( "mac_250" => "250 Liste", "mac_300" => "300 Liste", "mac_600" => "600 Liste", ); $produkt_id = $_GET[produkt_id]; echo $lookupNames[$produkt_id]; Link to comment https://forums.phpfreaks.com/topic/140576-solved-if-else-if-else/#findComment-735663 Share on other sites More sharing options...
killah Posted January 12, 2009 Share Posted January 12, 2009 <?php $prodigy = $_GET['produkt_id']; $var_array = array ( 'mac_250' => '250 Liste', 'mac_300' => '300 Liste', 'mac_600' => '600 Liste' ); echo $var_array[$prodigy]; ?> That there, IHMO could be faster i believe. Look's like mchl got to it before me. Link to comment https://forums.phpfreaks.com/topic/140576-solved-if-else-if-else/#findComment-735665 Share on other sites More sharing options...
fesan Posted January 12, 2009 Author Share Posted January 12, 2009 Thank you guys.... Think the perfect solution for me is the switch(). That i didn't think of that myself... The thing is that I'm gonna write out a lot of HTML between the cases, so by my judgement the switch is a easy way to have controll of the "big" script. Unless there is some kind of "goto" function. Thanks again! Link to comment https://forums.phpfreaks.com/topic/140576-solved-if-else-if-else/#findComment-735683 Share on other sites More sharing options...
Mchl Posted January 12, 2009 Share Posted January 12, 2009 Um no... If your switch cases are going to be large, it can quickly get very messy and difficult to maintain. You should try to move as much common code as possible outside of conditional statements. Link to comment https://forums.phpfreaks.com/topic/140576-solved-if-else-if-else/#findComment-735694 Share on other sites More sharing options...
killah Posted January 13, 2009 Share Posted January 13, 2009 As mchl said. Having 30 row's in a switch function can/could get messy. When you can rather have a much easier url such as: <a href="index.php?page=list_produkter&produkt_id=600&n=mac"> Then of course in the php page: <?php $produkt_id = abs(@intval($_GET['produkt_id'])); $name = $_GET['n']; echo $produkt_id.' Liste '.$name; ?> Link to comment https://forums.phpfreaks.com/topic/140576-solved-if-else-if-else/#findComment-736110 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.