Rich Client simple paging message board design based on JSON

Source: Internet
Author: User
Tags json

Originally thought that write a message board should be relatively easy things, never moved, this time to try to write a, feeling that is easy and not easy.

The easy aspect is that the structure of the message board is very simple, send data to the server-> save to the database-> return the new data to the browser-> refresh the page

Not easy place is a lot of detail to pay attention to, do not know where a small problem on the wrong.

This message board front desk uses the Flex, the file 200 many k, the initial loading is slower, the movement is fast. Background using PHP, with the Codeigniter framework, plus most of the work has a foreground to do, the entire background code less than 20 lines.

Front and rear communication uses the simplest form of HTTP page, the data is packaged in JSON, JSON greatly simplifies the workflow, extracts data from the database, converts it into an array of objects, encodes the array into a string, writes it to the page, and the whole process is OK, and at the front desk, the strings are parsed and restored to the object.

To see what the returned data looks like, click on the link below to see what page No. 0 returns, there are some hundred semicolons, and that's because all the strings are encoded:

http://myqiao.oxyhost.com/ci/index.php/bbs/action_show/0

Let's see how simple the code is.

function show($page_no)
    {
        //计算分页数
        $record_count=$this->db->count_all ('t_message');
        $page_count= Floor(($record_count-1)/PAGE_SIZE);
        if($page_count<0)
            $page_count=0;

        //从 t_message 表中查询当前页面留言
        $this->db->order_by("id", "desc");
        $query = $this->db->get('t_message', 10, 10*$page_no);

        //将前台需要的数据装配成数组
        $arr = array(
              //当前页面号
            'current_no' => "{$page_no}" ,
            'page_count' =>"{$page_count}" ,
            'record_count' =>"{$record_count}" ,

            //下面字段保存10条从数据空中查询来的留言信息, 字段包括id username , content, time
            'message' => $query->result_array()

        );
     //将数组编码成字符串
$json_string=$this->json->encode($arr);

     //响应给浏览器
        echo  $json_string;
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.