Jump to content

[SOLVED] Making this code a bit shorter


FMATeam

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/153631-solved-making-this-code-a-bit-shorter/
Share on other sites

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.

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.