現在、私のコードは動作していますが、コードの重複が多いので、これは本当に効率的ではありません。これを行う方法は知らない。ここでデータベースのドロップダウンリストから値を効率的に取得
<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();
}
私はあなたがそれを区別していないsportIDに基づいて書き込みクエリの必要性が何であるかを知りません。すべてのスポーツを取得する1つのクエリを作成します。 –
質問がありますか? – Strawberry
@Strawberryドロップダウンリストからさまざまなスポーツを見つけるための効率的な方法を求める –