Code Igniter显示数据库中的一条记录

时间:2012-10-03 21:37:04

标签: javascript database codeigniter

如何从数据库中显示一条记录?基本上我有一个页面,其中包含记录列表和每个记录投票的链接。当我点击特定记录的投票链接时,我希望它只显示该记录。我该怎么做?

我在视图中显示所有记录的代码是:

<h2>List of all polls in this site:</h2>
<?php echo '<table>';?>
<h3>Title</h3>
<?php foreach ($polls as $polls_item): ?>

  <tr><td><a href="http://studweb.cosc.canterbury.ac.nz/~njp63/365/polls/index.php/user">Vote</a>

<?php 

    echo $polls_item['title'] . "</td>";?>
    <div id="main">
        <?php echo "<td>" .  $polls_item['text'] . "</td>";

?>
    </div>
   <td><a href="#">Delete</a></td></tr>


<?php endforeach ?>
<?php echo "</table>"; ?>

我的模特:

<?php
class Polls_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
        $this->load->helper('url');
    }

    public function get_polls($slug = FALSE)
    {
        if ($slug === FALSE)
        {
            $query = $this->db->get('polls');
            return $query->result_array();
        }

        $query = $this->db->get_where('polls', array('slug' => $slug));
        return $query->row_array();
    }

    public function set_polls($title, $text)
    {

        $slug = url_title($this->input->post($title), 'dash', TRUE);

        $data = array(
            //'title' => $this->input->post('title'),
            //'slug' => $slug,
            //'text' => $this->input->post('text')

            'title' => $title,
            'slug' => $slug,
            'text' => $text
        );

        $this->db->insert('polls', $data);
        return $this->db->insert_id();

    }

    public function set_options($option, $pollid)
    {

        $values = $option;

        foreach ($values as $option){

            $options = array(
                'option_item' => $option,
                'poll_id' => $pollid
            );
            $this->db->insert('pollOption', $options);
        }
    }
}

我的控制器:

<?php

// Debugging
error_reporting(E_ALL);
ini_set('display_error', '1');

class User extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('polls_model');
    }

    public function index()
    {
        $data['polls'] = $this->polls_model->get_polls();
        $this->load->helper('html');
        $data['content'] = $this->load->view('user/index', $data, TRUE);
        $data['title'] = 'Polls archive';
        $this->load->view('templates/master', $data);
    }

    public function view($slug)
    {
        $data['polls_item'] = $this->polls_model->get_polls($slug);

        if (empty($data['polls_item']))
        {
            show_404();
        }

        $this->load->helper('html');
        $data['content'] = $this->load->view('user/view', $data, TRUE);
        $data['title'] = $data['polls_item']['title'];
        $this->load->view('templates/master', $data);
    }
}

4 个答案:

答案 0 :(得分:1)

查看

<?php foreach ($polls as $polls_item): ?>

  <tr><td><a href="siteurl/controller/method/{$polls_item['id']}">Vote</a>

<?php 

    echo $polls_item['title'] . "</td>";?>
    <div id="main">
        <?php echo "<td>" .  $polls_item['text'] . "</td>";

?>
    </div>
   <td><a href="#">Delete</a></td></tr>


<?php endforeach ?>

当您点击Vote时,该页面将重定向到href

中的空格网址

<强> Controlller

function method()
{
    $data   =   array();
    $data['record'] =   $this->db->get_where('tbl_name', array('id' => (int)$this->uri->segment(3)))->row_array();  

    //load the view
}

答案 1 :(得分:0)

为了只获取一条记录,请使用WHERE clause指定您希望从DB获取哪条记录。您可以使用GET参数传递有关记录的独特内容,例如ID。

答案 2 :(得分:0)

您能在这里向我们展示您的控制器和型号吗?您需要在模型中使用WHERE子句查询该单个结果的方法。

答案 3 :(得分:0)

您需要将item_id传递到视图中每行的投票链接:

<? echo '<a href="http://studweb.cosc.canterbury.ac.nz/~njp63/365/polls/index.php/user/view/"'.$polls_item['slug'].'">Vote</a>' ?>