iT邦幫忙

1

js 接php 從mysql讀出的字串出問題

  • 分享至 

  • xImage

非常抱歉,小弟初學,求大大幫提點。
最近coding遇到的障礙QQ
部分程式碼如下

如下所示,db_list存的是mysql附圖中的第一二行(都是浮點數),那接出來再push到js裡面的同名db_list,這部分沒有問題。

但一樣的方式db_gg雖有接收到sql內的第三四行(字串)卻會出問題沒辦法好好push

js好像也沒有明確規定型別,變數都是var所以頭好痛qq

https://ithelp.ithome.com.tw/upload/images/20180517/20109267d2Ltk2YKYk.pnghttps://ithelp.ithome.com.tw/upload/images/20180517/20109267Vr6pLDgQwh.pnghttps://ithelp.ithome.com.tw/upload/images/20180517/201092672Ef7wPI576.png


<?php
 
	$db_list = []; //資料庫清單
	$db_gg= [];
	//取值
	for($i=0 ; $i<($row_total) ; $i++){ //列
		array_push($db_list, ($db_data[$i][1]), ($db_data[$i][2]) );
	}
	for($j=0 ; $j<($row_total) ; $j++){ //列
		array_push($db_gg, ($db_data[$j][3]), ($db_data[$j][4]) );
	}	
	
	$db_list_length = count($db_list);
	
?>

<script type="text/javascript">

</script>


    <script>

	// In the following example, markers appear when the user clicks on the map.
	// Each marker is labeled with a single alphabetical character.
	var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var labelIndex = 0;

	var db_list_length = <?php echo($db_list_length); ?>;
    

	//console.log("資料數:", db_list_length);

	var db_list = [] //資料庫清單
    var db_gg=[]
	db_list.push(
	<?php
		for($i=0 ; $i<$db_list_length ; $i++){
			print_r($db_list[$i]);
			print(',');
		}
	?>
	);

	
	db_gg.push(
	<?php
		for($i=0 ; $i<$db_list_length; $i++){
			print_r($db_gg[$i]);
			
				print(',');
			
			
		}
	?>	
	
	);
weiclin iT邦高手 4 級 ‧ 2018-05-17 20:06:28 檢舉
哪有那麼麻煩...這樣就好了
var db_gg = <?= json_encode($db_gg) ?>;
liao86221 iT邦新手 5 級 ‧ 2018-05-18 12:24:10 檢舉
感謝,已解決
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答