2017-07-15 113 views
0

現在、私のコードは動作していますが、コードの重複が多いので、これは本当に効率的ではありません。これを行う方法は知らない。ここでデータベースのドロップダウンリストから値を効率的に取得

<form method='POST'> 
     <fieldset> 
      <div id="dropDownList"> 
       <select value="sport" name="sport"> 
        <option value="invalid">Please select a sport</option> 
        <option value="show">Show All</option> 
        <?php 
         foreach ($dropDown as $row) { 
          echo'<option value='.$row["sportName"].'>'.$row["sportName"].'</option>'; 
         } 
        ?> 
       </select> 
      </div> 
      <div> 
       <button type="submit">Submit</button> 
      </div> 
     </fieldset> 

    <table> 
      <tr> 
       <th>athleteID</th> 
       <th>eventID</th> 
       <th>sportID</th> 
       <th>lastName</th> 
       <th>firstName</th> 
       <th>eventName</th> 
       <th>sportName</th> 
       <th>gender</th> 
       <th>image</th> 
       <th>medal</th> 
      </tr> 
      <?php 
       if($sportName == 'show') { 
        foreach ($selectString1 as $row) { 
         echo'<tr>'; 
         echo'<td>'.$row['athleteID'].'</td>'; 
         echo'<td>'.$row['eventID'].'</td>'; 
         echo'<td>'.$row['sportID'].'</td>'; 
         echo'<td>'.$row['lastName'].'</td>'; 
         echo'<td>'.$row['firstName'].'</td>'; 
         echo'<td>'.$row['eventName'].'</td>'; 
         echo'<td>'.$row['sportName'].'</td>'; 
         echo'<td>'.$row['gender'].'</td>'; 
         echo'<td><img src="photos/'.$row['image'].'"</td>'; 
         echo'<td>'.$row['medal'].'</td>'; 
         echo'</tr>'; 
        } 
       } 

       if($sportName == 'Athletics') { 
        foreach ($selectString3 as $row) { 
         echo'<tr>'; 
         echo'<td>'.$row['athleteID'].'</td>'; 
         echo'<td>'.$row['eventID'].'</td>'; 
         echo'<td>'.$row['sportID'].'</td>'; 
         echo'<td>'.$row['lastName'].'</td>'; 
         echo'<td>'.$row['firstName'].'</td>'; 
         echo'<td>'.$row['eventName'].'</td>'; 
         echo'<td>'.$row['sportName'].'</td>'; 
         echo'<td>'.$row['gender'].'</td>'; 
         echo'<td><img src="photos/'.$row['image'].'"</td>'; 
         echo'<td>'.$row['medal'].'</td>'; 
         echo'</tr>'; 
        } 
       } 

       if($sportName == 'CanoeSprint') { 
        foreach ($selectString4 as $row) { 
         echo'<tr>'; 
         echo'<td>'.$row['athleteID'].'</td>'; 
         echo'<td>'.$row['eventID'].'</td>'; 
         echo'<td>'.$row['sportID'].'</td>'; 
         echo'<td>'.$row['lastName'].'</td>'; 
         echo'<td>'.$row['firstName'].'</td>'; 
         echo'<td>'.$row['eventName'].'</td>'; 
         echo'<td>'.$row['sportName'].'</td>'; 
         echo'<td>'.$row['gender'].'</td>'; 
         echo'<td><img src="photos/'.$row['image'].'"</td>'; 
         echo'<td>'.$row['medal'].'</td>'; 
         echo'</tr>'; 
        } 
       } 
      ?>  
     </table> 
    </form> 

私はカップルSQL文を作成しているいくつかのPHPのPDOコードされています:

try { 
    $selectString3 = $pdo->prepare (' 
SELECT a.athleteID 
    , a.eventID 
    , a.sportID 
    , a.lastName 
    , a.firstName 
    , a.gender 
    , e.eventName 
    , s.sportName 
    , a.gender 
    , a.image 
    , a.medal 
    FROM athlete a 
    JOIN event e 
    ON e.eventID = a.eventID 
    JOIN sport s 
    ON s.sportID = a.sportID 
WHERE s.sportID = 1 
'); 
    $selectString3->execute(); 
} catch (PDOException $e) { 
    $error = 'Select statement error'; 
    include 'error.html.php'; 
    exit(); 
} 

try { 
    $selectString4 = $pdo->prepare ('SELECT athlete.athleteID, 
    athlete.eventID,athlete.sportID, athlete.lastName, athlete.firstName, 
    athlete.gender, event.eventName, sport.sportName, athlete.gender, 
    athlete.image, athlete.medal 
    FROM athlete JOIN event ON event.eventID = athlete.eventID JOIN sport ON 
    sport.sportID = athlete.sportID WHERE sport.sportID = 2'); 
    $selectString4->execute(); 
} catch (PDOException $e) { 
    $error = 'Select statement error'; 
    include 'error.html.php'; 
    exit(); 
} 
+0

私はあなたがそれを区別していないsportIDに基づいて書き込みクエリの必要性が何であるかを知りません。すべてのスポーツを取得する1つのクエリを作成します。 –

+0

質問がありますか? – Strawberry

+0

@Strawberryドロップダウンリストからさまざまなスポーツを見つけるための効率的な方法を求める –

答えて

0

ので、異なるsportIdに関連したコードには何の区別はありませんここではいくつかのコードがあります。 1つのクエリを作成してすべてのスポーツを取得し、それに従って表示することができます。

更新されたコード

<table> 
    <tr> 
    <th>athleteID</th> 
    <th>eventID</th> 
    <th>sportID</th> 
    <th>lastName</th> 
    <th>firstName</th> 
    <th>eventName</th> 
    <th>sportName</th> 
    <th>gender</th> 
    <th>image</th> 
    <th>medal</th> 
    </tr> 
    <?php 
    if(!empty($selectString)) { 
    foreach ($selectString as $row) { 
     echo'<tr>'; 
     echo'<td>'.$row['athleteID'].'</td>'; 
     echo'<td>'.$row['eventID'].'</td>'; 
     echo'<td>'.$row['sportID'].'</td>'; 
     echo'<td>'.$row['lastName'].'</td>'; 
     echo'<td>'.$row['firstName'].'</td>'; 
     echo'<td>'.$row['eventName'].'</td>'; 
     echo'<td>'.$row['sportName'].'</td>'; 
     echo'<td>'.$row['gender'].'</td>'; 
     echo'<td><img src="photos/'.$row['image'].'"</td>'; 
     echo'<td>'.$row['medal'].'</td>'; 
     echo'</tr>'; 
    } 
    } else { 
    echo "<tr><td colspan='7'>No records found!</td></tr>"; 
    }?>  
</table> 

クエリ

<?php 
try { 
    $selectString = $pdo->prepare ('SELECT athlete.athleteID, 
    athlete.eventID, athlete.sportID, athlete.lastName, athlete.firstName, 
    athlete.gender, event.eventName, sport.sportName, athlete.gender, 
    athlete.image, athlete.medal 
    FROM athlete JOIN event ON event.eventID = athlete.eventID JOIN sport 
    ON sport.sportID = athlete.sportID'); 
    $selectString->execute(); 
} catch (PDOException $e) { 
    $error = 'Select statement error'; 
    include 'error.html.php'; 
    exit(); 
} 

編集-1

<?php 

$sportId = $_POST['sport_dropdown']; //Capture your sports dropdown list value. 

try { 
    $selectString = $pdo->prepare ('SELECT athlete.athleteID, 
    athlete.eventID, athlete.sportID, athlete.lastName, athlete.firstName, 
    athlete.gender, event.eventName, sport.sportName, athlete.gender, 
    athlete.image, athlete.medal 
    FROM athlete JOIN event ON event.eventID = athlete.eventID JOIN sport 
    ON sport.sportID = athlete.sportID WHERE sport.sportID = :sportId'); 
    $selectString->execute(array(':sportId' => $sportId)); 
} catch (PDOException $e) { 
    $error = 'Select statement error'; 
    include 'error.html.php'; 
    exit(); 
} 

編集-2

あなたのスポーツドロップダウン値をsportid値に変更します。その後、それに従ってフェッチします。

<select value="sport" name="sport"> 
    <option value="invalid">Please select a sport</option> 
    <option value="show">Show All</option> 
    <?php 
    foreach ($dropDown as $row) { 
    echo'<option value='.$row["sportID"].'>'.$row["sportName"].'</option>'; 
    }?> 
</select> 

クエリ

<?php 

$sportId = $_POST['sport']; 

$where = ""; 
$search_sport_id = false; 
if(is_numeric($sportId)){ 
    $search_sport_id = true; 
    $where = " WHERE sport.sportID = :sportId"; 
} 

try { 
    $selectString = $pdo->prepare ('SELECT athlete.athleteID, 
    athlete.eventID, athlete.sportID, athlete.lastName, athlete.firstName, 
    athlete.gender, event.eventName, sport.sportName, athlete.gender, 
    athlete.image, athlete.medal 
    FROM athlete JOIN event ON event.eventID = athlete.eventID JOIN sport 
    ON sport.sportID = athlete.sportID '.$where); 
    if($search_sport_id){ 
    $selectString->execute(array(':sportId' => $sportId)); 
    } else { 
    $selectString->execute(); 
    } 
} catch (PDOException $e) { 
    $error = 'Select statement error'; 
    include 'error.html.php'; 
    exit(); 
} 

?> 
+0

私はテーブルからさまざまなスポーツを取り出すドロップダウンリストを持っています。スポーツが選択されると、そのスポーツに関するIDと表示情報が取り出されます。特定のIDを取得していないため、私が与えてくれたコードはありません –

+0

質問のどこにでも、あなたのhtmlテーブルのデータがドロップダウン選択の後に来ていると書いてありますか@RaymondHua? –

+0

** Edit-1 **セクション@RaymondHuaを参照してください。 –

関連する問題