Welcome Guest, Not a member yet? Register   Sign In
Have problems with displaying data from database
#1

Can someone help me , I'm still new with CodeIgniter , I have problems in displaying data from database in another page because I am confused with controller either I should put on the function on default controller or made a new one, and if I made a new one, I don't know how to call the function to display the data from database on another page. 

this is my default controller : 

PHP Code:
<?php 

class Pages extends CI_Controller{
 
   public function __construct(){
 
       parent::__construct();
 
       $this->load->helper('url');
 
   }


    public function 
view($page 'home')
    {
        if ( ! 
file_exists(APPPATH.'/views/pages/'.$page.'.php'))
    {
        
// Whoops, we don't have a page for that!
        
show_404();
    }

    
$data['title'] = ucfirst($page); // Capitalize the first letter

    
$this->load->view('templates/header'$data);
    
$this->load->view('pages/'.$page$data);
    
$this->load->view('templates/footer'$data);

    }

public function 
GetAll(){
 
   $data['all_data'] = $this->exhibitions_model->selectAllData();
 
   $this->load->view('exhibitons'$data);
}

}

?>

and this is my model : 

PHP Code:
<?php
       
public function selectAllData() {
 
       $query $this->db->get('omg_events');
 
       return $query->result();
 
   }
?>


and the page that I want to display data from the database :

Code:
<head>
   <link href="<?php echo base_url() ?>css/about.css" rel="stylesheet">
</head>

<!--==========================
   Intro Section
 ============================-->

 <section id="intro">
   <div class="intro-container">
     <div id="introCarousel" class="carousel  slide carousel-fade" data-ride="carousel">


       <div class="carousel-inner" role="listbox">

         <div class="carousel-item active">
            <div class="carousel-background"><img src="<?php echo base_url() ?>img/events.jpg" alt=""></div>
           <div class="carousel-container">
             <div class="carousel-content">
               <h2>Exhibitions</h2>
             </div>
           </div>
         </div>

     </div>
   </div>
 </section><!-- #intro -->

 <main id="main">

   
   <!--==========================
     Team Section
   ============================-->
   <section id="team">
     <div class="container">
       <div class="section-header wow fadeInUp">
         

       <div class="album py-5 bg-light">
   <div class="container">

      <div class="section-header">
         <h3>EXHIBITIONS ROADSHOWS & SEMINAR</h3>
         <p></p>
       </div>

<table class="table table-hover">

 <thead>
   <tr>
   

   </tr>
 </thead>
 <tbody>
<?php

   foreach ($all_data as $show):
       ?>

   <tr>
     <th scope="row"><?php echo $show->event_id?></th>
     <td width="10%"><?php echo $show->name?></td>
     <td width="10%"><?php echo $show->address?></td>
     <td></td>
     <td></td>
     <td width="15%""><a href="">Send Enquiry</a></td>
   </tr>
  <?php

   endforeach;
   ?>
 </tbody>
</table>
     
     
     </div>

   </section><!-- #team -->


 
 </main>
Reply
#2

(This post was last modified: 01-28-2019, 09:45 AM by php_rocs.)

@firas,

