2

I am new to codeigniter. I followed a video tutorial and successfully created a login registration system. After registration or login the users reach a page called members.php.

members.php is a view.

my controller is main.php and model is model-users.php.

I want members.php to show/have content from the database so that the users can read it once the login or register?

I followed a few resources on the web that said not to call controller from view. How can I accomplice the above without doing so?

Thanks

1
  • If an answer helped you, you should mark it as accepted. Commented Oct 14, 2013 at 11:02

5 Answers 5

10

I think the CodeIgniter documentation is actually very clear, and very easy to follow. Nevertheless, here's an example of how you might structure your app.

Controller:

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

class Members extends CI_Controller {
    function __construct() {
        parent::__construct();

        $this->load->model('members_model');
    }

    function index() {
        $data['members'] = $this->members_model->get_members();

        $this->load->view('header', $data);
        $this->load->view('members');
        $this->load->view('footer');
    }
}

Model:

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

class Members_model extends CI_Model {

    function get_members() {
        $query = $this->db->get('members');
        return $query->result_array();
    }
}

View:

<?php

print_r($members);

To access the above page, you would visit http://yoursite.com/index.php/members.

You can use a .htaccess file to remove the index.php segment from the URI. The index() method is automatically called when you run a controller.

Sign up to request clarification or add additional context in comments.

2 Comments

In this case are header and footer two separate pages?
They are separate templates. It's convention to separate templates this way so you can reuse the header and footer parts across your entire site, instead of having to include that code on every page.
1

I don't have any example of the code, but I write one simple example of HTML when you can show name and surname of members. Controller:

function members(){
  $members = $this->model-user->get_list(); //array of members
  $this->load->view('members.php', $members); // load members in view
}

View members.php

<html>
<body>
    <table>
      <tr>
        <th>Name</th>
        <th>Surname</th>
      </tr>
      <?php foreach($members as $m): ?>
      <tr>
        <tr><?php echo $m['name']; ?>
        <tr><?php echo $m['surname']; ?>
      </tr>
      <?php endforeach; ?>
    <table>
</body>
</html>

Model:

function get_list() {
   // query in database
   return $query = $this->db->get('members'); //table name members
}

This is the simple example in a short form. I hope that you will understand it.

4 Comments

if I put the function members() in a separate controller (i.e not in the main controller), how will the view recognize it?
I think you are missing the basics. Controller calls the view. Not reverse.
k, I think I get your point. Let me know if I am wrong: I can call the same view from different controller and pass data. The view will display all the data from all the controllers?
@StacyJ, Look, here I write a MVC pattern of CI. function members() is controller function, and here you call the model function and get members, and load in view. Function get_list() in model, and here you will do query to get list in array. In view you just load it in one table. It is simple example to show you how you can do a query, load in view and show it. I hope that you will understand my point.
1

I think i know what you mean, this is definitely not the right way to go about this but if you really must you could call a model from a view like this.
In the view

<div>

    <?php 
    #You could load the model here or autoload it in config->autoload->models(i believe)
    $data = $this->model_name->model_function();?>
    foreach($data as $row): 
           echo '<pre>';
           print_r($row);
           echo '</pre>';
    endforeach; 
    ?>

</div>

Comments

0

here is the example of my codes:

views:

                <?php foreach($ecms as $i){ ?>
                    <tr>
                    <td><?php echo $i->accnum; ?></td>
                    <td><?php echo $i->crdnum; ?></td>
                    <td><?php echo $i->fname; ?></td>
                    <td><?php echo $i->bday; ?></td>
                </tr>
            <?php } ?>

model:

function masterHide()

{
    $sql =" select * from ecms";
    $query = $this->db->query($sql);
    return($query->num_rows() > 0) ? $query->result(): NULL;
}

controller:

    function search() {

//  $this->MasterListAccess();
    $this->load->model('navi_model');
    $query = $this->navi_model->masterHide();
    //check results or returned value of model
    //print_r($query);
    $data['ecms'] = $query;
    $data['main_content'] = 'search';
    $this->load->view('includes/template',$data);
}

Comments

0

At first connect the database in your project file and connect the model page in your view page and try this code. Go to the view page and try this code

View

20
21
<?php
	$get_data=$this->MyModel->get_table_data();
?>
<table class="table table-bordered">
	<thead>
		<tr>
			<th> Name </th>
			<th> Email </th>
			<th> Phone No </th>
		</tr>
	</thead>
	<tbody>
	<?php foreach($get_data as $val){ ?>
		<tr>
			<td><?php echo $val->name; ?></td>
			<td><?php echo $val->email; ?></td>
			<td><?php echo $val->phone; ?></td>
		</tr>
	<?php } ?>
	</tbody>
</table>

Model

public function get_table_data(){ $qry=$this->db->select('*')->from('Table Name')->get()->result(); return $qry; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.