0

我有 3 张桌子

|forum_id|name_forum|
|   1    |forum 1   |

|thread_id|name_thread|forum_id|
|    1    |Thread 1   |   1    |

|post_id|name_post|text|thread_id|
|   1   | post 1  |tttt|   1     |
  • 我建立第 1 页以显示 forum_name
  • 第 2 页按 forum_id 显示 name_thread 组(我可以按 FORUM_ID 插入 NAME_THREAD 组吗?)
  • 第 3 页按 thread_id 显示 name_post 和文本组(我可以插入 NAME_POST GROUP_BY THREAD_ID 吗?)

我用codeigniter构建。我可以显示所有这些问题,但我不能插入线程和帖子。

class Forum extends CI_Controller{
    private $limit = 10;

    function __construct() {
        parent::__construct();
        //load library helper
        $this->load->library(array('table','form_validation'));
        $this->load->helper(array('form','url'));
        $this->load->model('home_forum_model','',TRUE);
        $this->load->helper('date');
        $this->is_logged_in();
    }

    function index($offset=0,$order_column='forum_id',$order_type='ASC')
    {
        if(empty($offset)) $offset=0;
        if(empty($order_column)) $order_column='forum_id';
        if(empty($order_type)) $order_type='asc';
        //Untuk cek validasi kolom

        //tanggal
        $data['datestring'] ="Tanggal: %d  %M  %Y  ";
        $data['time']=time();
        $data['zona']= timezones('UP7');

        //load data siswa
        $forums = $this->home_forum_model->get_paged_list($this->limit,$offset,$order_column,$order_type)->result();
        //pagination
        $this->load->library('pagination');
        $config['base_url']= site_url('home/forum/index/');
        $config['prev_link']='&ltPrev';
        $config['next_link']='Next&gt';
        $config['first_link']='&lt&ltFirst';
        $config['last_link']='Last&gt&gt';
        $config['total_rows']= $this->home_forum_model->count_all_forum();
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = 4;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        //table data
        $this->load->library('table');
        $this->table->set_empty('&nbsp');
        $tmpl = array ( 'table_open'  => '<table border="10" style="color:black;align:left" cellpadding="2" cellspacing="2" class="mytable">' );
        $this->table->set_template($tmpl);
        //$new_order = ($order_type == 'asc'?'desc':'asc');

        $this->table->set_heading(
                'Nama Forum');
        //$i=0+$offset;
        foreach($forums as $forum)
        {
            $this->table->add_row(
                    anchor('home/forum/view_thread/'.$forum->forum_id,$forum->nama_forum,array('class'=>'view_thread') )

                    );
            $this->table->add_row($forum->deskripsi);
        }


        $data['table']=$this->table->generate();
        $data['content']='home/tampil_forum';
        $this->load->view('template_home/template',$data);
    }

function view_thread($id)
    {
        //tanggal
        $data['datestring'] ="Tanggal: %d  %M  %Y  ";
        $data['time']=time();
        $data['zona']= timezones('UP7');

        $data['title']='Detail Forum';
        $data['action']=site_url('home/forum/view_thread/');
        $data['link_back']=anchor('home/forum/index/','Lihat Daftar Forum',array('class'=>'back'));

        //ambil detail agenda
        $data['threads']=$this->home_forum_model->get_thread_by_id($id)->result();

        //load agenda
        $data['content']='home/tampil_thread';
        $this->load->view('template_home/template',$data);
    }

这个型号

类 Home_forum_model 扩展 CI_Model{ private $primary_key = 'forum_id'; 私人 $table_name = '论坛';

function __construct() {
    parent::__construct();
}

function get_paged_list($limit=10,$offset=0,$order_column='',$order_type='asc')
{
    if(empty($order_column) || empty($order_type))
    {
        $this->db->order_by('forum_id','asc');
    }else{
        $this->db->order_by($order_column,$order_type);
        return $this->db->get('forum',$limit,$offset);
    }
}
function get_forum()
{
    return $this->db->get('forum');
}

function count_all_forum(){
    return $this->db->count_all($this->table_name);
}

function count_all_thread(){
    return $this->db->count_all('thread');
}

function count_all_post(){
    return $this->db->count_all('post');
}

function get_post_by_id($id)
{
    $this->db->select('post.*,post.nama_post as judulpost,post.text as isipost');
    $this->db->from('post');
    $this->db->join('thread','post.thread_id=thread.thread_id');
    $this->db->where('post.thread_id',$id);
    $this->db->order_by('post.post_id','asc');
    return $this->db->get();
}

function get_thread_by_id($id)
{
    $this->db->select('thread.*,thread.title as title,forum.nama_forum as forum');
    $this->db->from('thread');
    $this->db->join('forum','thread.forum_id = forum.forum_id');
    $this->db->where('thread.forum_id',$id);
    $this->db->order_by('thread.thread_id','desc');
    return $this->db->get();
}
4

0 回答 0