dtj2ww9500 2015-11-05 08:10
浏览 137
已采纳

如何从数据库中检索单个元素的数据并将其存储到自动div中

what I'm trying to do is, i have a table named hotels and i have a page where peoples can book the hotel based on the room category. now for displaying the hotels I am using a while loop in which datas are fetch and hotels are displayed in automated divs which id same for all. so i added a $i=1 and added to the id fields and $i is incremented at the end of the while loop.
so what error I am getting is I am not able to retrieve the cost of the room category.

here is my code:

php code for getting individual hotels and show it in a div.

<?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; ?>"/>

so i want a js script that shows the cost of the category of room selected for individual hotels.

here is my js code:

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");
        }
        
        
    });
    }

and the error iam getting from console is: "Uncaught ReferenceError: hotel_rate is not defined" and "Uncaught TypeError: Cannot read property 'split' of undefined".

any type of help would be appriciated. thanx in advance.

</div>
  • 写回答

3条回答 默认 最新

  • duanjiao4763 2015-11-05 08:20
    关注

    Your jquery file must be something like this

    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
    });
    

    Also you missed quotes in var test = cat.split(:); it should be var test = cat.split(':');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1