the previous question is here incase you guys needed a other information:
Fetching information from the database
ANOTHER UPDATE
Although
There are a few things that could be clarified:
model
is a entity that persists in the database. In your case: Membership controller
is in charge of load the model, and pass it to the view to be showedview
only should have html markup, no functions, no logic.Assuming that your database have the structure below.
+---------------+-----------+------------+--------------------+
| id_membership | username | name | email |
+---------------+-----------+------------+--------------------+
| 1 | Marishka | marishkapv | marishka@email.com |
| 2 | johndoe | John | john@doe.com |
| 3 | dennisv | Dennis | dennis@v.com |
+---------------+-----------+------------+--------------------+
You could build your model class Membership extends CI_Model
at /application/models/membership.php
file.
class Membership extends CI_Model {
function __construct()
{
parent::__construct();
}
function member_here()
{
$this->db->select('');
$this->db->from('membership');
$this->db->where('username',$this->input->post('username'));
$q = $this->db->get('');
if($q->num_rows() > 0)
{
$data = array();
foreach($q->result() as $row)
{
$data=$row;
}
return $data;
}
}
}
Supposing that your have a login page at example.com/index.php/login
, this should resides at /application/controllers/login.php
as below:
class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('login_view');
}
public function result()
{
$this->load->model('membership');
$data['member'] = $this->membership->member_here();
$this->load->view('result_view', $data);
}
}
Look like the models is loaded throws this two lines:
//Load the model in make use of its functions
$this->load->model('membership');
//load the row whit the given username
//the result is assigned to array with the key "member", in the view you access this
//value with $member var
$data['member'] = $this->membership->member_here();
As you noted in controller code, there are two views files, login_view
and result_view
. The first one will show a html form where you can put the username in order to trigger a select query. The result_view
show the informacion of the member according to the selected membership
login_view.php
<html>
<head></head>
<body>
<form action="login/result" method="post">
Type your username: <input type="text" name="username" />
<input type="submit" value="Load from database" />
</form>
</body>
</html>
result_view.php
<h4>Details:</h4>
<p>
Id: <?=$member->id_membership?> <br />
Username: <?=$member->username?> <br />
Email: <?=$member->email?>
</p>
This can be very simple read the user guide for more simple examples
Model
Class Members Extends CI_Model
{
function __construct(){
parent::__construct();
}
function member_here()
{
$this->db->where('username',$this->input->post('username'));
$query = $this->db->get('membership');
if($query->num_rows() > 0)
{
return $query->result();
}
}
}
Controlelr
Class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('login_view');
}
public function result()
{
$this->load->model('membership');
$data['member'] = $this->membership->member_here();
$this->load->view('result_view', $data);
}
}
And in View you can simple do it
<?php
foreach($member as $row){
?>
<option value="<?php echo $row->id?>"><?php echo $row->name?></option>
<?php
}
?>