如何显示数据库中的一条记录?基本上,我有一个页面,其中包含记录列表和每条记录的链接,上面写着投票。当我单击特定记录的投票链接时,我希望它只显示该记录。我该怎么做?
我在视图中显示所有记录的代码是:
<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);
}
}