1

次のコードは、ユーザーが指定した3つの変数を使用する必要があります。デフォルトでは、これらの変数はすべて0に等しくなります。

  1. 時間(テキストボックス)
  2. 都市(ドロップダウンリスト)
  3. タイプ(ドロップダウンリスト)

たとえば、時間と都市がユーザーによって指定されたが、タイプをゼロにした場合、結果は返されません。私の質問は、ユーザーが時間、都市、タイプ、またはこれらの組み合わせを選択しないことを選択した場合に結果が返されるように、既存のコードを変更する効果的かつ効率的な方法は何ですか?

たとえば、時刻21:00に都市番号3を追加すると、2つの基準を満たすすべてのタイプが一覧表示されます。

$question= 'SELECT * FROM events WHERE ABS(TIMESTAMPDIFF( HOUR , `time`, :time )) < 2 AND city=:city AND type=:type';
$query = $db->prepare($question);
$query->bindValue(":time", $time, PDO::PARAM_INT);
$query->bindValue(":city", $city, PDO::PARAM_INT);
$query->bindValue(":type", $type, PDO::PARAM_INT);
$query->execute();
4

4 に答える 4

1

SQLクエリの個々の部分を構築するために、条件の配列を使用し、条件が存在するかどうかを確認することを好みます。

$conditions = array(); // Creating an array of conditions.
if ($time) // Checks to see if value exists.
{
    $timeCondition = "ABS(TIMESTAMPDIFF( HOUR , `time`, :time )) < 2";
    $conditions[] = $timeCondition; // Adds this condition string to the array.
}
if ($city)
{
    $cityCondition = "city=:city";
    $conditions[] = $cityCondition;
}
if ($type)
{
    $typeCondition = "type=:type";
    $conditions[] = $typeCondition;
}

$conditionString = implode(" AND ", $conditions); // Gluing the values of the array with " AND " in between the string conditions.

if (count($conditions) > 0) // If conditions exist, add "WHERE " to the condition string.
{
    $conditionString = "WHERE ".$conditionString;
}
else // Otherwise, the condition string is blank by default.
{
    $conditionString = '';
}

$question= 'SELECT * FROM events '.$conditionString; // If no conditions, will return all from events. Otherwise, conditions will be slotted in through $conditionString.

$query = $db->prepare($question);

if($time)
    $query->bindValue(":time", $time, PDO::PARAM_INT);
if($city)
    $query->bindValue(":city", $city, PDO::PARAM_INT);
if($type)
    $query->bindValue(":type", $type, PDO::PARAM_INT);

$query->execute();
于 2012-07-20T16:22:25.610 に答える
1
<?php
$question= 'SELECT * FROM events WHERE ';

$hasTime = false;
if(!empty($time)) { // @note better validation here
    $hasTime = true;
    $question .= 'ABS(TIMESTAMPDIFF( HOUR , `time`, :time )) < 2 ';
}

$hasCity = false;
if(!empty($city)) { // @note better validation here
    $hasCity = true;
    $question .= 'AND city=:city ';
}

$hasType = false;
if(!empty($type)) { // @note better validation here
    $hasType = true;
    $question .= 'AND type=:type';
}

$query = $db->prepare($question);

if($hasTime)
    $query->bindValue(":time", $time, PDO::PARAM_INT);
if($hasCity)
    $query->bindValue(":city", $city, PDO::PARAM_INT);
if($hasType)
    $query->bindValue(":type", $type, PDO::PARAM_INT);

$query->execute();
$results = $query->fetchAll();

if(empty($results))
    echo 'no results';
else
    // $results is an array of arrays
于 2012-07-20T16:23:59.953 に答える
0

IF()SQLステートメントで一連のステートメントを使用し、値が設定されていない場合はtrueを返すことができます。だからこのようなもの:

...WHERE IF(:time, ABS(TIMESTAMPDIFF(HOUR, `time`, :time)) < 2, 1)
AND IF(:city, city=:city, 1) AND IF(:type, type=:type, 1)
于 2012-07-20T16:24:56.557 に答える
0

フィールドがデフォルトの場合、where句に含めないように、クエリを動的に作成します。

$conditions = array();
if ($_POST['time']) {
    $conditions[] = "ABS(TIMESTAMPDIFF( HOUR , `time`, :time )) < 2";
}
if ($_POST['city']) {
    $conditions[] = "city=:city";
}
if ($_POST['type']) {
    $conditions[] = "type=:type";
}

$conditionString = implode(" AND ", $conditions);
if (count($conditions) > 0) {
    $conditionString = "WHERE " . $conditionString;
}
else {
    $conditionString = '';
}
$question = 'SELECT * FROM events ' . $conditionString;
于 2012-07-20T16:22:24.920 に答える