You need to load the model in your controller ( https://codeigniter.com/user_guide/gener...ng-a-model ).

PHP Code:
public function __construct(){
 
       parent::__construct();
 
       $this->load->helper('url');
        
$this->load->model('Exhibitions_model');
 
   
Reply
#3

To view the results, you have to use this url:
https://www.yourwebsitename/pages/GetAll

But now you're missing the header and footer view.
You have 2 options:
1. Include the header and footer views in the GetAll function, like it's done in the "view" function of the "Pages" controller.
2. (better!) Create a MY_Controller.php in the applications/core folder. Inside that "base controller", create a function like "render_page()", that loads the header, the selected page and the footer for you. Let all your own controllers extend the MY_Controller. The render_page() function will be available in all controllers.

In MY_Controller:
PHP Code:
public function render_page($view$data)
{
 
  $this->load->view('template/header',$data);
 
  $this->load->view($view,$data);
 
  $this->load->view('template/footer',$data);


From the Pages controller:
PHP Code:
$this->load->model('exhibitions_model');
$data['all_data'] = $this->exhibitions_model->selectAllData();
$this->render_page('exhibitions',$data); 
Reply
#4

(01-28-2019, 11:25 AM)Wouter60 Wrote: To view the results, you have to use this url:
https://www.yourwebsitename/pages/GetAll

But now you're missing the header and footer view.
You have 2 options:
1. Include the header and footer views in the GetAll function, like it's done in the "view" function of the "Pages" controller.
2. (better!) Create a MY_Controller.php in the applications/core folder. Inside that "base controller", create a function like "render_page()", that loads the header, the selected page and the footer for you. Let all your own controllers extend the MY_Controller. The render_page() function will be available in all controllers.

In MY_Controller:
PHP Code:
public function render_page($view$data)
{
 
  $this->load->view('template/header',$data);
 
  $this->load->view($view,$data);
 
  $this->load->view('template/footer',$data);


From the Pages controller:
PHP Code:
$this->load->model('exhibitions_model');
$data['all_data'] = $this->exhibitions_model->selectAllData();
$this->render_page('exhibitions',$data); 

I have followed your ways but I can't call the other controller files (MY_Controller) into my main controller, so I combined
all the functions into one controller, it works but it can't find where were my exhibitions.php, its shows error "Unable to load the requested file: exhibitions.php"  


does that happen because of my view page? Did I call the right PHP inside the table to show the data? 

This is the Controller file that I have combined, pages.php : 


Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pages extends CI_Controller{
   public function __construct(){
       parent::__construct();
       $this->load->helper('url');
       $this->load->model('Exhibitions_model');
        $data['all_data'] = $this->Exhibitions_model->selectAllData();
       $this->render_page('exhibitions',$data);
   }


    public function view($page = 'home')
    {
        if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
    {
        // Whoops, we don't have a page for that!
        show_404();
    }

    $data['title'] = ucfirst($page); // Capitalize the first letter

    $this->load->view('templates/header', $data);
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/footer', $data);

    }

    public function GetAll(){
   $data['all_data'] = $this->exhibitions_model->selectAllData();
   $this->render_page('exhibitions',$data);

}

public function render_page($view, $data)
{
  $this->load->view('templates/header',$data);
  $this->load->view($view,$data);
  $this->load->view('templates/footer',$data);
}

}


and this is the view page : 

Code:
<head>
  <link href="<?php echo base_url() ?>css/about.css" rel="stylesheet">
</head>

<!--==========================
  Intro Section
============================-->

<section id="intro">
  <div class="intro-container">
    <div id="introCarousel" class="carousel  slide carousel-fade" data-ride="carousel">


      <div class="carousel-inner" role="listbox">

        <div class="carousel-item active">
           <div class="carousel-background"><img src="<?php echo base_url() ?>img/events.jpg" alt=""></div>
          <div class="carousel-container">
            <div class="carousel-content">
              <h2>Exhibitions</h2>
            </div>
          </div>
        </div>

    </div>
  </div>
</section><!-- #intro -->

<main id="main">

   
  <!--==========================
    Team Section
  ============================-->
  <section id="team">
    <div class="container">
      <div class="section-header wow fadeInUp">
       

      <div class="album py-5 bg-light">
  <div class="container">

     <div class="section-header">
        <h3>EXHIBITIONS ROADSHOWS & SEMINAR</h3>
        <p></p>
      </div>

<table class="table table-hover">

<thead>
  <tr>
   

  </tr>
</thead>
<tbody>
<?php

  foreach ($all_data as $show):
      ?>

  <tr>
    <th scope="row"><?php echo $show->event_id?></th>
    <td width="10%"><?php echo $show->name?></td>
    <td width="10%"><?php echo $show->address?></td>
    <td></td>
    <td></td>
    <td width="15%""><a href="">Send Enquiry</a></td>
  </tr>
 <?php

  endforeach;
  ?>
</tbody>
</table>
     
   
    </div>

  </section><!-- #team -->


 
</main>
Reply
#5

(01-28-2019, 09:39 AM)php_rocs Wrote: @firas,

You need to load the model in your controller ( https://codeigniter.com/user_guide/gener...ng-a-model ).

PHP Code:
public function __construct(){
 
       parent::__construct();
 
       $this->load->helper('url');
 
       $this->load->model('Exhibitions_model');
 
   

can you check my view page, my view page (exhibitions.php) because its keep on showing there is an error on my view page
Reply
#6

@firas,

Make sure you check the privileges of the view exhibitions.php. Is it 775? 777? Also, make sure that it is spelled exactly the same way. Also is exhibitions.php in the /application/views directory? Is it all lowercase? Don't forget that PHP is case sensitive.
Reply
#7

You're totally on the wrong track.
Did you start with reading (and understanding) the CI documentation and tutorial?

Your exhibitions.php goes into the application\views folder.

Don't load data and views in the construct of the controller.
The construct is just a function that runs every time there is a request to open one of the other functions in the controller.
In a controller that always needs a specific model or helper, you can load that in the construct.

Controller functions are called by the url in the browser.
E.g. www.mywebsite/products/overview/2019
products is the controller name (Products.php)
overview is the function name inside Products.php
2019 is an argument for the function, for instance if the function has this name:
PHP Code:
public function overview($year)
{
 
   // load data from a model here and pass the value of $year to the model to get the data for a specific year only.
 
   // load a view to display the data here, by passing an array with $data to the view.

Reply
#8

(01-29-2019, 09:31 AM)Wouter60 Wrote: You're totally on the wrong track.
Did you start with reading (and understanding) the CI documentation and tutorial?

Your exhibitions.php goes into the application\views folder.

Don't load data and views in the construct of the controller.
The construct is just a function that runs every time there is a request to open one of the other functions in the controller.
In a controller that always needs a specific model or helper, you can load that in the construct.

Controller functions are called by the url in the browser.
E.g. www.mywebsite/products/overview/2019
products is the controller name (Products.php)
overview is the function name inside Products.php
2019 is an argument for the function, for instance if the function has this name:
PHP Code:
public function overview($year)
{
 
   // load data from a model here and pass the value of $year to the model to get the data for a specific year only.
 
   // load a view to display the data here, by passing an array with $data to the view.


I'm sorry for your misunderstanding, I know where the location file to load data and views, I already solved this problem, the problem was that I didn't put my controller into the route.php in config, now I have a new problem for my pagination on the page, how can i delete this forum and make a new one ?
Reply
#9

If it's a new problem then create a new forum topic for it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB