从数据库下拉列表中有效获取值

时间:2017-07-15 07:59:51

标签: php mysql pdo

目前我的代码正在运行,但这并不是很有效,因为有很多代码重复。不知道这样做的方法。这是一些代码:

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

这是一些PHP pdo代码,我在其中创建了几个SQL语句:

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

1 个答案:

答案 0 :(得分:0)

因为,与不同sportId相关的代码没有区别。您可以创建一个查询来获取所有运动并根据它进行显示。

更新代码

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

?>