データベースから 1 つのレコードを表示するには? 基本的に、レコードのリストと、投票を示す各レコードへのリンクを含むページがあります。特定のレコードの投票リンクをクリックすると、そのレコードのみが表示されるようにします。どうすればいいですか?
ビューにすべてのレコードを表示するための私のコードは次のとおりです。
<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);
}
}