2013-08-01 62 views
-2

使用,因爲我有定義代碼評級這樣如何從PHP中獲取價值,並在javascript

<?php foreach($rating as $ratingValue): 
$total = @round($ratingValue->TOT_RATING/$ratingValue->TOTAL_VOTER ,1); 
echo $total."/10"; 
endforeach; 
?> 

從上面的PHP代碼,它會給總ratin然後

開始的JavaScript到JavaScript需要的總額定值爲$總在那裏我有以下

<script language="javascript" type="text/javascript"> 

var totalRating = <?php echo $total;?>; 
var baseUrl = "<?php echo base_url();?>"; 
$(function() { 
$("#rating_simple1").webwidget_rating_sex({ 
rating_star_length: '5', 
rating_initial_value: totalRating, 
rating_function_name: '', //this is function name for click 
directory: baseUrl+'./assets/frontend/images/' 
}); 
}); 
</script> 

定義,當我運行它,它會LIK這個

顯示錯誤信息10
<script language="javascript" type="text/javascript"> 

var totalRating = <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;"> 

<h4>A PHP Error was encountered</h4> 

<p>Severity: Notice</p> 
<p>Message: Undefined variable: total</p> 
<p>Filename: views/detail.php</p> 
<p>Line Number: 69</p> 

</div>; 
var baseUrl = "http://localhost/aka/"; 
$(function() { 
$("#rating_simple1").webwidget_rating_sex({ 
rating_star_length: '5', 
rating_initial_value: totalRating, 
rating_function_name: '', //this is function name for click 
directory: baseUrl+'./assets/frontend/images/' 
    }); 
    }); 
    </script> 

請幫我解決這個問題,這樣我可以做我的評價

+0

「消息:未定義的變量:總」,「線編號:69「;也許你應該檢查第69行,看看你是否真的定義了總數。 – Jackson

+0

更好地在HTML中的隱藏字段中設置值,並在您的Javascript代碼中使用jQuery獲取該隱藏字段的值 –

+0

請參閱行號處的總變量:69它未定義表示未聲明,並且您正在使用此變量代碼 –

回答

0
$ratingValue->TOT_RATING 

TOT_RATING <<< explain this please 

$ratingValue->TOTAL_VOTER 

TOTAL_VOTER <<< explain this please 

$total = @round($ratingValue->TOT_RATING/$ratingValue->TOTAL_VOTER ,1); 

您使用的是(的foreach)變量,類

+0

TOT_RATING是表中的字段名稱TOTAL_VOTER是所有TOT_RATING – user2567647