【问题标题】:read more function with codeigniter使用 codeigniter 阅读更多功能
【发布时间】:2015-06-15 12:44:20
【问题描述】:

我正在尝试在 codeigniter 中创建一个 readmore 函数,其中 readmore 链接将链接到一个控制器,该控制器将显示有关该特定 id 的所有数据......我对如何去做有点困惑...... . 我试过了...

<?php 
                    $research_detail_url = site_url()."/research/research_details";
                    //echo json_encode($research)
                    if($research)
                    {
                        foreach ($research as  $_research) {
                            $author = $_research->author;
                            $content = $_research->content;
                            $dsubmitted = $_research->dsubmitted;
                            echo "<div class='menu-collapse'> <h5>$author</h5>";
                            echo "<p>";
                            echo "<span class='support_text'>$content <span><br />";
                            echo "<span class='support_text'>$dsubmitted <span><br />";
                            echo "<a href='$research_detail_url' target='_blank' style='text-decoration:underline; color:#0088cc;'>
                            read more &raquo; </a>";
                            echo "</p> </div>";
                        }
                    }
                    ?>

但我似乎没有得到任何结果...我需要帮助... 这是我的控制器功能.....

public function research_details($id='')
    {
        if(!$id)
        {
            echo "Project Id required";
            return;
        }   


        $_result = $this->projects_model->get_project($id);
        if($_result)
        {// success in fetching data hurray

            $result['projects'] = $_result;
            $users_ids = $this->users_model->get_user_ids(); //return available user id's
            $groups_ids = $this->groups_model->get_group_ids(); //return available group id's

            //echo json_encode($users_ids);
            //echo json_encode($groups_ids);
            $group_record = $this->map_names_to_ids($users_ids , $groups_ids );
            $result['group_record'] = $group_record;




            //load the view
            $this->load->view('__includes__/header');
            $this->load->view('__includes__/boostrap_responsive');
            $this->load->view('projects/project_panel', $result);
            $this->load->view('__includes__/footer_scripts');
            $this->load->view('__includes__/wijmo_file_jquery');
            $this->load->view('__includes__/footer');
        }
        else
        {
            exit("An Error occured in fetching the requested project");
        }

    }

这是我的模型.....

<?php

class research_model extends CI_Model {


    function add()
    {
        $this->db->insert('research',$_POST);
        if($this->db->_error_number())
        {
            return $this->db->_error_number();
        }
    }

    function update($article_id, $data_fields = NULL){
        if($data_fields == NULL)
        {
            $this->db->where("article_id =".$article_id);
            $this->db->update('research',$_POST);
        }
        else
        {
            $this->db->where("article_id =".$article_id);
            $this->db->update('research',$data_fields);
        }

        $is_error = $this->db->_error_number();
        if($is_error){
            echo $is_error;
        }

        return TRUE;
    }

    function delete($id){
        $this->db->where("article_id =".$id);
        return $this->db->delete('research');
    }

    //return the research with this id
    function get_research($id){
        $this->db->where("article_id =".$id);
        $query = $this->db->get('research');
        if ($query->num_rows() > 0){
            return $query->row_array();
        }
        else
            echo $this->db->_error_message();
            return FALSE;
    }

    //return the available research in the table
    function get_research_all(){

        $query = $this->db->get('research');
        if ($query->num_rows() > 0)
        {
            foreach($query->result() as $row)
            {
                $result[]   =   $row;
            }
            return $result;
        }
    }





}

还有我的整个控制器.....

