1

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>
1
  • This method $sortby = $_GET['sort'] or $sortby = 'price'; may technically "work" as expected but generates a notice when $_GET['sort'] is not set. Try isset() or !empty(). Commented Sep 1, 2015 at 14:58

1 Answer 1

1

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';
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, so all variables have been replaced with your suggestion. I'm not clear on how to implement the first part of creating the array. Are you saying to setup an array of values for all sortby and orderby options and then pull them depending on the URL variables?
Yep that is what I mean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.