Ajax/JSON 检索和数组为从数据库检索的 n 条笔记创建 n 条 div(php、sql)
我还没有找到处理“php 数组 ->”这一侧的教程/解释jQuery 的问题。 (或者我理解,我对这一切都是新手:S)
工作代码,但没有为n个笔记创建n个div
我在PHP文件中有工作SQL:
$sql = "SELECT * FROM notes WHERE username = '$uname' and notetype = 'todo'";
$result = mysql_query($sql,$conn);
$i = 0;
while($row = mysql_fetch_assoc($result))
{
$notes[$i] = $row['note'];
echo json_encode($notes[$i]);
$i += 1;
}
它将成功回显到div/DOM(!?)元素通过 Ajax:
$.ajax({
url:'getnotestodo.php',
data: "",
datatype: 'json',
success: function(data)
{
$('<div class="stickit" />').text(data).appendTo('#stickitholder');
}
});
但显然我遇到的麻烦是它全部回显到一个新的
即“note1”“note2”note3”等 -每个音符都放入一个 div 中,而不是 div 上创建一个新的 div。当前的想法(失败的想法......)
所以我尝试用 PHP 数组填充 jQuery 数组,以便运行“while”循环客户端在每个新的 索引与该索引的数据 - 这个过程,但在一个循环中:
$('<div class="stickit" />').text(data).appendTo('#stickitholder');
我曾想象整个 PHP 数组上的 json_encode() 会返回一个对象,jquery 将能够使用其中一个地图() 类型的函数。我遇到了可怕的脚本故障,导致糟糕的故障排除尝试和崩溃,并且完全陷入了困境
PHP:
$sql = "SELECT * FROM notes WHERE username = '$uname' and notetype = 'todo'";
$result = mysql_query($sql,$conn);
$i = 0;
while($row = mysql_fetch_assoc($result))
{
$notes[$i] = $row['note'];
$i += 1;
}
json_encode($notes[]);
略有不同 jQuery:
$.ajax({
url:'getnotestodo.php',
data: "",
datatype: 'json',
success: function(data)
{
$('<div class="stickit" />').text(data).appendTo('#stickitholder');
var notesarray = new Array();
notesarray = data;
while (notesarray.length >= 0){
$('<div class="stickit" />').text(data).appendTo('#stickitholder');
}
}
});
我正在学习,但我现在不会去任何地方,所以帮助将是巨大的。赞赏。
I've not found a tutorial/ explanation that deals with this side of the 'php array -> jQuery' issue. (Or one I understand, I'm new to all this :S)
Working code, but not creating n divs for n notes
I have working SQL in a PHP file:
$sql = "SELECT * FROM notes WHERE username = '$uname' and notetype = 'todo'";
$result = mysql_query($sql,$conn);
$i = 0;
while($row = mysql_fetch_assoc($result))
{
$notes[$i] = $row['note'];
echo json_encode($notes[$i]);
$i += 1;
}
Which will echo into a div/DOM(!?) element successfully through Ajax:
$.ajax({
url:'getnotestodo.php',
data: "",
datatype: 'json',
success: function(data)
{
$('<div class="stickit" />').text(data).appendTo('#stickitholder');
}
});
But obviously the trouble I'm having is that it is all echoed into the one new <div class="stickit">
i.e. "note1""note2"note3" etc. - every note into one div, rather than one new div for each new note.
Current thinking (failing thinking...)
So I am trying to populate a jQuery array with a PHP array, in order to run a "while" loop client side to create a new div on each index with that index's data - this process, but in a loop:
$('<div class="stickit" />').text(data).appendTo('#stickitholder');
I had imagined json_encode()
over the whole PHP array would return an object that jquery would be able to use one of the map()
type functions. But I'm having terrible script failures leading to terrible troubleshooting attempts and crashing and am at a complete dead end.
PHP:
$sql = "SELECT * FROM notes WHERE username = '$uname' and notetype = 'todo'";
$result = mysql_query($sql,$conn);
$i = 0;
while($row = mysql_fetch_assoc($result))
{
$notes[$i] = $row['note'];
$i += 1;
}
json_encode($notes[]);
slightly different jQuery:
$.ajax({
url:'getnotestodo.php',
data: "",
datatype: 'json',
success: function(data)
{
$('<div class="stickit" />').text(data).appendTo('#stickitholder');
var notesarray = new Array();
notesarray = data;
while (notesarray.length >= 0){
$('<div class="stickit" />').text(data).appendTo('#stickitholder');
}
}
});
I'm learning as I'm going but I'm not going anywhere now so help would be massively appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您面临的问题是因为您试图在一个响应中传递许多 json 对象。所以javascript将无法将其识别为json对象。
你的对象看起来像
现在你做对了(一点点)
你可以像这样拥有你的div。
现在,在您的 php 代码中,将此行 json_encode($notes[]) 更改为 json_encode($notes)
Problem you are facing is because you are trying to pass many json objects in one response. So javascript wont be able to recognize it as json object.
Your object will look like
Now you are doing it right(little bit)
You can have your divs like this.
NOw in your php code, change this line json_encode($notes[]) to json_encode($notes)