0
votes

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>
2
Its correct logic. But first check you put htaccess to remove index.php from URL, Otherwise you should use site_url() function.rawathemant
are u storing image in your table? if yes then use img tag also within loopPradeep
i did not got u sirkeerthi patil
i mean how r u getting image name ,it is coming from table like first_content, second_content or it is fix namePradeep
you've statically defined an image path... why do you even need php code other than what is present in the img src, if you just want 1 image from upload folder to show?BILAL MALIK

2 Answers

0
votes

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

0
votes
<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>