简体   繁体   English

PHP从URL干净地设置多个变量

[英]PHP Setting Multiple Variables From URL Cleanly

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. 本质上,我的URL中包含多个变量。 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>

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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM