Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将查询结果应用于PHP/jQuery新闻提要_Php_Jquery - Fatal编程技术网

将查询结果应用于PHP/jQuery新闻提要

将查询结果应用于PHP/jQuery新闻提要,php,jquery,Php,Jquery,我有一个用于票务信息系统的新闻提要。新闻提要的效果与Facebook的新闻提要相同,只有在有新内容需要获取时才会获取数据 我在理解如何将SQL查询结果应用于多维数组时遇到了一个问题,多维数组将在JSON函数中用于返回要添加到新闻提要的其他内容。代码如下: feed.php <? php $array_with_news = array( 'news1' => array('pk' => 'pk1', 'title' => 'title1', 'co

我有一个用于票务信息系统的新闻提要。新闻提要的效果与Facebook的新闻提要相同,只有在有新内容需要获取时才会获取数据

我在理解如何将SQL查询结果应用于多维数组时遇到了一个问题,多维数组将在JSON函数中用于返回要添加到新闻提要的其他内容。代码如下:

feed.php

<? php
    $array_with_news = array(
        'news1' => array('pk' => 'pk1', 'title' => 'title1', 'content' => 'title2'), 
        'news2' => array('pk' => 'pk2', 'title' => 'title2', 'content' => 'content2')
    );
    echo json_encode($array_with_news);
?> 
// store the last updated time in the db, get it here
$last_update = $user->get_last_update();

// obviously, this probably has a lot more logic
$sql = "SELECT * FROM news WHERE post_time > $last_update";
$news = array();

while($row = query($sql))
{
    $key = 'news'.count($news);
    $news[$key] = array(
        'pk' => $row->pk, 
        'title' => $row->title, 
        'content' => $row->content
   );
}

echo json_encode($news);

query('UPDATE users SET last_update_time=now()');
main.php

<html>
    <head>
        <script type="text\javascript">
            window.setInterval(function() {
                updateNews();
            }, 5000);


            function updateNews() {
                var scope = this;
                $.getJSON('newsFeed.php*', function(data) {
                    //data will contain the news information
                    $.each(data, function(index, value) {
                        this.addNewsRow(value);
                    });
                });
            }

            function addNewsRow(newsData) { 
                //this function will add html content 
                var pk = newsData.pk, title = newsData.title, content = newsData.content;
                $('#news').append(pk + ' ' + title + ' ' + content);
            }
        </script>
    </head>
    <body>
        <div id="news"></div>
    </body>
</html>
包含提要信息的表如下所示

主键、标题、类别、内容


而且,我只想在需要获取新数据时获取它

不确定您到底在问什么,但听起来您想知道如何将SQL结果输入到JSON数组中,2、您只想获取更新。这是我的看法

feed.php

<? php
    $array_with_news = array(
        'news1' => array('pk' => 'pk1', 'title' => 'title1', 'content' => 'title2'), 
        'news2' => array('pk' => 'pk2', 'title' => 'title2', 'content' => 'content2')
    );
    echo json_encode($array_with_news);
?> 
// store the last updated time in the db, get it here
$last_update = $user->get_last_update();

// obviously, this probably has a lot more logic
$sql = "SELECT * FROM news WHERE post_time > $last_update";
$news = array();

while($row = query($sql))
{
    $key = 'news'.count($news);
    $news[$key] = array(
        'pk' => $row->pk, 
        'title' => $row->title, 
        'content' => $row->content
   );
}

echo json_encode($news);

query('UPDATE users SET last_update_time=now()');

把这更多地当作psuedo代码——看看你的代码应该如何流动。如果我误解了这个问题,那么我将修改我的答案。

不确定你到底在问什么,但听起来你想知道如何将SQL结果输入到JSON数组中,2,你只想获取更新。这是我的看法

feed.php

<? php
    $array_with_news = array(
        'news1' => array('pk' => 'pk1', 'title' => 'title1', 'content' => 'title2'), 
        'news2' => array('pk' => 'pk2', 'title' => 'title2', 'content' => 'content2')
    );
    echo json_encode($array_with_news);
?> 
// store the last updated time in the db, get it here
$last_update = $user->get_last_update();

// obviously, this probably has a lot more logic
$sql = "SELECT * FROM news WHERE post_time > $last_update";
$news = array();

while($row = query($sql))
{
    $key = 'news'.count($news);
    $news[$key] = array(
        'pk' => $row->pk, 
        'title' => $row->title, 
        'content' => $row->content
   );
}

echo json_encode($news);

query('UPDATE users SET last_update_time=now()');
把这更多地当作psuedo代码——看看你的代码应该如何流动。如果我误解了这个问题,那么我将修改我的答案