2017-05-31 14 views
0

それは動作しますが、私はそれが私の問題のための最良の解決策であるとは思わない。 私のコードで行うべきことは、location = 1であるかどうかをチェックして、すべての場所のメッセージを送信することです。条件付きプリペイントステートメントPHP mysqli、reduce

function getCurrentMessage($location){ 
    $conn = Connection::getConnection(); 

    if($location == 1) { 
     $query = "SELECT first_name, last_name, description, title, message ,font_size , effective_date 
       FROM tbl_messages 
       JOIN tbl_authors ON tbl_authors.id_author = tbl_messages.id_author 
       JOIN tbl_locations ON tbl_messages.id_location = tbl_locations.id_location 
       AND effective_date <= CURDATE() 
       ORDER BY effective_date desc 
       LIMIT 2;"; 

     $result = array(); 

     if ($stmt = $conn->prepare($query)) { 
      $stmt->bind_result($first_name, $last_name, $location, $title, $message, $size, $date); 
      $stmt->execute(); 

      while ($stmt->fetch()) { 
       $message = new Message($first_name, $last_name, $location, $title, $message, $size, $date); 
       array_push($result, $message); 
      } 
     } 
    } 
    else{ 
     $query = "SELECT first_name, last_name, description, title, message ,font_size , effective_date 
       FROM tbl_messages 
       JOIN tbl_authors ON tbl_authors.id_author = tbl_messages.id_author 
       JOIN tbl_locations ON tbl_messages.id_location = tbl_locations.id_location 
       WHERE tbl_messages.id_location = ? 
       AND effective_date <= CURDATE() 
       ORDER BY effective_date desc 
       LIMIT 2;"; 

     $result = array(); 

     if ($stmt = $conn->prepare($query)) { 
      $stmt->bind_result($first_name, $last_name, $location, $title, $message, $size, $date); 
      $stmt->bind_param('i', $location); 
      $stmt->execute(); 

      while ($stmt->fetch()) { 
       $m = new Message($first_name, $last_name, $location, $title, $message, $size, $date); 
       array_push($result, $m); 
      } 
     } 
    } 

    return $result; 
} 

多分、私はいくつかのロジックをSQL文に入れることができます。 洞察力があれば、助けてください。

+1

機能の始めにチェックを外すことができます。これは、1つのクエリだけを使用できるようにすることによって役立ちます。 WHERE location =?のような関数内のパラメータから場所を取るwhere節があります。 – Akintunde007

+0

[コードレビュー](https://codereview.stackexchange.com/)は、質問するのに最適な場所です –

答えて

0

私はこの質問は、コードレビューCode review 、 に掲載されている必要がありますが、MySQLの詳細を学んだ後、私は私の答えは、制御フロー機能を使用しているので、ここでのコードをクリーンアップする方法を考え出したことを今実現しています。
コードをさらにクリーンアップする方法があれば教えてください。

$conn = getConnection(); 
$query = "SELECT first_name, last_name, description, title, message ,font_size , DATE_FORMAT(effective_date,'%h:%i %p %m-%d-%Y') 
      FROM tbl_messages 
      JOIN tbl_authors ON tbl_authors.id_author = tbl_messages.id_author 
      JOIN tbl_locations ON tbl_messages.id_location = tbl_locations.id_location 
      WHERE tbl_messages.id_location = IF(? = 1,tbl_messages.id_location,?) 
      AND effective_date <= NOW() 
      ORDER BY effective_date DESC 
      LIMIT 1 
      "; 

if (!$stmt = $conn->prepare($query)) { 
    return false; 
} 

$stmt->bind_param('ii', $location,$location); 

$result = array(); 

$stmt->bind_result($first_name, $last_name, $location, $title, $message, $size, $date); 
$stmt->execute(); 

while ($stmt->fetch()) { 
    $m = new Message($first_name, $last_name, $location, $title, $message, $size, $date); 
    array_push($result, $m); 
} 

return $result; 
0

条件に依存しない重複部分をすべてリファクタリングするだけです。

function getCurrentMessage($location){ 
    $conn = Connection::getConnection(); 

    if($location == 1) { 
     $query = "SELECT first_name, last_name, description, title, message ,font_size , effective_date 
       FROM tbl_messages 
       JOIN tbl_authors ON tbl_authors.id_author = tbl_messages.id_author 
       JOIN tbl_locations ON tbl_messages.id_location = tbl_locations.id_location 
       AND effective_date <= CURDATE() 
       ORDER BY effective_date desc 
       LIMIT 2;"; 

     if (!$stmt = $conn->prepare($query)) { 
      return false; 
     } 

    } 
    else{ 
     $query = "SELECT first_name, last_name, description, title, message ,font_size , effective_date 
       FROM tbl_messages 
       JOIN tbl_authors ON tbl_authors.id_author = tbl_messages.id_author 
       JOIN tbl_locations ON tbl_messages.id_location = tbl_locations.id_location 
       WHERE tbl_messages.id_location = ? 
       AND effective_date <= CURDATE() 
       ORDER BY effective_date desc 
       LIMIT 2;"; 

     if (!$stmt = $conn->prepare($query)) { 
      return false; 
     } 
     $stmt->bind_param('i', $location); 
    } 

    $result = array(); 

    $stmt->bind_result($first_name, $last_name, $location, $title, $message, $size, $date); 
    $stmt->execute(); 

    while ($stmt->fetch()) { 
     $m = new Message($first_name, $last_name, $location, $title, $message, $size, $date); 
     array_push($result, $m); 
    } 

    return $result; 
} 

さらに進んでも問題ありませんが、これは単なるサンプルに過ぎません。文の準備が失敗した場合、関数がどのようにfalseを返すかに注意してくださいあなたは関数の中にいるので、これは関数の実行を停止し、準備が失敗した場合にはさらに進むべきポイントがないのでfalseを返します。捕まえることができるものが必要な場合は、例外を使用することもできます。