【问题标题】:Code Igniter displaying one record from databaseCodeigniter 显示数据库中的一条记录
【发布时间】:2012-10-03 21:37:03
【问题描述】:

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

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

<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);
    }
}

【问题讨论】:

    标签: javascript database codeigniter


    【解决方案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 中的分隔网址

    控制器

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我将如何使用链接来做到这一点?
      • @NiraliPatel 您可以在链接末尾添加一个参数,因此它看起来像 helloworld.php?id=123 然后使用 $_GET['id'] 检索
      【解决方案3】:

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

      【讨论】:

      • 我已经添加了我的控制器和模型
      • 您的模型缺少类定义。没有它,构造函数方法将无法执行任何操作。
      • 嘿抱歉.. 好像我忘记粘贴了..:"
      【解决方案4】:

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

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

      【讨论】:

      • 我可以使用 .$polls_item['id']。我决定不使用蛞蝓。这是一个更好的主意吗?它会起作用吗?谢谢。
      • 继续跟踪您的用户/客户(并访问他们)的最佳方法是为每个人设置唯一标识符。如果你有,我强烈建议你使用它。例如,在此链接中将$polls_item['slug'] 切换为$polls_item['user_id']。因此,在您的 get_polls 函数中,您将检索与此 user_id 关联的数据
      猜你喜欢
      • 2015-06-13
      • 2021-09-27
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 2018-03-18
      • 1970-01-01
      相关资源
      最近更新 更多