English 中文(简体)
基于URL 变量的搜索
原标题:Search based on URL varibles

我希望我能集中探讨《乌拉圭回合宪章》中的各种变量。 目前,它使用一种基于表格的搜索方法。

它使用此方法。

<?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
?>

不需要所有变量。 希望得到任何帮助。

提前感谢:

最佳回答

有一些选择,将回报价值或民族解放军:

<?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要素中获取变量。


Alternative syntax (shorter):

<?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);
}
?>

EDIT: In order to set a WHERE clause based on it, I suggest:

<?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
?>
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签