【问题标题】:how to fetch data in multiple div from database in codeigniter如何从codeigniter中的数据库中获取多个div中的数据
【发布时间】:2016-05-14 15:04:37
【问题描述】:

我是 codeigniter 的新手。我有一个带有以下列名称的表。

id month day year review source

表名是 testimonial。testimonial 表中有很多行。我希望所有的推荐详细信息都应该显示在单独的框中。我如何在单独的框中从数据库中获取数据。

我试过了:

//在控制器中

class edit_content extends CI_Controller {

    function edit_content()
    {
        parent::__construct();
        $this->load->model('editcontent_model');
        $this->load->helper('url');
        $this->load->library('acl');
        $this->data = $this->editcontent_model->get_contents();
    }
}

//在视图中

 <table>
    <tr>
        <th>Content</th>
    </tr>

    <?php foreach($this->data  as $r): ?>
        <tr>
            <tr><?php echo $r['review']; ?>
        </tr>
    <?php endforeach; ?>

<table>

//在模型中

class editcontent_model extends CI_Model {
    var $CI; 

    function editcontent_model(){
        parent::__construct();
    }

    function get_contents() {
        $this->db->select('review');
        $this->db->from('testimonials');
        $query = $this->db->get();
        return $result = $query->result();
        $this->load->view('edit_content/edit_content', $result);
    }
}

但我只用于一列。它不起作用我试图在一个框中获取所有列详细信息。请帮我解决这个问题。

我是codeigniter的新手,请帮我找到解决方案

【问题讨论】:

  • 您能简单介绍一下您的尝试吗?
  • 我有一个表。表名是 testimonials。在 testimonial 表中,我已经在问题中给出了一些列名。我想从 testimonial 表中获取数据并显示在不同的不同 div 或框中。一行三个框中的示例包含推荐详细信息(日期,推荐描述,评论人),然后在一行中的三个框等。
  • 我读了这个问题。我没有问你的问题是什么,我问的是你已经尝试过什么来达到这个结果。
  • 查看链接jsfiddle.net/27j65vsL/3/..in 这我只用于一列。它不工作我想要一个框中的所有列详细信息
  • jsfiddle.net/27j65vsL/3

标签: php html mysql codeigniter


【解决方案1】:

希望下面的代码对你有所帮助。

$query = $this->db->query('SELECT id, month,day,year,review,source FROM testimonials');

foreach ($query->result() as $row)
{
    echo $row->id;
    echo $row->month;
    echo $row->day;
    echo $row->year;
    echo $row->review;
    echo $row->source;
}

echo 'Total Results: ' . $query->num_rows();

在上面的代码中,我给出了简单的演示,现在如何根据您的要求和主题显示数据,您可以使用您的 div 以您想要的方式在不同的框或行中显示。希望能帮到你。

【讨论】:

  • dinesh 我知道这个过程。主要问题是我不知道如何以正确的方式在一个 div 或框中显示
  • @nushart 这意味着你是在设计视图中谈论,所以请告诉我你正在使用哪个主题,你已经做了什么,直到你能分享那个代码
【解决方案2】:

我认为您的问题是您使用了两个 &lt;tr&gt; 标签而不是 &lt;td&gt; 标签。

//In controller 
class edit_content extends CI_Controller {

    function edit_content()
    {
        parent::__construct();
        $this->load->model('editcontent_model');
        $this->load->helper('url');
        $this->load->library('acl');
        $this->data = $this->editcontent_model->get_contents();
    }
}

 // In view
 <table>
    <tr>
        <th>Content</th>
    </tr>

    <?php foreach($this->data  as $r): ?>
        <tr>
            <td><?php echo $r['review']; ?></td>
        </tr>
    <?php endforeach; ?>

<table>

// in model
class editcontent_model extends CI_Model {
    var $CI; 

    function editcontent_model(){
        parent::__construct();
    }

    function get_contents() {
        $this->db->select('review');
        $this->db->from('testimonials');
        $query = $this->db->get();
        return $result = $query->result();
        $this->load->view('edit_content/edit_content', $result);
    }
}

如果您想打印所有列,请查看下面的代码

//In controller 
class edit_content extends CI_Controller {

    function edit_content()
    {
        parent::__construct();
        $this->load->model('editcontent_model');
        $this->load->helper('url');
        $this->load->library('acl');
        $this->data = $this->editcontent_model->get_contents();
    }
}

 // In view
 <table>
    <tr>
        <th>Content</th>
    </tr>

    <?php foreach($this->data  as $r): ?>
        <tr>
            <?php foreach($r as $v): ?>
            <td><?php echo $r['review']; ?></td>
            <?php endoforeach; ?>
        </tr>
    <?php endforeach; ?>

<table>

// in model
class editcontent_model extends CI_Model {
    var $CI; 

    function editcontent_model(){
        parent::__construct();
    }

    function get_contents() {
        $this->db->select('review');
        $this->db->from('testimonials');
        $query = $this->db->get();
        return $result = $query->result();
        $this->load->view('edit_content/edit_content', $result);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2016-12-16
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多