Php 在视图中使用JSON数据
我有一个非常复杂的PHP/HTML视图。然而,我最近更改了我的站点,使用AJAX自动刷新。它当前返回一个JSON数组,我需要使用该数据重现我的视图 以下是我的AJAX:Php 在视图中使用JSON数据,php,ajax,json,Php,Ajax,Json,我有一个非常复杂的PHP/HTML视图。然而,我最近更改了我的站点,使用AJAX自动刷新。它当前返回一个JSON数组,我需要使用该数据重现我的视图 以下是我的AJAX: $(function() { $( "#selectable" ).selectable({ selected: updatefilters, }); function updatefilters(ev, ui){ // get the selected filter
$(function() {
$( "#selectable" ).selectable({
selected: updatefilters,
});
function updatefilters(ev, ui){
// get the selected filters
var $selected = $('#selectable').children('.ui-selected');
// create a string that has each filter separated by a pipe ("|")
var filters = $selected.map(function(){return this.id;}).get().join("\|");
$.ajax({
type: "POST",
url: 'updatefilters',
dataType: 'json',
data: { filters: filters },
success: function(data){
var html = "<div id ='board'><table>";
for (i=0 ; i< data.length ; i++)
{
html += "<tr><td>" + data[i].subject + "</td><td>" + data[i].body + "</td></tr>";
}
html += "</table></div>";
$('#board').html(html);
}
});
}
});
$(函数(){
$(“#可选”)。可选({
选中:updatefilters,
});
函数更新过滤器(ev、ui){
//获取选定的筛选器
变量$selected=$('#可选')。子项('.ui selected');
//创建一个字符串,其中每个过滤器由管道(“|”)分隔
var filters=$selected.map(函数(){returnthis.id;}).get().join(\\\;);
$.ajax({
类型:“POST”,
url:'updatefilters',
数据类型:“json”,
数据:{filters:filters},
成功:功能(数据){
var html=“”;
对于(i=0;i
这是我的PHP文件:
<div id='board'>
<?php
if ($threads)
{
$count = count($threads);
$perpage = 17;
$startlist = 3;
$numpages = ceil($count / $perpage);
if ($page == 'home') {$page = 1;}
$threadstart = ($page * $perpage) - $perpage;
$i = 0;
echo "<table class='board'>
<tr>
<th width='5%'><img src='".base_url()."img/board/icons/category_icon.png' alt='category_icon'/></th>
<th width='5%'>Tag</th>
<th width='50%'>Subject</th>
<th width='7.5%'>Author</th>
<th width='7.5%'>Replies/Views</th>
<th width='15%'>Last Post</th>
</tr>";
foreach ($threads as $list)
{ $i++;
$thread_owner = $this->thread_model->get_owner($list['owner_id']);
$thread_id = $list['thread_id'];
$query = $this->db->query("SELECT f.tag FROM filter_thread AS ft
INNER JOIN filter AS f ON ft.filter_id = f.filter_id
WHERE thread_id = $thread_id");
$result = $query->result_array();
$trunc_body = $this->thread_model->get_reply_summary($thread_id);
$filter = "";
$filter2 ="";
$ent = "entertainment";
foreach ($result as $string)
{
if ($string['tag'] == $ent) {
$filter2 .= "<div id='entertainment'><p>";
$filter2 .= "<img src='".base_url()."img/board/icons/entertainment_icon.png' title='Entertainment' alt='Entertainment'/>";
$filter2 .= "</p></div>";
} else {
$filter2 .= "<div id='misc'><p>";
$filter2 .= "<img src='".base_url()."img/board/icons/misc_icon.png' title='Misc' alt='Misc'/>";
$filter2 .= "</p></div>";
$filter .= " [".$string['tag']."]";
}
}
if (!$result) { $filter = "" AND $filter2 =""; }
if ($i > $threadstart AND $i <= $threadstart + $perpage)
{
echo "<tr>";
echo "<td>".$filter2."</td>";
echo "<td>".$filter."</td>";
echo "<td title='".$trunc_body['0']['body']."'><a href=".base_url()."main/thread/".$list['slug'].">".ucfirst($list['subject'])."<div id='filter'><p></p></div></a></td>";
echo "<td><p>".$thread_owner."</p></td>";
echo "<td align='right'>Replies: ".$list['replies_num']."<br>Views: ".$list['views']."</td>";
$owner = $this->thread_model->get_owner($list['last_post_id']);
echo "<td>".$owner." <br>".$list['replystamp'] ."</td></tr>";
}
}
echo "</table>";
echo "<br>";
echo "Current Page: $page | ";
echo "Page: ";
for ($n = 1; $n < $numpages+1; $n++)
{
echo "<a href=".base_url()."main/home/$n>$n</a> | ";
}
}
?>
</div>
尝试添加标题(“内容类型:text/plain”)代码>位于PHP文件的顶部
另外,将
移动到脚本中的变量中,在header()函数之后
此外,将所有echo
更改为变量,将这些变量放入数组中,然后使用
json_encode($array);
例如:
$output = "<div id='board'>";
$output .= "<table class='board'>
<tr>
<th width='5%'><img src='".base_url()."img/board/icons/category_icon.png' alt='category_icon'/></th>
<th width='5%'>Tag</th>
<th width='50%'>Subject</th>
<th width='7.5%'>Author</th>
<th width='7.5%'>Replies/Views</th>
<th width='15%'>Last Post</th>
</tr>";
echo json_encode( array('output'=>$output) );
$output=”“;
$output.=”
标签
主题
作者
答复/意见
最后一篇文章
";
echo json_编码(数组('output'=>$output));
尝试添加标题(“内容类型:text/plain”)代码>位于PHP文件的顶部
另外,将
移动到脚本中的变量中,在header()函数之后
此外,将所有echo
更改为变量,将这些变量放入数组中,然后使用
json_encode($array);
例如:
$output = "<div id='board'>";
$output .= "<table class='board'>
<tr>
<th width='5%'><img src='".base_url()."img/board/icons/category_icon.png' alt='category_icon'/></th>
<th width='5%'>Tag</th>
<th width='50%'>Subject</th>
<th width='7.5%'>Author</th>
<th width='7.5%'>Replies/Views</th>
<th width='15%'>Last Post</th>
</tr>";
echo json_encode( array('output'=>$output) );
$output=”“;
$output.=”
标签
主题
作者
答复/意见
最后一篇文章
";
echo json_编码(数组('output'=>$output));
谢谢你的开始。如何获取JSON数据?您希望在JSON中发送HTML,还是希望采用完全数据驱动的方法?如果您只发送JSON格式的原始数据,并在前端处理在HTML中插入数据的操作,那么您就可以将所有HTML从PHP中删除。感谢您的帮助。如何获取JSON数据?您希望在JSON中发送HTML,还是希望采用完全数据驱动的方法?如果您只发送JSON格式的原始数据,并在前端处理在HTML中插入数据的操作,那么您可以将所有HTML从PHP中删除。我想让我感到困惑的是,我目前没有将javascript与HTML放在同一视图中。rickymason.net/letschat/main/home Ricky,您需要的是一个好的HTML模板解决方案,一个类似于mustache的解决方案,用于指示数据在HTML模板中的位置<代码>{subject:“主题”,“作者”:“John”}
然后可以进入模板{Author}{subject}
。我不具备PHP知识,无法为您提供一个非常好的示例,但您可以从这里开始。Google mustache或HTML模板。我想让我困惑的是,我目前没有与HTML相同的视图中的javascript。rickymason.net/letschat/main/home Ricky,您需要的是一个好的HTML模板解决方案,一个类似于mustache的解决方案,用于指示数据在HTML模板中的位置<代码>{subject:“主题”,“作者”:“John”}
然后可以进入模板{Author}{subject}
。我不具备PHP知识,无法为您提供一个非常好的示例,但您可以从这里开始。谷歌胡子或HTML模板。