我正在嘗試做的是,我有一個名爲hotels的表,我有一個人們可以根據房間類別預訂酒店的頁面。現在用於顯示我正在使用一個while循環的酒店,其中數據是獲取和酒店顯示在自動化的div中,所有的ID都相同。所以我添加了一個$i=1
並添加到id字段中,並且$i
在while循環結束時遞增。
所以我得到什麼錯誤是我無法檢索房間類別的成本。如何從數據庫中檢索單個元素的數據並將其存儲到自動div中
這裏是我的代碼:
用於獲取個別酒店,並顯示在一個div PHP代碼。
<?php
include 'mysql.php';
$i = 1;
$query = mysql_query("SELECT * FROM hotels WHERE location = 'portblair' ") or die("the query cannot be completed at this moment");
if(mysql_num_rows($query) <1) {
die("no hotels found");
}
while($row = mysql_fetch_array($query, MYSQL_ASSOC)){
$hotel_name = $row['name'];
$hotel_type = $row['type'];
$hotel_location = $row['location'];
$hotel_cat1 = $row['cat1'];
$hotel_cat2 = $row['cat2'];
$hotel_cat3 = $row['cat3'];
$hotel_cat4 = $row['cat4'];
$hotel_cp = $row['cp'];
$hotel_map = $row['map'];
$hotel_ap = $row['ap'];
$hotel_em = $row['em'];
if($hotel_type == "0"){
$hotel_star ='<span class="glyphicon glyphicon-star-empty"></span><span class="glyphicon glyphicon-star-empty"></span><span class="glyphicon glyphicon-star-empty"></span><span class="glyphicon glyphicon-star-empty"></span><span class="glyphicon glyphicon-star-empty"></span>';
}
if($hotel_type == "1"){
$hotel_star ='<span class="glyphicon glyphicon-star"></span><span class="glyphicon glyphicon-star-empty"></span><span class="glyphicon glyphicon-star-empty"></span><span class="glyphicon glyphicon-star-empty"></span><span class="glyphicon glyphicon-star-empty"></span>';
}
if($hotel_type == "2"){
$hotel_star ='<span class="glyphicon glyphicon-star"></span><span class="glyphicon glyphicon-star"></span><span class="glyphicon glyphicon-star-empty"></span><span class="glyphicon glyphicon-star-empty"></span><span class="glyphicon glyphicon-star-empty"></span>';
}
if($hotel_type == "3"){
$hotel_star ='<span class="glyphicon glyphicon-star"></span><span class="glyphicon glyphicon-star"></span><span class="glyphicon glyphicon-star"></span><span class="glyphicon glyphicon-star-empty"></span><span class="glyphicon glyphicon-star-empty"></span>';
}
if($hotel_type == "4"){
$hotel_star ='<span class="glyphicon glyphicon-star"></span><span class="glyphicon glyphicon-star"></span><span class="glyphicon glyphicon-star"></span><span class="glyphicon glyphicon-star"></span><span class="glyphicon glyphicon-star-empty"></span>';
}
?>
<div class="col-md-3 col-xs-6">
<div class="panel panel-red">
<div class="panel-heading">
<?php echo $hotel_name; ?>
</div>
<div class="panel-body">
<img src="images/hotels/<?php echo $hotel_name ?>.png" class="img-responsive"/>
<div class="my-caption">
<?php echo $hotel_star; ?>
</div>
<select name="hotel_cat" id="hotel_cat[<?php echo $i; ?>]" class="form-control" onchange="hotel_rate()">
<option value="">Category</option>
<?php if($hotel_cat1 != ""){
?>
<option value="<?php echo $hotel_cat1; ?>"><?php echo $hotel_cat1; ?></option>
<?php
} ?>
<?php if($hotel_cat2 != ""){
?>
<option value="<?php echo $hotel_cat2; ?>"><?php echo $hotel_cat2; ?></option>
<?php
} ?>
<?php if($hotel_cat3 != ""){
?>
<option value="<?php echo $hotel_cat3; ?>"><?php echo $hotel_cat3; ?></option>
<?php
} ?>
<?php if($hotel_cat4 != ""){
?>
<option value="<?php echo $hotel_cat4; ?>"><?php echo $hotel_cat4; ?></option>
<?php
} ?>
</select>
<span id="add[<?php echo $i; ?>]" >Add</span>
</div>
</div>
</div>
<?php
$i++;
}
?>
<input type="hidden" name="count" id="count" value="<?php echo $i; ?>"/>
所以我想要一個js腳本,顯示爲單個酒店選擇的房間類別的成本。
這裏是我的js代碼:
function hotel_rate(){
\t jQuery(document).ready(function($){
\t \t var i=1;
\t \t var cnt = $("#count").val();
\t \t for(i=1;i<cnt;i++){
\t \t \t var cat = $("#hotel_cat["+i+"]").val();
\t \t \t var test = cat.split(:);
\t \t \t var cat_cost = parseInt(test[1]);
\t \t \t $("#add["+i+"]").html("Rs: "+cat_cost+" Add");
\t \t }
\t \t
\t \t
\t });
\t }
和錯誤IAM從控制檯得到的是: 「未捕獲的ReferenceError:hotel_rate沒有定義」 和「遺漏的類型錯誤:無法讀取屬性「拆分「未定義」。
任何類型的幫助將被appriciated。 thanx提前。
不要使用已棄用的'mysql'。改爲使用'mysqli'。 – Thaillie
1你申報功能。 '函數hote_rate(){...沒有文檔準備在它的邊...}',然後在文檔準備就緒的情況下啓動它。 '$(document).ready({hotel_rate()})' –
我還不知道mysqli。 :'( –