FMATeam Posted April 11, 2009 Share Posted April 11, 2009 Hey guys, Is there anyway to make this code any shorter? if($_GET['sort'] == "genre" && $_GET['order'] == "asc") { $o_genre = "desc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "genre" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "gametitle" && $_GET['order'] == "asc"){ $o_genre = "asc"; $o_title = "desc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "gametitle" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "description" && $_GET['order'] == "asc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "desc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "description" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "keywords" && $_GET['order'] == "asc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "desc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "keywords" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "swf" && $_GET['order'] == "asc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "desc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "swf" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "dimw" && $_GET['order'] == "asc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "desc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "dimw" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "dimh" && $_GET['order'] == "asc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "desc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "dimh" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "gameinstructions" && $_GET['order'] == "asc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "desc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "gameinstructions" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "gamepic" && $_GET['order'] == "asc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "desc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "gamepic" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "author" && $_GET['order'] == "asc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "desc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "author" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "pageloads" && $_GET['order'] == "asc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "desc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "pageloads" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif($_GET['sort'] == "time" && $_GET['order'] == "asc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "desc"; } elseif($_GET['sort'] == "time" && $_GET['order'] == "desc") { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } elseif(!$_GET['sort'] && !$_GET['order']) { $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; } It does what I want it to at the moment but exeution time is a little slow. The overall file itself is upwards of 1000 lines. Thanks FMAteam Quote Link to comment https://forums.phpfreaks.com/topic/153631-solved-making-this-code-a-bit-shorter/ Share on other sites More sharing options...
AdRock Posted April 11, 2009 Share Posted April 11, 2009 Might work with a switch statement Quote Link to comment https://forums.phpfreaks.com/topic/153631-solved-making-this-code-a-bit-shorter/#findComment-807296 Share on other sites More sharing options...
FMATeam Posted April 11, 2009 Author Share Posted April 11, 2009 I don't know if using a switch statement would necessarily make the code significantly shorter. I was looking for a solution more to do with not having to define the value of every variable every time the sort or order changes. Quote Link to comment https://forums.phpfreaks.com/topic/153631-solved-making-this-code-a-bit-shorter/#findComment-807299 Share on other sites More sharing options...
wildteen88 Posted April 11, 2009 Share Posted April 11, 2009 Looking at your code you should first define all variables as asc. Now within your if/elseif/else statement you just redefine the necessary variable(s) when the sort order changes. Quote Link to comment https://forums.phpfreaks.com/topic/153631-solved-making-this-code-a-bit-shorter/#findComment-807301 Share on other sites More sharing options...
PFMaBiSmAd Posted April 11, 2009 Share Posted April 11, 2009 but exeution time is a little slow The posted code only takes .0007 seconds to execute, and that is for the last elseif statement being matched and with all the "undefined" index errors being processed when the $_GET variables don't exist. With some default value/validation code to set the $_GET variables to a FALSE value when they don't exist, the time drops to 3.4093856811523E-5 seconds. The posted code is not where your time problem is. Based on what it is setting the variables to, you have some horrendous ORDER BY query. The problem is in your query and/or your table design. You would need to post information about those to get help. Quote Link to comment https://forums.phpfreaks.com/topic/153631-solved-making-this-code-a-bit-shorter/#findComment-807302 Share on other sites More sharing options...
FMATeam Posted April 11, 2009 Author Share Posted April 11, 2009 Thanks for all the help! Cheers Quote Link to comment https://forums.phpfreaks.com/topic/153631-solved-making-this-code-a-bit-shorter/#findComment-807303 Share on other sites More sharing options...
ToonMariner Posted April 11, 2009 Share Posted April 11, 2009 not that I like the look of all that i would reduce that lot to this... <?php $o_genre = "asc"; $o_title = "asc"; $o_desc = "asc"; $o_keywords = "asc"; $o_swf = "asc"; $o_dimw = "asc"; $o_dimh = "asc"; $o_inst = "asc"; $o_pic = "asc"; $o_author = "asc"; $o_pl = "asc"; $o_timestamp = "asc"; if (isset($_GET['sort'],$_GET['order'])) { $i = $_GET['sort']; $temp = "o_$i"; $$temp = $_GET['order']; } ?> you will have to adjust your code a little bit as you use gametitle in the url which relates to $o_title... the $o_xxx should relate to the sort value in the url string so xxx = will be what ever $_GET['sort'] is (hope that made sense). doing that will make your code more manageable. try that... ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/153631-solved-making-this-code-a-bit-shorter/#findComment-807306 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.