首页 > 解决方案 > 使用 codeigniter 从数据库中检索图像

问题描述

我想将存储在我的数据库中的内容提取到网页上,它可以工作,但我从下面的代码中面临的问题是,所有的行都被提取了。但相反,我想在每个不同的部分获取数据库的每一行。我的意思是,第一行数据库的内容应该显示在我的视图页面的第一行部分,第二行数据库的内容应该显示在我的第二行部分。请任何人都可以帮助我。我不知道我哪里出错了

        <!--controller-->
        <?php  
        defined('BASEPATH') OR exit('No direct script access allowed');  

        class Home extends CI_Controller {  

            public function __construct() 
            {
                parent::__construct();

                //load database libray manually
                $this->load->database();

                //load Model
                $this->load->model('Contact_model');

                // load form and url helpers
                $this->load->helper(array('form', 'url'));

                // load form_validation library
                $this->load->library('form_validation');
            }

               function deesha()
            {
                $this->load->model("Contact_model");
                $data['results'] = $this->Contact_model->getAllRecords10();
                $this->load->view('homeview',$data);
            }
        }  
        ?>

            <!--model-->
        <?php
            class Contact_model extends CI_Model 
            {

                  function getAllRecords10()
                {
                    //$this->load->library("database");
                    $results = array();
                    $this->db->select('title,content');
                    $this->db->from('test');
                    //$this->db->limit(1);
                    $q = $this->db->get();
                    if($q->num_rows() > 0)
                    {
                        $results= $q->result();
                    }
                    return $results;
                }
            }
        ?>


            <!--view-->
        <body>

        <div class="container-fluid">
          <h1>Hello World!</h1>
          <div class="row">
              <div class="col-sm-4">
                  <!-- first row should be displyed-->
                  <table>
                    <?php
                    if( !empty($results) ) {

                        foreach($results as $row) {
                            echo '<tr>';

                            echo '<h3>'.$row->title.'</h3>';
                            echo '<p>'.$row->content.'</p>';
                            echo '</tr>';

                        }
                    }
                    ?>
                </table>

              </div>
              <div class="col-sm-4">
                  <!-- second row should be displyed -->
                  <table>
                    <?php
                    if( !empty($results) ) {

                        foreach($results as $row) {
                            echo '<tr>';

                            echo '<h3>'.$row->title.'</h3>';
                            echo '<p>'.$row->content.'</p>';
                            echo '</tr>';

                        }
                    }
                    ?>
                </table>


              </div>
          </div>
        </div>

        </body>

标签: phpcodeigniter

解决方案


是的,这个逻辑是正确的。但是,如果您还在 table 中存储了图像,那么您必须将 img 标签放在 foreach() 循环中。


推荐阅读