我想做的是,我有一个名为酒店的表,并且我有一个页面,人们可以在其中根据房间类别预订酒店。现在,为了显示酒店,我使用 while 循环,其中获取数据,酒店显示在自动 div 中,所有 ID 都相同。所以我添加了一个 $i=1
并添加到 id 字段中,并且 $i
在 while 循环结束时递增。
所以我得到的错误是我无法检索房间类别的成本。
这是我的代码:
用于获取单个酒店并将其显示在 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(){
jQuery(document).ready(function($){
var i=1;
var cnt = $("#count").val();
for(i=1;i<cnt;i++){
var cat = $("#hotel_cat["+i+"]").val();
var test = cat.split(:);
var cat_cost = parseInt(test[1]);
$("#add["+i+"]").html("Rs: "+cat_cost+" Add");
}
});
}
并且我从控制台得到的错误是:“未捕获的ReferenceError:hotel_rate未定义”和“未捕获的类型错误:无法读取未定义的属性'split'”。
任何类型的帮助都会受到欢迎。 提前致谢。
最佳答案
你的 jquery 文件必须是这样的
function hotel_rate(){ // just define the function.
var i=1;
var cnt = $("#count").val();
for(i=1;i<cnt;i++){
var cat = $("#hotel_cat["+i+"]").val();
var test = cat.split(':');
var cat_cost = parseInt(test[1]);
$("#add[1]").html("Rs: "+cat+" Add");
}
}
jQuery(document).ready(function($){
hotel_rate(); // fire it on document ready
});
此外,您还错过了 var test = cat.split(:);
中的引号,它应该是 var test = cat.split(':');
关于javascript - 如何从数据库中检索各个元素的数据并将其存储到自动 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33539426/