Reputation: 143
So I have a jumble of code that I'm sure there is a cleaner way to go about this. Essentially I have a url with multiple variables within it. Depending on the variable other variables are set. The code below functions as expected, just trying to find a cleaner route if possible.
$sortby = $_GET['sort'] or $sortby = 'price';
$direction = $_GET['dir'] or $direction = 'desc';
$automake = $_GET['make'] or $automake = '';
$autocat = $_GET['model'] or $autocat = '';
$searchkey = $_GET['s'] or $searchkey = '';
$autocondition = $_GET['cond'] or $autocondition = '';
if ($sortby == 'price') { $sorting = '_auto_price'; $orderby = 'meta_value_num'; }
else if ($sortby == 'year') { $sorting = '_auto_year'; $orderby = 'meta_value'; }
else if ($sortby == 'make') { $sorting = '_auto_make'; $orderby = 'meta_value'; }
else if ($sortby == 'model') { $sorting = '_auto_model'; $orderby = 'meta_value'; }
else { $sorting = ''; $model = ''; $orderby = 'date'; }
Then I have a few links displaying to set new variables. If you haven't guessed these are different ways to sort the page's content. These are pretty ugly as well. Again they work, just not very pretty.
<ul class="breadcrumb">
<li><strong>SORT BY:</strong></li>
<li style="padding:0 3px;"></li>
<li>Price:
<a href="/inventory?s=<?php echo $searchkey ?>&make=<?php echo $automake ?>&model=<?php echo $autocat ?>&sort=price&dir=asc&cond=<?php echo $autocondition ?>">UP</i></a>
<a href="/inventory?s=<?php echo $searchkey ?>&make=<?php echo $automake ?>&model=<?php echo $autocat ?>&sort=price&dir=desc&cond=<?php echo $autocondition ?>">DOWN</i></a>
</li>
<li style="padding:0 3px;"></li>
<li>Year:
<a href="/inventory?s=<?php echo $searchkey ?>&make=<?php echo $automake ?>&model=<?php echo $autocat ?>&sort=year&dir=asc&cond=<?php echo $autocondition ?>">UP</a>
<a href="/inventory?s=<?php echo $searchkey ?>&make=<?php echo $automake ?>&model=<?php echo $autocat ?>&sort=year&dir=desc&cond=<?php echo $autocondition ?>">DOWN</a>
</li>
<li style="padding:0 3px;"></li>
<li>Make:
<a href="/inventory?s=<?php echo $searchkey ?>&make=<?php echo $automake ?>&model=<?php echo $autocat ?>&sort=make&dir=asc&cond=<?php echo $autocondition ?>">UP</i></a>
<a href="/inventory?s=<?php echo $searchkey ?>&make=<?php echo $automake ?>&model=<?php echo $autocat ?>&sort=make&dir=desc&cond=<?php echo $autocondition ?>">DOWN</a>
</li>
<li style="padding:0 3px;"></li>
<li>Model:
<a href="/inventory?s=<?php echo $searchkey ?>&make=<?php echo $automake ?>&model=<?php echo $autocat ?>&sort=model&dir=asc&cond=<?php echo $autocondition ?>">UP</i></a>
<a href="/inventory?s=<?php echo $searchkey ?>&make=<?php echo $automake ?>&model=<?php echo $autocat ?>&sort=model&dir=desc&cond=<?php echo $autocondition ?>">DOWN</a>
</li>
</ul>
Upvotes: 1
Views: 35
Reputation: 46900
One way could be to store all the values in an array and pick up corresponding values
$parameters["price"]=array("sort_by"=>"_auto_price","order_by"=>"meta_value_num");
$parameters["year"] =array("sort_by"=>"_auto_year","order_by"=>"meta_value");
// Same for other parameters
Then you can simply do
$sorting = $parameters[$sortby]["sort_by"];
$orderby = $parameters[$sortby]["order_by"];
Also this following style is not really clean and will surely generate notices
$sortby = $_GET['sort'] or $sortby = 'price';
A better way would be
$sortby = isset($_GET['sort']) ? $_GET['sort'] : 'price';
Upvotes: 1