2015-11-05 59 views
1

我正在嘗試做的是,我有一個名爲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提前。

+1

不要使用已棄用的'mysql'。改爲使用'mysqli'。 – Thaillie

+0

1你申報功能。 '函數hote_rate(){...沒有文檔準備在它的邊...}',然後在文檔準備就緒的情況下啓動它。 '$(document).ready({hotel_rate()})' –

+0

我還不知道mysqli。 :'( –

回答

0

你的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(:);錯過了引號它應該be var test = cat.split(':');

+0

現在控制檯說這個錯誤:Uncaught TypeError:無法讀取undefined屬性'split' –

+0

console.log'$(「#hotel_cat [」+ i +「]」)。val();'看看它會顯示什麼你在這裏顯示它 –

+0

它顯示「未捕獲的SyntaxError:意外的標識符」 –

0

hotel_rate()聲明應該是內部功能齊全,同時更換cat.split(:);通過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"); 
     } 
    } 
}); 
+0

仍然收到兩個錯誤。未捕獲的ReferenceError:hotel_rate未定義且未捕獲SyntaxError:意外的令牌: –

+0

嘗試準備好像這樣'$(function(){',請參閱我的更新回答 –

+0

未捕獲的ReferenceError:hotel_rate未定義且未捕獲SyntaxError:意外的令牌: –

0

以上兩個答案是另外,您必須在document.ready部分之外聲明函數,並且split函數參數中必須包含字符串。

這裏是我把工作代碼的例子根據您的concept.you也可以檢查粘貼在這個小提琴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(){ 
 
    \t 
 
\t \t \t var cat = document.getElementById("hotel_cat_1").value; 
 
     alert(cat); 
 
\t \t \t var test = cat.split(':'); 
 
\t \t \t var cat_cost = parseInt(test[1]); 
 
\t \t \t document.getElementById("add[1]").innerHTML="Rs: "+cat_cost+" Add"; 
 
\t \t } 
 
\t 
 
</script>

,你是通過貓的分割值「:」酒店獲取成本,使價值,你必須擁有價值:分離器。

相關問題