0

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

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

<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

4 回答 4

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页面时,将重定向到中指定的 urlhref

控制器

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

    //load the view
}
于 2012-10-04T05:13:30.263 回答
0

为了只获取一条记录,请使用WHERE 子句指定要从数据库中获取哪条记录。您可以使用 GET 参数来传递有关记录的独特信息,例如 ID。

于 2012-10-03T21:47:01.803 回答
0

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

于 2012-10-03T22:16:18.690 回答
0

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

<? echo '<a href="http://studweb.cosc.canterbury.ac.nz/~njp63/365/polls/index.php/user/view/"'.$polls_item['slug'].'">Vote</a>' ?>
于 2012-10-04T02:19:25.427 回答