檢索到的數據請到通過整個代碼JSON響應數據
這是我的MySQL表:
mysql> select imgurl from images where family="shoes";
+-------------------------------+
| imgurl |
+-------------------------------+
| images/zara/shoes/thumbnail |
| images/hermes/shoes/thumbnail |
| images/hermes/shoes/thumbnail |
| images/hermes/shoes/thumbnail |
+-------------------------------+
從上表IM檢索圖像的URL與這個jQuery代碼:
$(document).ready(function() {
$('ul.sub_menu a').click(function() {
$('#sliderid, .prodcls').fadeOut(4000);
var txt = $(this).text();
$.ajax({
type: 'POST',
url: 'thegamer.php',
data: {send_txt: txt},
datatype:'json',
success: function(data){
$('#pgwrapid').html(data);
}
});
});
});
這是PHP代碼,其從jQuery AJAX請求得到:
<?php
//Credentials
$server = "localhost";
$user = "root";
$db = "lemonx";
//Connect
$link = mysql_connect($server, $user);
//Select database
mysql_select_db($db, $link);
//Assemble query
$family = mysql_real_escape_string($_REQUEST['send_txt'], $link);
$query = "SELECT imgurl FROM images WHERE family='$family'";
//Query database
$result = mysql_query($query, $link);
//Output result, send back to ajax as var 'response'
$imgurl=array();
$i=0;
if(mysql_num_rows($result) > 0){
//Fetch rows
while($row = mysql_fetch_array($result)){
$imgurl[$i] = $row['imgurl'];
//echo $imgurl[$i];
$i+=1;
}
}
echo json_encode($imgurl);
?>
現在,會發生什麼,這是輸出在下面的jQuery選擇:
$('#pgwrapid').html(data);
輸出
["images\/zara\/shoes\/thumbnail","images\/hermes\/shoes\/thumbnail","images\/hermes\/shoes\/thumbnail"]
我的問題是:
- 爲什麼反斜槓這裏?
是否有任何代碼遍歷上面的輸出,並提取每個路徑,並將其插入圖像標籤是這樣的:
$('#pgwrapid').append("<img src='"imagepath"' alt='Thumbnail'/>");
代碼將是有用的。
不知道反斜槓是怎麼回事,但就創建新'
'元素而言,如果data是一個數組,然後使用'for'循環或jQuery [泛型迭代器函數'$。 each()'](http://api.jquery.com/jQuery.each/)每次處理一個數組的元素。 –
nnnnnn
2012-01-09 04:46:07
我用alert(typeof(data))檢查了它;它給出字符串 – sajid 2012-01-09 04:49:42
你有一堆已經回答的問題,顯然對你有幫助,但是你沒有[接受](http://u.sbhat.me/t6SXUH)。其他人也許不會傾向於幫助你。 – Sathya 2012-01-09 04:59:56