Reputation: 321
How to display one record from a database? Basically I have a page that has a list of records and a link for each record that says vote. When I click on the vote link for a particular record, I want it to display only that record. How would I do this?
My code for displaying all the records in the view is:
<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>"; ?>
My model:
<?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);
}
}
}
My controller:
<?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);
}
}
Upvotes: 0
Views: 268
Reputation: 31
View
<?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 ?>
when you click on Vote
the page will redirected to spacified url in 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
}
Upvotes: 1
Reputation: 1625
You need to pass item_id to the vote link of each row in your view:
<? echo '<a href="http://studweb.cosc.canterbury.ac.nz/~njp63/365/polls/index.php/user/view/"'.$polls_item['slug'].'">Vote</a>' ?>
Upvotes: 0
Reputation: 3643
Can you show us your controller and model here? You need to have a method in the model that will query for that single result with the WHERE clause.
Upvotes: 0
Reputation: 864
In order to get only one record, use WHERE clause to specify which record do you want to get from the DB. And you can use GET parameters to pass something unique about the record, e.g. ID.
Upvotes: 0