i am still fresher in PHP MVC architecture. here i am trying to list the data in my database table through the model view controller. i am phase this error to fetch the data . i am doing edit my data,. so edit function is not working .
test-table name
there are few fields to get the data username and email..
please help me to solve this query
here is my Model file.
public function getAllRecords()
{
$this->load->library("database");
$q = $this->db->get("test");
if($q->num_rows() > 0)
{
return $q->result();
}
return array();
}
Here is my Controller file
public function index()
{
$this->load->model("testmodel");
$data['records'] = $this->model->getAllRecords();
$this->load->view("edit",$data);
}
and it is my View/edit.php file
<tr>
<?php
if (is_array($data)){
foreach($data as $row):
?>
<tr>
<td><?=$row->id?></td>
<td><?=$row->uname?></td>
<td><?=$row->email?></td>
<td> <img src="../edit.png" alt=""/> </td>
<td> <img src="../delete.png" alt=""/> </td>
</tr>
Controller
Change
$data['records'] = $this->model->getAllRecords();
to
$data['records'] = $this->testmodel->getAllRecords();
View
Use $records instead of $data
in your view file use $records instead of $data
<?php
if (is_array($records)){
foreach($records as $row):
?>
when you pass a data from controller to view say
$data['name'] = 'max';
$this->load->view("edit",$data);
in view you can access it with variable $name
Controller:
public function index()
{
$this->load->model("testmodel");
$data['records'] = $this->model->getAllRecords();
$this->load->view("edit",array('data'=>$data));
}
This will work for sure.
Enjoy!
Related
I am getting user profile fields from the database and want to display in my view but don't know to do this with the master layout.
this is my model
function fetchProfile($id,$page){
$query = $this->db->query("SELECT * FROM staff_master WHERE Id='$id'");
return $query->result();
}
this is my controller:
public function edit($id,$page){
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
$this->load->view('template_master',$data);
}
I am also trying to find a solution. I am passing user Id and another by URL (get method).
You are overwriting $data['query'] when you assign the array next:
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
Either do:
$data= array('content'=>'view_staff_edit');
$data['query'] = $this->StaffModel->fetchProfile($id,$page); // note position
Or:
$data = array(
'content' = 'view_staff_edit',
'query' => $this->StaffModel->fetchProfile($id,$page),
);
Access in view via $query and $content.
Unrelated:
You are also missing $page in your query, and its generally a good idea to declare gets as null if not set or you will get a notice: public function edit($id=null,$page=null){
Your overriding your first declaration of variable $data what you can do is to initialize them both at the same time.
Controller
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id,$page),
'content' => 'view_staff_edit'
);
$this->load->view('template_master',$data);
}
Then access it on your View file
<h1><?php echo $content ?></h1>
<?php foreach($query as $result): ?>
<p><?php echo $result->id ?></p>
<?php endforeach; ?>
Try doing something like this:
public function edit($id,$page) {
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data['content']= 'view_staff_edit';
$this->load->view('template_master',$data);
}
YOUR MODEL
function fetchProfile($id){
return $this->db->get_where('Id' => $id)->result_array();
}
YOUR CONTROLLER
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id),
'content' => 'view_staff_edit',
);
$this->load->view('template_master',$data);
}
YOUR "template_master" VIEW
<?php foreach ($query as $res){?>
<span><?php echo $res['Id'];?></span> //User html content according to your requirements ALSO you can add all columns retrieved from database
<?php } ?>
I am new to Laravel and I am getting following error Undefined variable: no_student (Laravel 5.3)
Here is my view. blade
#foreach($no_marks as $no_mark)
<tr class="gradeX odd" role="row" id="">
<td style="display:none"></td>
<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
<input type="checkbox" class="checkboxes checkbox-index" value="">
<span></span>
</label>
</td>
<td></td>
<td></td>
<td></td>
<td>
#endforeach
my index method in controller
public function index()
{
//
$no_marks = Markno::orderBy('id', 'desc')->get();
return view('no-mark.index');
}
I am storing record using this store method
public function store(Request $request)
{
$this->validate($request, [
'ip_range' => 'required|max:255',
]);
$ip_data['ip_range'] = $request->input('ip_range');
$ip_data['min_ip'] = $request->input('ip_range');
$ip_data['max_ip'] = $request->input('ip_range');
$ip_data['list_id'] = $request->input('list_id');
$ip_data['user_id'] = Auth::user()->id;
$no_marks = Markno::create($ip_data);
session()->flash('msg', ' Successfully created');
return view('no-mark.index');
}
I don't know where I am going wrong. Please help to fix it.
You did not passed your variable $no_marks into your view that's why it is undefined to passed it
return view('no-mark.index', array('no_marks' => $no_marks));
To use a variable available on controller you have to pass it on view like:
return view('no-mark.index', array('no_marks' => $no_marks));
here the second parameter is the array, you can access its index on view page
please update your action
public function index()
{
//
$no_marks = Markno::orderBy('id', 'desc')->get();
return view('no-mark.index',compact(['no_marks']));
}
I've just completed user and admin signup and login system in codeigniter. I have two table massuser and meal. in the meal table admin add data for users and in the massuser table is recorded data when user sign up. and I am able to show specific user signup data into the view by session but I am not able to show admin inputed data in the same view. I have tried many times but I can't. please help me?
Meal Table structure
**================================
id---- username----meal---date
===============================**
Massuser Table structure
============================================================
id----fullname----username----password----email----regdate
============================================================
Controller
public function mymeals()
{
if($this->session->userdata('is_logged_in')){
$username = $this->session->userdata('username');
$data['results'] = $this->usermodel->get_meal($username);
$this->load->view('public/profile/profile',$data);
}else{
echo "No results";
}
}
model
public function get_meal($username)
{
$user = $this->session->userdata('username');
$this->db->select('*');
$this->db->from('meal');
$this->db->where('username', $user);
$query = $this->db->get()->result();
return $query;
}
view
<table class="table table-striped">
<tr>
<th>Date</th>
<th>Meal</th>
</tr>
<?php
foreach($results as $meal):?>
<tr>
<td><?php echo $meal->date;?></td>
<td><?php echo $meal->meal;?></td>
</tr>
<?php endforeach;?>
</table>
It could be that in your model you are accessing session data.
Do you still get the problem if you used the actual passed variable?
Try
public function get_meal($username) {
$this->db->select('*')
->from('meal')
->where('username', $username);
return $this->db->get()->result();
}
Sorry if this question is already been answered, but i couldn't find an answer.
I want to pass a variable from my model to the controller and finally display it in the view.
I execute a query on the database and after that I count my results (via num_rows). Then I return the result.
public function countHighRisk(){
$this->db->select("ares_status");
$this->db->from("tbl_alert_result");
$this->db->join('tbl_status_standards', 'ares_status = sts_status_id');
$this->db->where('sts_status_risk', 3);
$query = $this->db->get();
return $query->num_rows();
}
After that i place the result in an array and pass the array to my view.
public function index()
{
$this->load->model('Alert_result_model');
$data['high'] = $this->Alert_result_model->countHighRisk();
$this->load->view('templates/head');
$this->load->view('templates/menu');
$this->load->view('pages/Dashboard', $data);
$this->load->view('templates/footer');
}
I get an error when I try to display the variable in the view.
<div class="col-xs-8 text-right">
<span> High Risk </span>
<h2 class="font-bold"><span class="count"><?= $high; ?></span></h2>
</div>
error: http://imgur.com/iHkYHiI
Thanks in advance.
Your whole idea of calling the views is wrong here. Please use it as follows:
Controller:
public function index()
{
$this->load->model('Alert_result_model');
$data['high'] = $this->Alert_result_model->countHighRisk();
$this->load->view('pages/Dashboard', $data);
}
View:
<? $this->load->view('templates/head'); ?>
<? $this->load->view('templates/menu'); ?>
<div class="col-xs-8 text-right">
<span> High Risk </span>
<h2 class="font-bold"><span class="count"><?= $high; ?></span></h2>
</div>
<? $this->load->view('templates/footer'); ?>
Also check what $this->Alert_result_model->countHighRisk(); is actually returning by doing print_r($this->Alert_result_model->countHighRisk();)
You can load a view into controller but do this way below
class Dashboard extends CI_Controller {
public function index() {
$this->load->model('Alert_result_model');
$data['high'] = $this->Alert_result_model->countHighRisk();
// Choose only one header examples:
$data['header'] = $this->load->view('header', Null, TRUE); // If No Data
$data['header'] = $this->load->view('header', $data, TRUE); // If Data
// Choose only one Footer examples:
$data['footer'] = $this->load->view('footer', Null, TRUE); // If No Data
$data['footer'] = $this->load->view('footer', $data, TRUE); // If Data
$this->load->view('dashboard', $data);
}
}
Dashboard View
<?php echo $header;?>
<?php foreach ($high as $low) { ?>
<?php echo $low['something'];?>
<?php }?>
<?php echo $footer;?>
If you have a header separate header controller and footer controller you will need HMVC if you are confused on user guide top right corner of default user guide in CI3 is a switch to more cleaner version.
please change
$this->load->view('pages/Dashboard', $data);
into
$this->load->view('pages/Dashboard', $data, true);
now it should work.
value of FORM INPUT Help!!
//this is just a refrence of $nm and $fid from test_model//
$data['fid']['value'] = 0;
$data['nm'] = array('name'=>'fname',
'id'=>'id');
say i have one form_view with
<?=form_label('Insert Your Name :')?>
<?=form_input($nm)?>
and a function to get single row
function get($id){
$query = $this->db->getwhere('test',array('id'=>$id));
return $query->row_array();
}
then in controller.. index($id = 0)
and somewhere in index
if((int)$id > 0)
{
$q = $this->test_model->get($id);
$data['fid']['value'] = $q['id'];
$data['nm']['value'] = $q['name'];
}
and mysql table has something like 1. victor, 2. visible etc. as a name value
but here its not taking the value of name and id from form_input and not showing it again in form_view in same input box as victor etc so to update and post it back to database...
anyone please help!!
and please be easy as I am new to CI!!
Based on your comment to my first answer, here is a sample of a Controller, Model and View to update a user entry pulled from a table in a database.
Controller
class Users extends Controller
{
function Users()
{
parent::Controller();
}
function browse()
{
}
function edit($id)
{
// Fetch user by id
$user = $this->user_model->get_user($id);
// Form validation
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
if ($this->form_validation->run())
{
// Update user
$user['name'] = $this->input->post('name', true);
$this->user_model->update_user($user);
// Redirect to some other page
redirect('users/browse');
}
else
{
// Load edit view
$this->load->view('users/edit', array('user' => $user));
}
}
}
Model
class User_model extends Model
{
function User_model()
{
parent::Model();
}
function get_user($user_id)
{
$sql = 'select * from users where user_id=?';
$query = $this->db->query($sql, array($user_id));
return $query->row();
}
function update_user($user)
{
$this->db->where(array('user_id' => $user['user_id']));
$this->db->update('users', $user);
}
}
View
<?php echo form_open('users/edit/' . $user['user_id']); ?>
<div>
<label for="name">Name:</label>
<input type="text" name="name" value="<?php echo set_value('name', $user['name']); ?>" />
</div>
<div>
<input type="submit" value="Update" />
</div>
<?php echo form_close(); ?>
It's hard to see the problem from your snippets of code, please try and give a little more information as to the structure of your app and where these code samples are placed.
Presume in the last code listing ('somewhere in index') you are getting $id from the form, but you define the ID of the form input box as 'id' array('name'=>'fname','id'=>'id') rather than an integer value so maybe this is where the problem lies.
Where does the $data array get passed to in the third code listing?
From your question I think you want to display a form to edit a person record in the database.
Controller code
// Normally data object is retrieved from the database
// This is just to simplify the code
$person = array('id' => 1, 'name' => 'stephenc');
// Pass to the view
$this->load->view('my_view_name', array('person' => $person));
View code
<?php echo form_label('Your name: ', 'name'); ?>
<?php echo form_input(array('name' => 'name', 'value' => $person['name'])); ?>
Don't forget to echo what is returned from form_label and form_input. This could be where you are going wrong.