需要从数据库自动重新加载值

时间:2014-12-12 13:17:42

标签: php codeigniter

我遇到从数据库中获取自动值的问题。 使用PHP CodeIgnitor 我在我的主页中使用从我的数据库的学生表中加载的推荐。我知道如何展示整个推荐书或特定的推荐书。但请帮我自动重新加载并在10秒后显示每个推荐。这是我的代码:

控制器

public function index()

{
    $data=$this->data;
    $data['news']=$this->stthomasmodel->latestNews();
    $data['testimonials']=$this->stthomasmodel->testimonials();

    $this->load->view('index.php',$data);

}

模型

//Function for retreiving Testimomnials from the database
public function testimonials()
{
    $query=$this->db->query("SELECT testimonials FROM student");
    return $query->result();
}

查看

 $i=count($testimonials); for ($x = 0; $x <$i; $x++)    echo
 $testimonials[$x]->testimonials."<br>";

请建议我一个想法。 感谢

2 个答案:

答案 0 :(得分:0)

为此,您必须使用Ajax jquery。 加载学生数据特定div,例如div id = studentlist。 你可以使用load或get方法来检索学生数据ub studentlist div 这里get和load都是jquery函数

或者您可以在给定的时间段后使用HTML元标记重新加载页面

<meta http-equiv="refresh" content="5" />

此页面在5秒后自动刷新

答案 1 :(得分:0)

使用jquery setInterval ..

setInterval()方法以指定的时间间隔(以毫秒为单位)调用函数或计算表达式。

<script type="text/javascript">
  $(document).ready(function() {
    setInterval(get_data, 1000);
  });
  function get_data()
  {
    $.ajax({
    type: 'post',
    url: "<?php echo base_url().'controller/method' ?>",
      success: function(data) {
        $('#content').html(data.result);
      }
   });
  }
</script>

如果你想要你可以使用load方法而不是ajax ..

相关问题