Controller :
public function TampilGejala(){
$data['gejala'] = $this->model_admin->tampil_gejala();
$this->load->view('auth/admin_gejala',$data);
}
Models:
public function tampil_gejala() {
return $this->db->get('gejala')->result_array();
}
View :
<?php
foreach($gejala as $g){
?>
<tr>
<td><?php echo $g->kode_gejala ?></td>
<td><?php echo $g->nama_gejala ?></td>
<td>Button</td>
</tr>
<?php } ?>
Mysql:
enter image description here
Can you help me ?
Related
I want to load another view using the function within the same controller. I'm new to codeIgniter so Please go easy on me :D
loaded home page by default -> From Home page filled in fields -> submit -> using ajax grabbed data from database -> use data on another page by loading the new view in the same controller as the default.
Controller:
<?php
class logInCon extends CI_Controller
{
public function login()
{
//$data['creds'] = $this->accsModel->getUser();
$this->load->view("Login");
}
public function validate_LogIn()
{
$uname = $this->input->post('uname');
$pass = $this->input->post('pass');
$this->load->model("accsModel");
$data = $this->accsModel->logInCheck($uname, $pass);
$check = $data['verified'];
if ($check <= 0)
{
echo "Not a valid member";
}else
{
$data['logged'] = $this->accsModel->getUser($uname, $pass);
$this->load->view('comms.php');//want this to load
}
}
}
?>
Model
<?php
/**
*/
class accsModel extends CI_Model
{
public function getBps()
{
$query = $this->db->get('bps');
return $query->result();
}
public function getUser($uname, $psswrd)
{
$getCreds = $this->db->query("SELECT `bps`.*, `users`.bps_id
FROM `bps`
LEFT JOIN `users`
ON `bps`.id = `users`.bps_id
AND `users`.`uname` = '$uname'
AND `users`.`pwd` ='$psswrd'
WHERE `users`.bps_id != ''
OR `users`.bps_id IS NOT NULL");
return $getCreds->result();
}
public function logInCheck($uname, $psswrd)
{
$this->db->select('COUNT(*) AS verified');
$this->db->where('uname', $uname);
$this->db->where('pwd', $psswrd);
$this->db->limit(1);
return $this->db->get('users')->row_array();
}
}
?>
View
<body>
<div class="container">
<div class="row">
<div class="LogForm">
<div class="col-md-12">
<input type="text" id="bpCode" name="bpCode">
</div>
<div class="col-md-12">
<input type="password" id="pass" name="pass">
</div>
<div class="btn">
<span id="emptyF" hidden style="color:red;"></span>
<button id="submit" class="btn btn-info btn-lg btn-block">Login</button>
<span id="result"></span>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function()
{
$("#submit").click(function()
{
var uname = $('#bpCode').val();
var pass = $('#pass').val();
if(uname == "" || pass =="")
{
$("#emptyF").attr("hidden", false).html("Please fill in the form");
}else{
$("#emptyF").attr("hidden", true);
$.ajax({//start of ajax function
type: "POST",
url: "<?php echo base_url('logInCon/validate_LogIn'); ?>",
data:
{
uname : uname,
pass : pass
}
//end of ajax function
});
}
});
});
</script>
</body>
View comms want this to be loaded and use the the data from the getUser() function
<body>
<p><?php print_r($logged) ?></p>
<h1>Commissions View</h1>
<table style="width:100%">
<?php
foreach ($logged as $lgdIn) {
?>
<tr>
<th>ID</th>
<th>bpID</th>
<th>pc</th>
<th>up</th>
<th>tmLdr</th>
<th>fName</th>
<th>mName</th>
<th>lName</th>
<th>contactNo</th>
<th>Status-Id</th>
<th>bpclass_id</th>
</tr>
<tr>
<td><?php echo $lgdIn->id; ?></td>
<td><?php echo $lgdIn->bpID; ?></td>
<td><?php echo $lgdIn->pc; ?></td>
<td><?php echo $lgdIn->up; ?></td>
<td><?php echo $lgdIn->tmLdr; ?></td>
<td><?php echo $lgdIn->fName; ?></td>
<td><?php echo $lgdIn->mName; ?></td>
<td><?php echo $lgdIn->lName; ?></td>
<td><?php echo $lgdIn->contactNo; ?></td>
<td><?php echo $lgdIn->status_id; ?></td>
<td><?php echo $lgdIn->bpclass_id; ?></td>
</tr>
<?php
}
?>
</table>
</body>
You need to pass $data in load view just like below code
public function validate_LogIn()
{
$uname = $this->input->post('uname');
$pass = $this->input->post('pass');
$this->load->model("accsModel");
$data = $this->accsModel->logInCheck($uname, $pass);
$check = $data['verified'];
if ($check <= 0)
{
echo "Not a valid member";
}else{
$data['logged'] = $this->accsModel->getUser($uname, $pass);
$this->load->view('comms',$data);//want this to load
}
}
little change in view too
<body>
<p><?php print_r($logged) ?></p>
<h1>Commissions View</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>bpID</th>
<th>pc</th>
<th>up</th>
<th>tmLdr</th>
<th>fName</th>
<th>mName</th>
<th>lName</th>
<th>contactNo</th>
<th>Status-Id</th>
<th>bpclass_id</th>
</tr>
<?php if(!empty($logged)) { foreach ($logged as $lgdIn) { ?>
<tr>
<td><?php echo $lgdIn->id; ?></td>
<td><?php echo $lgdIn->bpID; ?></td>
<td><?php echo $lgdIn->pc; ?></td>
<td><?php echo $lgdIn->up; ?></td>
<td><?php echo $lgdIn->tmLdr; ?></td>
<td><?php echo $lgdIn->fName; ?></td>
<td><?php echo $lgdIn->mName; ?></td>
<td><?php echo $lgdIn->lName; ?></td>
<td><?php echo $lgdIn->contactNo; ?></td>
<td><?php echo $lgdIn->status_id; ?></td>
<td><?php echo $lgdIn->bpclass_id; ?></td>
</tr>
<?php } } else { ?>
<tr colspan="11"><td>No record found.</td></tr>
<?php } ?>
</table>
</body>
fetch_view.php
This is my fetch_view.php file through this file I am showing my html.
<?php
foreach ($h->result() as $row)
{ ?>
<tr>
<td><?php echo $row->fname; ?></td>
<td><?php echo $row->lname; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo $row->mobile; ?></td>
<td><?php echo $row->message; ?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php } ?>
select.php
This is my model in this task.
public function deleteuser($id)
{
$this->load->database();
$this->db->where('id', $id);
$this->db->delete('student');
return true;
}
**home.php**
Controller function, In this file I am making a delete function.
, I have attached screenshot when I tried to delete data I am facing that error.
public function delete()
{
$this->load->model('select');
$id=$this->input->get('id');
if($this->select->deleteuser($id))
{
$data['data']=$this->select->getuser();
$this->load->view('fetch_view', $data);
}
}
Everything is working fine but you don't have a getuser() function defined in your model 'select'.
Note: When creating a model Please give name ending with "..._m" like "select_m" you will confuse about it later when your project will get larger.
Controller
public function admin()
{
// get the current date employee logged in details
$data['get_attendance'] = $this->attendance_model->get_attendance();
// get the active employee details
$data['get_employee'] = $this->attendance_model->get_employee();
//print_r($data['get_employee']); exit();
// attendance page
$data['header'] = "Employee";
$data['sub_header'] = "Attendance employee";
$data['main_content'] = 'attendance/admin_list';
$this->load->view('employeelayout/main',$data);
}
Model
public function get_attendance()
{
return $this->db->where('date',date('Y-m-d'))->get('attendance')->result_array();
}
public function get_employee()
{
return $this->db->where('is_active',1)->get('employee')->result_array();
}
Views
<?php $count = 1 ?>
<?php foreach ($get_employee as $employee) { ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
<td><?php echo $employee['employee_number'] ?></td>
<?php foreach ($get_attendance as $attendance) : ?>
<!-- if employee exists -->
<?php if($employee['employee_id'] == $attendance['employee_id'] ) { ?>
<td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
<td><?php echo $attendance['mark_in_time'] ?></td>
<td>mark out going</td>
<td>Active</td>
<?php break; ?>
<?php } elseif($employee['employee_id'] != $attendance['employee_id'] ) { ?>
<td><?php echo "i'm absent" ?></td>
<?php break; ?>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php } ?>
I want to display the currently logged in (present) employee and display the absent employees too. I'm getting the employee details from employee table.
and attendance details from attendance table. if an employee is absent and I want to display the absent else display the login details. where here foreach loop not displaying properly with if condition. what's wrong with the loop.
try this:
<?php foreach ($get_employee as $employee) : ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
<td><?php echo $employee['employee_number'] ?></td>
<?php foreach ($get_attendance as $attendance) : ?>
<!-- if employee exists -->
<?php if($employee['employee_id'] == $attendance['employee_id']) : ?>
<td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
<td><?php echo $attendance['mark_in_time'] ?></td>
<td>mark out going</td>
<td>Active</td>
<?php elseif ($employee['employee_id'] != $attendance['employee_id']) : ?>
<td><?php echo "i'm absent" ?></td>
<?php else : ?>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
Apply left join in place of two different queries.
Because currently you have applied two loops and their comparisons, will consume a lot of execution time And I don't think this is the good code If you get thousands of data than definitely you can analyse the execution time.
So it's better to apply join query.
am new to codeigniter and I want to develop todo app.now the problem is get data from database ,anyone findout and tell me what the error is,
get.php
<html>
<head>
<title></title>
</head>
<body>
<?php foreach($data as $row){?>
<tr>
<td><?php echo $row->task; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php }?>
</body>
</html>
Add_task.php(model)
public function get_user()
{
$this->load->database();
$data=$this->db->get('user');
return $data->result();
}
Welcome.php(controller)
public function get_task($data)
{
$this->load->model('Add_task');
$data=$this->Add_task->get_user();
$this->load->view('get',$data);
}
Am getting error like this
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/get.php
Line Number: 8
Backtrace:
File: C:\wamp\www\CodeIgniter-3.0.4\application\views\get.php Line: 8
Function: _error_handler
File:
C:\wamp\www\CodeIgniter-3.0.4\application\controllers\Welcome.php
Line: 43 Function: view
File:
C:\wamp\www\CodeIgniter-3.0.4\application\controllers\Welcome.php
Line: 33 Function: get_task
File: C:\wamp\www\CodeIgniter-3.0.4\index.php Line: 292 Function:
require_once
Just change your controller as:
public function get_task($data) {
$this->load->model('Add_task');
$data["data"] =$this->Add_task->get_user();
$this->load->view('get',$data);
}
You need to pass associative array in load view $data["data"]
Codeigniter read your array by keys, so every variable you want pass to view you must put it as a key in your $data array like this:
public function get_task()
{
$this->load->model('Add_task');
$rows=$this->Add_task->get_user();
$data['title'] = "page title";
$data['rows'] = $rows;
$this->load->view('get',$data);
}
and in your view, replace $data with $rows:
<?php foreach($rows as $row){?>
<tr>
<td><?php echo $row->task; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php }?>
Just change
$this->load->view('get',$data);
to
$this->load->view('get', ['data' => $data]);
and all be fine.
If you want to pass a variable into a view, you need to create correspond array index for it (with the same name but without the $ sign). For example, if you want to have $var1, $my_variable, $myCustomVariable, and $data in the 'get' view, you need to pass it as:
$this->load->view('get', [
'var1' => 'value of $var1',
'my_variable' => 'value of $my_variable',
'myCustomVariable' => 'value of $myCustomVariable',
'data' => $data
]);
You must pass an associative array that contain your database result.
See you the documentation page for views at "Creating Loops" paragrapher
Welcome.php (controller)
public function get_task($data)
{
$this->load->model('Add_task');
$data['users'] = $this->Add_task->get_user();
$this->load->view('get', $data);
}
And change the loop in your get.php (view) :
<?php foreach($users as $user) { ?>
<tr>
<td><?php echo $user->task; ?></td>
<td><?php echo $user->status; ?></td>
<td><?php echo $user->description; ?></td>
<td><?php echo $user->date; ?></td>
</tr>
<?php } ?>
I'm using codeigniter, and i want to make a conditional based on return value from model.
here is the model:
public function get_sentitems()
{
$query = $this->db->get('sentitems');
return $query->result_array();
}
and here is the controller:
public function sentitems()
{
$data['sentitems'] = $this->sms_model->get_sentitems();
if($data['sentitems']['Status'] === 'SendingOKNoReport')
{
$data['status_message'] = 'Sent';
}
else
{
$data['status_message'] = 'Failed';
}
$data['title'] = ucwords('sent items');
$this->load->view('templates/header', $data);
$this->load->view('sms/sentitems', $data);
$this->load->view('templates/footer');
}
And here is the view
<h2><?php echo $title; ?></h2>
<table border="1" width="100%">
<thead>
<tr>
<th>No.</th>
<th>Tujuan</th>
<th>Waktu</th>
<th>Isi</th>
<th>Ket.</th>
</tr>
</thead>
<tbody>
<?php foreach ($sentitems as $sentitems_item): ?>
<tr>
<td><?php echo $sentitems_item['ID']; ?></td>
<td><?php echo $sentitems_item['DestinationNumber']; ?></td>
<td><?php echo $sentitems_item['SendingDateTime']; ?></td>
<td><?php echo $sentitems_item['TextDecoded']; ?></td>
<td><?php echo $sentitems_item['Status']; ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
I have a column Status, but why the result in browser is always like this:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: Status
Filename: controllers/sms.php
Line Number: 62
What is the solution? Sorry for my bad English.
The issue is that in your controller $data['sentitems'] contains an array of results from your database, so you cannot get at it with $data['sentitems']['Status']. What you actually have is
$data['sentitems'][0]['Status']
$data['sentitems'][1]['Status']
$data['sentitems'][2]['Status']
and so on.
I think your best option at this point is to remove the following from your controller:
if($data['sentitems']['Status'] === 'SendingOKNoReport')
{
$data['status_message'] = 'Sent';
}
else
{
$data['status_message'] = 'Failed';
}
and amend the line of your template that outputs the status to:
<td><?php echo $sentitems_item['Status'] == 'SendingOKNoReport' ? 'Sent' : 'Failed'; ?></td>