<?php
class Research extends Public_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('research_model');       
    }

    function index()
    {
        if($this->ion_auth->is_admin())
        {

            $result =   $this->research_model->get_research_all();
            $data   =   array(
                'main_content'  =>  'research/index',
                'research'          =>  $result
            );
            $this->load->view("loader", $data);
        }
        else
        {
            redirect('home');
        }

    }//END INDEX


    // public view
    function current()
    {
        $result = $this->research_model->get_research_all();
        $data =  array('research' => $result); 

        $this->load->view('__includes__/header');
        $this->load->view('__includes__/navbar');
        $this->load->view('research/current', $data);
        $this->load->view('__includes__/footer');

    }

    function add()
    {
        if($this->ion_auth->is_admin())
        {
            $this->load->view("loader",array('main_content'=>"research/add_research"));
        }
    }//END ADD

    function edit($id='')
    {
        if(! $id)
        {
            echo "research Id required";
            return;
        }
        $result =   $this->research_model->get_research($id);
        if( ! $result)
        {
            echo "Nothing to edit";
            return;
        }
        $result['main_content'] =   "research/add_research";
        $this->load->view("loader",$result);
    }//END EDIT

    function delete($id='')
    {
        if(! $id)
        {
            echo "Id required";
            return;
        }
        $this->research_model->delete($id);
        $this->get_research();
    }//END DELETE

    function submit($id='')
    {

        //validate form [perform validation server-side to make sure of fields]
        $this->load->library('form_validation');
        $this->form_validation->set_rules('author', 'Author', 'trim|required|min_length[4]');       

        if ($this->form_validation->run() == FALSE){
            //ajax data array
            $data   =   array(
                'server_validation'     =>  validation_errors()
            );
            echo str_replace('\\/', '/', json_encode($data));
        }
        else{

            if($id){
                $result     =   $this->research_model->update($id);
                $content    =   "article has been UPDATED successfully";

                //$retArr["content"] = $content;
                //echo json_encode($retArr);
            }
            else{
                $result     =   $this->research_model->add();
                $content    =   "article has been CREATED successfully";
                //$retArr["content"] = $content;
                //echo json_encode($retArr);
            }
            //if duplicate key
            if($result == 1062){
                //ajax data array
                $data   =   array();
                    $data['is_valid']   =   0;

                echo json_encode($data);
            }else{
                //ajax data array
                $data   =   array(
                    'is_valid'  =>  1,
                    'content'   =>  $content
                );
                echo json_encode($data);
            }
        }//end ELSE form valid
    }//END SUBMIT

    public function research_details($id='')
    {
        if(!$id)
        {
            echo "Project Id required";
            return;
        }   


        $_result = $this->research_model->get_research($id);
        if($_result)
        {// success in fetching data hurray

            $result['article'] = $_result;



            //load the view
            $this->load->view('__includes__/header');
            $this->load->view('__includes__/boostrap_responsive');
            $this->load->view('research/research_details', $Aresult);
            $this->load->view('__includes__/footer_scripts');
            $this->load->view('__includes__/wijmo_file_jquery');
            $this->load->view('__includes__/footer');
        }
        else
        {
            exit("An Error occured in fetching the requested project");
        }

    }//END EDIT

}
?>

我的公共控制者

<?php 
abstract  class Public_Controller extends CI_Controller
{
    public $about_data;
    function __construct()
    {
        parent::__construct();

        //Making This variable availale for the whole site
        $this->load->model('about_model');
        $this->load->model('captcha_model');
        //get your data
        $this->about_data = $this->about_model->get_abouts();   


    }
}
?>

【问题讨论】:

  • 在您的foreach 循环中,您忘记关闭span 标签。你需要&lt;/span&gt;
  • 当您点击“阅读更多”链接时会发生什么?你看到任何错误吗?它会重定向到任何地方吗?
  • 另外,您打算如何将 ID 传递给控制器​​?到目前为止,您的视图似乎构建了没有 ID 的链接,因此当它到达您的控制器时,您将始终点击“需要项目 ID”
  • 我还会包含您的模型方法,以便我们可以查看那一侧是否正确编码
  • @CodeGodie 我将添加我的模型方法,是的,我的观点一直在点击“需要项目 ID”

标签: php codeigniter


【解决方案1】:

我发现了几个问题:

  1. 在您看来,您并没有关闭span 标签。你需要 &lt;/span&gt;

  2. 在您看来,您正在创建链接,但您不是 包括您在控制器中需要的 ID。我的意思是:

    首先,你创建这个$research_detail_url = site_url()."/research/research_details";

    然后echo "&lt;a href='$research_detail_url' target='_blank' style=''&gt;read more&lt;/a&gt;";

    如您所见,您的 URL 在创建时没有 ID。你应该做的是这样的事情:$research_detail_url = site_url()."/research/research_details/31"; 其中31 是一个随机 ID。您需要提供正确的 ID 才能将其传递给您的控制器。您的控制器当前正在分配一个空白 ID,因为没有传递任何内容,这就是您最终得到“需要项目 ID”结果的原因

【讨论】:

  • 我这样做了,我把一个特定的 ID 放在那里只是为了确定,我得到了这个错误是遇到无法加载请求的文件:research/research_details.php
  • 好吧 &lt;a href="" target="_blank"&gt; 将在您的浏览器上打开一个新页面或标签。它将查找“Research”控制器类,然后查找“research_details”控制器方法。如果你有An Error Was Encountered Unable to load the requested file: research/research_details.php 告诉我控制器没有被看到。您可以在代码中显示整个控制器及其名称吗?
  • 好的。我看到你的控制器类是从一个名为“Public_Controller”的父类扩展而来的。你能包括那个控制器的代码吗?
  • 我认为通过 Teamviewer 帮助您会更容易。有太多的因素。你想这样做吗?告诉我
  • 给我一分钟让我下载并安装它...我需要我能得到的所有帮助
猜你喜欢
  • 2014-01-20
  • 1970-01-01
  • 2017-07-11
  • 2010-11-18
  • 2018-08-08
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
相关资源
最近更新 更多