Codeigniter链接搜索结果

时间:2014-11-11 17:00:06

标签: codeigniter

我是CodeIgniter的新手,当我显示项目列表时遇到问题,我希望每个项目都是一个链接,因此当用户点击该特定项目时,它将显示该项目的详细信息。或者也许就我应该google的内容提出一些建议。我真的不知道应该搜索哪些术语或关键词。

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller 
{
    public function index()
    {
        $this->getListOfAgent();                    
    }  

    function getListOfAgent()
    {
        $this->load->model("agentDB_model");

        $data['results'] = $this->agentDB_model->getAllAgentInfo();

        $this->load->view("viewAgent_view", $data);
    }




    function userInformation()
    {           
        //how to i get the selected item which the user clicked and show the details accordingly?

    }
}

模型

<?php 

class agentDB_model extends CI_Model 
{
    function getAllAgentInfo()
    {
        $query = $this->db->query("SELECT * FROM user");

        return $query->result();
    }

    function addAgent($newAgent)
    {
        $this->db->insert("user", $newAgent);
    }

}

视图

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Welcome</title>
</head>
<body>

    <div id="content">
        <h1>Home Page</h1>
        <p>List Of Agent in database</p>
    </div>

    <?php
        foreach($results as $row)
        {
            echo "<a href = 'Site/userInformation'>$row->userName</a>";

            echo "</br>";   
            echo "</br>";   
        }



        //$this->load->controller(Site/userInformation);
    ?>


    <div id="footer">
        <p>Copyright (c) 2012 basicsite.com</p>
    </div>


</body>
</html>

1 个答案:

答案 0 :(得分:1)

您需要告诉Site/userInformation方法您想要哪些记录信息。您可以使用URI segments

因此,在您的视图中,更改以下行:

echo "<a href = 'Site/userInformation'>$row->userName</a>";

为:

echo "<a href = 'Site/userInformation/$row->userID'>$row->userName</a>";

然后,在您的控制器方法中,将参数添加到方法声明:

function userInformation($userID)
{           
    // now, you use the model to get the correct record from the db based on 
    // the $userID and display the information
}