Итак, у меня есть эта форма:
<form name="form1" action="passearch.php" method="POST" class="basic-grey"><h1 class="offset3"> Search for a Bandpass Filter
<span>Please complete all the fields.</span></h1>
<label>
<span><b>Start Bandpass(fL):</b></span>
<input type="text" name="Lowfreq" placeholder="Lowest Frequency">
</label>
<label>
<span><b>Stop Bandpass(fH):</b></span>
<input type="text" name ="Highfreq" placeholder="Highest Frequency">
</label>
<label>
<span>
<b>Connector Type:</b> </span>
<select name="connector_type">
<option value=''></option>
<option value="Any">Any</option>
<option value="no_Connector">No Connector</option>
<option value="SMA-F">SMA-F</option>
<option value="SMA-M">SMA-M</option>
</select>
</label>
<label>
<span>
<b>Construction Type:</b> </span>
<select name="construction_type">
<option value="any">Any</option>
<option value="combline">Combline</option>
<option value="lumped">Lumped Element</option>
<option value="mixed">Mixed</option>
<option value="suspended substrate">Suspended Substrate</option>
<option value="waveguide">Waveguide</option>
</select>
</label>
<input type="submit" class="small button" value="Submit">
</form>
Получение информации из базы данных MySQL. Идея состоит в том, чтобы иметь возможность искать фильтр, который соответствует данным формы, но если выбор не выбран (например, если не выбран тип соединителя, поиск продолжается, но не выполняется поиск фильтров с типом соединителя x). У меня это работает для ‘$ highfreq’, но не могу заставить его работать для Connector_type и Construction_type. Я не уверен, что пойду правильно. Вот мой выбор PHP
<?php
$username = "root";
$password = "Toom13371!";
$lowfreq = $_POST['Lowfreq']; # User-Supplied Data. I'm A
$highfreq = $_POST['Highfreq']; # User-Supplied Data. Placeholder yo.
$construction = $_POST['construction_type']; # User-Supplied Data. Still holding
$connector = $_POST['connector_type']; # User-Supplied Data. that place.
try {
$dbh = new PDO("mysql:host=127.0.0.1;dbname=filters", $username, $password);
} catch(PDOException $e) {
echo $e->getMessage();
}//print
//echo $construction; #test
// Select some data
$sth = $dbh->prepare("SELECT id, type_code, connector_type, construction_type, start_passband, stop_passband, low_passband, high_passband FROM filter_bandpass WHERE ((start_passband = $lowfreq OR $lowfreq = '') AND (stop_passband = $highfreq OR $highfreq = '')
AND (construction_type = '$construction')
)");
//$sth = $dbh->prepare("SELECT id, type_code FROM filter_bandpass WHERE construction_type = '$construction'");
// Execute the query, replacing the placeholders with their true value
$sth->execute(array(
));
// How many records did we find?
echo "<div class='jumbotron'>";
echo "<h1>Filters Found</h1>";
echo 'We found ' . $sth->rowCount() . ' filters matching your search request.';
echo "</div>";?>
<hr>
<div class="row-fluid marketing">
<!-- Pwetty little table. -->
<table class="gridtable">
<thead>
<tr>
<th> ID</th>
<th> Type Code</th>
<th> Connector Type</th>
<th>Construction Type</th>
<th> Start Passband </th>
<th> Stop Passband </th>
<th> Low Passband </th>
<th> High Passband </th>
</tr>
</thead>
<tbody>
<?php while ($row = $sth->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($row['id'])?></td>
<td><?php echo htmlspecialchars($row['type_code']); ?></td>
<th><?php echo htmlspecialchars($row['connector_type']); ?></td>
<th><?php echo htmlspecialchars($row['construction_type']); ?></td>
<th><?php echo htmlspecialchars($row['start_passband']); ?></td>
<th><?php echo htmlspecialchars($row['stop_passband']); ?></td>
<th><?php echo htmlspecialchars($row['low_passband']); ?></td>
<th><?php echo htmlspecialchars($row['high_passband']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
Спасибо заранее, Том.
установить пустой тип конструкции —
<select name="construction_type">
<option value="">Any</option>
<option value="combline">Combline</option>
<option value="lumped">Lumped Element</option>
<option value="mixed">Mixed</option>
<option value="suspended substrate">Suspended Substrate</option>
<option value="waveguide">Waveguide</option>
</select>
после подачи —
$lowfreq = $_POST['Lowfreq']; # User-Supplied Data. I'm A
$highfreq = $_POST['Highfreq']; # User-Supplied Data. Placeholder yo.
$construction = $_POST['construction_type']; # User-Supplied Data. Still holding
$connector = $_POST['connector_type']; # User-Supplied Data.
$query = "SELECT id, type_code, connector_type, construction_type, start_passband, stop_passband, low_passband, high_passband FROM filter_bandpass WHERE "
$condition = array();
if (!empty($lowfreq)) {
$condition[] = " start_passband = $lowfreq";
}
if (!empty($highfreq)) {
$condition[] = " stop_passband = $highfreq";
}
if (!empty($construction)) {
$condition[] = " construction_type = $construction";
}
if (!empty($construction)) {
$condition[] = " connector_type = $connector";
}
if (!empty($condition)) {
$query .= " ".implode('AND', $condition);
}
вы получите запрос.
Других решений пока нет …