[英]PHP echo with html select data values from Mysql
我對動態網站編碼不是很感興趣,但也許有人可以舉個例子。 我的代碼是:
echo "<tr>
<td>BOKSO</td>
<td> <input type='text' id='cargo' name='cargob' value=" . $row['CARGO'] . " size='30' /></td></tr>
<th>CAR</th>
<td>
<select name='cars' id='cars'>
<option value='v'>".$row['CAR']."</option>;
我想把這個例子中的數據 - 它將來到 select 選項值
<select>
<option disabled selected>-- Select City --</option>
<?php
include "dbConn.php"; // Using database connection file here
$records = mysqli_query($db, "SELECT city_name From tblcity"); // Use select query here
while($data = mysqli_fetch_array($records))
{
echo "<option value='". $data['city_name'] ."'>" .$data['city_name'] ."</option>"; // displaying data in option menu
}
?>
所以有人可以向我展示 - 我如何將代碼正確地插入到我的 PHP 回顯代碼中。
關於在 PHP 中創建動態網站,您需要了解一些事項。 首先,將您的數據庫查詢到一個數組中,最好在文檔的最頂部,然后將該數組輸入您的 html。 這將產生一個干凈且更具可讀性的代碼。
<?php
include "dbConn.php"; // Using database connection file here
$records = mysqli_query($db, "SELECT city_name From tblcity"); // Use select query here
// Create an empty array to hold the queried city_name data
$city_arr = [];
while ($data = mysqli_fetch_array($records)) {
// Populate the $city_arr with data from your DB
array_push($city_arr, $data['city_name']);
}
// Use your own data for the $row array
$row = array(
'CARGO' => 'CARGO',
'CAR' => 'CAR',
);
?>
// The main HTML document should be below the PHP block.
<tr>
<td>BOKSO</td>
<td> <input type='text' id='cargo' name='cargob' value="<?php echo $row['CARGO'] ?>" size='30' /></td>
<td>
<select name='cars' id='cars'>
<option value='v'><?php echo $row['CAR'] ?></option>
</select>
</td>
<td>
<select name='cars' id='cars'>
<?php
// Finally iterate over the $city_arr to populate your select options
for ($i = 0; $i < count($city_arr); $i++) {
?>
<option value='<?php echo $city_arr[$i] ?>'><?php echo $city_arr[$i] ?></option>
<?php
}
?>
</select>
</td>
</tr>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.