0

我希望我的搜索专注于 URL 字符串中的变量。目前它使用基于表单的搜索。

它使用这个

<?php
    include('db.php');  // include your code to connect to DB.
    $tbl_name="mobile";     //your table name

$whereClauses = array(); 
if (! empty($_POST['Model'])) $whereClauses[] ="model='".mysql_real_escape_string($_POST['Model'])."'"; 
if (! empty($_POST['Mins'])) $whereClauses[] ="minutes='".mysql_real_escape_string($_POST['Mins'])."'"; 
if (! empty($_POST['Texts'])) $whereClauses[] ="texts='".mysql_real_escape_string($_POST['Texts'])."'"; 
if (! empty($_POST['Freegifts'])) $whereClauses[] ="free_gift='".mysql_real_escape_string($_POST['Freegifts'])."'"; 
if (! empty($_POST['Network'])) $whereClauses[] ="network_name='".mysql_real_escape_string($_POST['Network'])."'"; 
if (! empty($_POST['Merchant'])) $whereClauses[] ="merchant_category='".mysql_real_escape_string($_POST['Merchant'])."'"; 
$where = ''; 
if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } 
$sql = mysql_query("SELECT * FROM $tbl_name".$where); 
?>

但是,我已将此添加到我的页面:

<?php
    $model = $_GET['model']; //gets model from URL
    $mins = $_GET['mins']; //gets mins from URL
    $texts = $_GET['texts']; //gets mins from URL
    $freegift = $_GET['free-gift']; //gets mins from URL
    $network = $_GET['network']; //gets mins from URL
    $plan = $_GET['plan']; //gets mins from URL
?>

它需要如此,所以并非所有变量都是必需的。任何帮助,将不胜感激。

提前致谢 :)

4

1 回答 1

0

有许多选项将返回 value 或 NULL:

<?php
       $model = (isset($_GET['model']) ? $_GET['model'] : NULL);
        $mins = (isset($_GET['mins'])  ? $_GET['mins']  : NULL);    
       $texts = (isset($_GET['texts']) ? $_GET['texts'] : NULL); 
    $freegift = (isset($_GET['free-gift']) ? $_GET['free-gift'] : NULL);
     $network = (isset($_GET['network'])   ? $_GET['network']   : NULL);
        $plan = (isset($_GET['plan'])  ? $_GET['plan']  : NULL);
?>

这将从相应的 $_GET 元素中获取变量,前提是只有一组。


替代语法(更短):

<?php
       $model = ($_GET['model'] ? $_GET['model'] : NULL);
        $mins = ($_GET['mins']  ? $_GET['mins']  : NULL);    
       $texts = ($_GET['texts'] ? $_GET['texts'] : NULL); 
    $freegift = ($_GET['free-gift'] ? $_GET['free-gift'] : NULL);
     $network = ($_GET['network']   ? $_GET['network']   : NULL);
        $plan = ($_GET['plan']  ? $_GET['plan']  : NULL);
?>

替代方案(虽然未经测试):

<?php
$vars = array('model','mins','texts','free-gift','network','plan');

foreach($vars as $value) {
    $$value = (isset($_GET[$value]) ? $_GET[$value] : NULL);
}
?>

编辑: 为了基于它设置 WHERE 子句,我建议:

<?php
$vars = array('model','mins','texts','free-gift','network','plan');

foreach($vars as $value) {
    $$value = (isset($_GET[$value]) ? $_GET[$value] : NULL);
    unset $vars[$value];  //sweeping the NULL ones
}
$where_clause = $vars[0]; //the only remaining value after previous cleanup
?>
于 2012-04-08T14:37:12.217 回答