[英]Populate select box with DATE RANGE from MySQL
我有一張桌子,上面有兩個日期,一個開始日期和一個結束日期。 我不知道的是如何使用從開始日期到結束日期的所有日期填充選擇框。
我的代碼是
$stmt = $mysqli->prepare("SELECT `eventStart` `eventEnd` FROM $tbl_events WHERE eventName = '$eventID'");
$stmt->execute();
$stmt->bind_result($name);
while ($stmt->fetch()){
echo "<option value='$name'>$name</option>";
}
$stmt->close();
它可以工作,但只填充第一個日期。 我已經在網上搜索過,但是找不到解決我問題的方法。
<?php
// $aResults = $stmt->fetch_all(); or $stmp->fetch_array();
// Ensure your results look great to your liking before continuing.
$aResults = array( 2001, 2002, 2003, 2004, 2005, 2006 );
$iCountResults = count( $aResults );
echo "<select>";
for( $i = 0; $i < $iCountResults; ++$i )
{
echo '<option value="' . $aResults[ $i ] . '">' . $aResults[ $i ] . '</option>';
}
echo "</select>";
// $stmt->close();
?>
$startdate_options = array();
$enddate_options = array();
$stmt = $mysqli->prepare("SELECT `eventStart` `eventEnd` FROM $tbl_events WHERE eventName = '$eventID'");
$stmt->execute();
$stmt->bind_result($startdate_val, $enddate_val);
while ($stmt->fetch()){
$startdate_options[] = $startdate_val;
$enddate_options[] = $enddate_val;
}
$stmt->close();
// from this point it really should be in another file
// startdate values render
echo '<select name="startdate" class="startdate">';
foreach ($startdate_options as $startdate)
{
echo "<option value='$startdate'>$startdate</option>";
}
echo '</select>';
//enddate values render
echo '<select name="enddate" class="enddate">';
foreach ($enddate_options as $enddate)
{
echo "<option value='$enddate'>$enddate</option>";
}
echo '</select>';
//還請注意,這不是MVC,出於可讀性考慮,您實際上不應該在同一源文件,同一類或同一程序流中執行SQL語句,業務邏輯和呈現html。 實際上,您只應該傳遞“模板引擎” $ startdate_options和$ enddate_options數組(更像是作為返回值或類成員,而不是全局變量)
希望我能幫到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.