dtj2ww9500
2015-11-05 08:10
浏览 131

如何从数据库中检索单个元素的数据并将其存储到自动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(':');

    点赞 评论
  • duanjupiao4730 2015-11-05 08:13

    The declaration of hotel_rate()should be inside ready function, also replace cat.split(:); by cat.split(':'); :

    $(function(){
        function hotel_rate(){
            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");
            }
        }
    });
    
    点赞 评论
  • doulun1666 2017-10-10 18:10

    Above two answers is also right.you have to declare function outside document.ready section and split function argument must have string inside it.

    here is i put working code example as per your concept.you can also check paste in this fiddle php fiddle.

        <div class="col-md-3 col-xs-6">
        <div class="panel panel-red">
            <div class="panel-heading">
                Hotel name            </div>
            <div class="panel-body">
                <img src="images/hotels/Hotel name.png" class="img-responsive"/>
                <div class="my-caption">
                    <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>                </div>
                <select name="hotel_cat" id="hotel_cat_1" class="form-control" onchange="hotel_rate()">
                    <option value="">Category</option>
                                            <option value="1:50">1</option>
    
                </select>
                <span id="add[1]" >Add</span>
    
            </div>
        </div>
    </div>
    <input  type="hidden" name="count" id="count" value="1"/>
    

    <script>
        function hotel_rate(){
        
                var cat = document.getElementById("hotel_cat_1").value;
            alert(cat);
                var test = cat.split(':');
                var cat_cost = parseInt(test[1]);
                document.getElementById("add[1]").innerHTML="Rs: "+cat_cost+" Add";
            }
        
    </script>

    and you are splitting value of cat by ':' for get cost of hotel so in value you must have value with : separator.

    </div>
    
    点赞 评论

相关推荐 更多相似问题