简体   繁体   中英

Retrieve the image from the database using codeigniter

i want to fetch the content stored in my database on to the webpage, its working but the issue im facing from the below code is that, all the rows are getting fetched. but instead i want to fetch each row of the database on each of the different section. i mean to say is that, the content of 1st row of database should be displayed on the first row section of my view page and the content of 2nd row of database should be displayed on my second row section. please can any one help me. i dont know where im going wrong

        <!--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>
<div class="container-fluid">
          <h1>Hello World!</h1>
          <div class="row">
              <div class="col-sm-4">
                  <!-- first row should be displyed-->
                  <table>
                   <thead>
                       <tr>
                         <th>Title</th>
                         <th>Content</th>
                       </tr>
                   </thead>
                   <tbody>
                    <?php
                    if( !empty($results) ) {

                        foreach($results as $row) {
                            echo '<tr> <td>';
                            echo '<h3>'.$row->title.'</h3>';
                            echo '<p>'.$row->content.'</p>';
                            echo '</td></tr>';

                        }
                    }
                    ?>
                   </tbody>
                </table>

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

Yeah this logic is correct. But if you have also stored image inside table then you have to put img tag inside foreach() loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM