Good day, I have some trouble making an update specific record on a selected row from the database. I want to display the data after clicking on the edit icon from the selected data from the table display table.
Controller:
//Show Users/Members page
public function users_page() {
$data['users'] = $this->user->get_users();
$this->load->view('user_page' , $data);
}
Model:
// Retrieve user data from database
public function get_users() {
$this->db->select('id, email, first_name, last_name, date_registered');
$this->db->from('auth_users');
$query = $this->db->get();
$result = $query->result();
foreach ($result as $key => $value) {
// var_dump($key);
$data[$key] = array(
'id' => $value->id,
'firstname' => $value->first_name,
'lastname' => $value->last_name,
'datereg' => $value->date_registered,
'email' => $value->email
);
}
return $data;
}
View:
<table class="table table-condensed">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Date Registered</th>
<th>Action</th>
</tr>
<?php foreach($users as $key => $value) {?>
<tr>
<td><?php echo $value['firstname']; ?></td>
<td><?php echo $value['lastname']; ?></td>
<td><?php echo $value['email']; ?></td>
<td><?php echo $value['datereg']; ?></td>
<td><i class="material-icons" title="Click to update">mode_edit</i> |
<i class="material-icons" title="Click to remove">delete</i></td>
</tr>
<?php } ?>
</thead>
<tbody>
</tbody>
</table>
create a new method in your controller, something like this (not exactly like this as I don't know the classes you're already using)
public function show_user($id) {
$data['users'] = $this->user->get_user($id);
$this->load->view('user_page' , $data);
}
create another method in your model, again something like this
public function get_user($id) {
$this->db->select('id, email, first_name, last_name, date_registered');
$this->db->from('auth_users');
$this->db->where('id', $id);
$query = $this->db->get();
$result = $query->result();
$var_dump($result); --or write a view an pass the result to the view
create a form taking the result from the previous (This I cannot write for you)
create an update method in your controller and model (just like you just did to query the information, but now to update the information from this record)
Related
I am trying to display 'Category' database and this part is quite easy. The hard part is I need to count the subcategory from the 'Category' database and displaying them with one table.
Category | Sub Category
------------------------
Cat A | 5
Cat B | 7
here is my Model:
function category() {
$query = $this->db->get('category');
$result = $query->result_array();
foreach($query->row() as $q) {
$this->db->where('subcat_id', $q['cat_id']);
$query2 = $this->db->get('subcat');
if($query2) {
return true;
} else {
return false;
}
here is my Controller:
function dispaly_category() {
$data['category'] = $this->mymodel->category();
$this->load->view('view', $data);
}
here is my View:
<table>
<thead>
<th>Category</th>
<th>Subcategory</th>
</thead>
<tbody>
<?php foreach($category as $c) : ?>
<tr>
<td><?php echo $c->category_name; ?></td>
<td><?php echo (count subcat for the above category); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
i just post an answer with the assumption you've one Table only (you don't need a separate table for subcategories, but in case you really need your second table you should be able to derive this based on my answer)
Your Model
function category()
{
$query = $this->db
->select("c.*, count(cc.id) AS countSubCategories")
->from("category c")
->join("category cc", "cc.parent_id = c.cat_id","left")
->group_by("c.id")
->get();
return $query->result();
}
Your Controller
function display_category()
{
$arrViewData = array(
'category' => $this->mymodel->category()
);
$this->load->view('view', $arrViewData);
}
your view
<table>
<thead>
<th>Category</th>
<th>Subcategory</th>
</thead>
<tbody>
<?php foreach($category as $c) : ?>
<tr>
<td><?php echo $c->category_name; ?></td>
<td><?php echo $c->countSubCategories; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
i need same help from u all,
i have a code to delete rocord from table but it`s not working. my same one know.
this is my code
=> view
<table class="table table-striped">
<th class="success">NO</th>
<th class="success">TANGGAL</th>
<th class="success">JUDUL</th>
<th class="success">AKSI</th>
<?php
$count = 1;
foreach($hasil AS $row){
?>
<tr>
<td width="30px"><?=$count++;?></td>
<td width="110px"><?= $row['tanggal']; ?></td>
<td><?= $row['judul']; ?></td>
<td width="190px">
Edit
Hapus
</td>
</tr>
<?php } ?>
</table>
=> routes
$route['admin/headline/hapus'] = 'admin/amor_con_headline/hapus';
=> controler
public function hapus($id){
$this->load->model("admin_model/amor_model_headline");
$this->amor_model_headline->hapus_headline($id);
redirect('admin/dashboard');
}
=> model
function hapus_headline($id){
$this->db->where('id_headline',$id);
$this->db->DELETE('a_headline');
}
it's not work.
=> EDIT
it`s was solved by me.
i add this in my routes
$route['admin/headline/hapus/(:any)'] = 'admin/amor_con_headline/hapus/$1';
Try
MODEL
function hapus_headline($id){
$this->db->delete('table_name', array('column_name' => $id));
}
Or
function hapus_headline($id){
$var = array(
'column_name' => $id
);
$this->db->delete('table_name', $var);
}
Try this dude! using URI
=> Model
function hapus_headline(){
$this->db->where('id_headline', $this->uri->segment(3));
$this->db->delete('a_headline');
}
=> controller
public function delete()
{
//product id
$id_headline = $this->uri->segment(3);
$this->products_model->hapus_headline($id_headline);
redirect('admin/headline');
}
=> view
Hapus
I hope this might help you.
I'm currently working on codeigniter. I want to display a value that is not been duplicated or overwrite a duplicated value from MySQL database into the datatable of PHP foreach loop.
Here is the picture my 2 tables in database:
Table "employees" & table "times"
Here is the relationship of my 2 tables:
Relation of table "employees" & table "times"
Here is my controller (home.php):
public function goPresent() { // attendance present page
$this->load->model('Model_attendance');
$query = $this->Model_attendance->getEmployees();
$data['EMPLOYEES'] = null;
if ($query) {
$data['EMPLOYEES'] = $query;
}
$this->load->view('imports/header');
$this->load->view('imports/menu');
$this->load->view('attend', $data);
}
Here is my model (model_attendance.php):
class Model_attendance extends CI_Model {
public $userID;
public $username;
public $password;
public $totime;
public $absence_type;
public $date;
public $hours;
public function getEmployees() {
$this->db->select("*");
$this->db->from('times');
$this->db->join('employees', 'employees.empnum = times.userID');
$query = $this->db->get();
return $query->result();
}
}
Here is my view (attend.php):
<table class="table table-striped responsive-utilities jambo_table" id="myTable">
<thead>
<tr class="headings">
<th>Employee No.</th>
<th>Username</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
foreach($EMPLOYEES as $employee){?>
<tr>
<td><?php echo $employee->empnum; ?></td>
<td><?php echo $employee->username; ?></td>
<td><?php echo $employee->name; ?> <?php echo $employee->lastname; ?></td>
<td><?php
if ($employee->hasClockOut==1){
echo '<a class="label label-danger">Inactive</a>';
}else {
echo '<a class="label label-success">Active</a>';
}
?></td>
</tr>
<?php } ?>
</tbody>
</table>
on which basis you want unique values, just add this line in your query....
$this->db->group_by("column name which needs to be unique");
<?php
$customers = $this->db->group_by('customer_id')->get_where('registered_table' , array(
'chat_group_id' => $group_id , 'year' => $reg_year
))->result_array();
foreach($customers as $row):?>.....
the example code above will find duplicated customer_id and avoids printing out duplicated values as you intended. Here i used group_by('customer-id')
i have a web project using codeigniter.
i have a problem with searching in my project. i have to show the multiple result from searching page with some keyword.
here is my Model
function find_user($like){
$this->db->from('user');
$this->db->like('name', $like,'after');
$result =$this->db->get();
return $result->result_array();
}
and in my user table, include
id | name | place_code
in the user table , column place_code is use to show the place from the user
here is my Controller
function search(){
$query = $this->input->post('query_cari');
$find = $this->m_user->find_user($query);
foreach ($find as $key) {
$code = $key['place_code'];
if ($code == '1') {
$place = 'Secretray';
}elseif($code == '2'){
$place = 'OB';
}elseif($code == '3'){
$place ='Manager';
}
}
$data['result'] = $find;
$data['place'] = $place;
$this->load->view('home/search',$data);
}
that's my code for controller, include a logic for get the position from user in office. but the problem is, when i get a result just 1 result, the place is right. but if i get more than 1 result, place is going wrong and just show the place for all result is the place from the last result in searching.
what i want is, all result just shown their own place.
here my VIEW
<?php foreach ($find as $key: ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $key['id'] ?></td>
<td><?php echo $key['name'] ?></td>
<td><?php echo $place ?></td>
</tr>
</tbody>
</table>
<?php endforeach ?>
It is normal that you always get the last place because your logic is not correct. First, you have an error in your model. Secondly, you can improve your model code:
function find_user($like)
{
$this->db->like('name', $like, 'after');
return $this->db->get('user')->result();
}
Now, in your controller you want to change the variable place according to the value of place_code. You are allowed to add a new key (or change an existing one) in real time to the stdClass() as follows:
foreach($find as $i => $result)
{
// By default, we assume the 'place_code' is equal to '1'
$find[$i]->place = 'Secretray';
if($result->place_code == '2')
$find[$i]->place = 'OB';
else
$find[$i]->place = 'Manager';
}
$data['find'] = $find;
$this->load->view('home/search', $data);
Finally, in your view:
<?php foreach ($find as $result){ ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $result->id; ?></td>
<td><?php echo $result->name; ?></td>
<td><?php echo $result->place; ?></td>
</tr>
</tbody>
</table>
<?php } ?>
I have a list of records from database, see the attachment. How can i insert a vat no for each record using student id(see leftmost column). Each record has a input field(see rightmost column).
Here is my view:
<?php $attributes = array('id' => 'VATformAdd', 'name' =>
'VATformAdd', 'autocomplete' => 'off'); echo form_open('report/addVATno', $attributes);?>
<fieldset>
<legend>Income By Student Admission</legend>
<table class="">
<thead>
<tr>
<th width="15%">Student ID</th>
<th width="15%">Student Form No</th>
<th width="15%">Course</th>
<th width="20%">Student Name</th>
<th width="15%">Admitted by</th>
<th width="10%">Amount</th>
<th width="10%">VAT Serial No</th>
</tr>
</thead>
<tbody>
<?php
if (isset($addmission_income)) {
foreach ($addmission_income as $addmissionincome) {
?>
<tr>
<td><?php echo $addmissionincome->student_id;?></td>
<td><?php echo $addmissionincome->student_form_no;?></td>
<td><?php echo $addmissionincome->course_name;?></td>
<td><?php echo $addmissionincome->student_full_name;?></td>
<td><?php echo user_profile_name($addmissionincome->student_added_by); ?></td>
<td><?php echo $addmissionincome->student_fee_paid;?></td>
<td><input type="text" name="vatno[<?php echo $addmissionincome->student_id;?>]" class="width-100"/></td>
<?php
}
?>
</tr>
<?php
}
?>
</tbody>
</table>
<p>
<input id="submit" name="submit" type="submit" value="Add VAT Number" />
</p>
</fieldset>
<?php echo form_close();?>
Here is my controller:
function addVATno(){
$studentid = $this->input->post('studentid');
foreach($studentid as $a){
if($val[$a]!=''){
$this->report_mdl->addVATno($a);
}
}
}
Here is Model:
function addVATno($a){
$val = $this->input->post('vatno');
$updatevatsl = array(
'dupVATserial_no' => $val);
$this->db->where('student_id', $a);
$query = $this->db->update('data_student_master', $updatevatsl);
return $query;
}
make your view file with a hidden student_id like:
<input type="hidden" name="student_id[]" value="<?php echo $addmissionincome->student_id;?>"/>
and vatno field:
<input type="text" name="vatno[]" class="width-100"/>
Adding the following to your controller will do everything you need (without needing to put any code in your model):
$student_id = $this->input->post('student_id');
$vatno = $this->input->post('vatno');
for ($i = 0; $i < count($student_id); $i++) {
$sm_data[] = array(
'vatno' => $vatno[$i],
'student_id' => $student_id[$i]
);
}
$this->db->update_batch('data_student_master', $sm_data, 'student_id');
Note: It would be better to move the db call to your model to adhere to MVC conventions.
Your issue is that you aren't pulling the correct values from POST.
You are not submitting the student id on its own anywhere in the form, so I can't see how you would be able to access it from POST.
You are also trying to access 'vatno' from POST to insert it into a DB, but 'vatno' in POST is an array.
Because you are using the student id as the array key in your form, you can access both the student ID and the vat number you want to update from the same array. In your controller, you can do:
function addVATno(){
//vatnos is an array, the key is the student id
$vatnos = $this->input->post('vatno');
foreach($vatnos as $key=>$vatno){
$this->report_mdl->addVATno($key, $vatno);
}
}
Then in your model, you just need:
function addVATno($studentid, $vatno){
$updatevatsl = array(
'dupVATserial_no' => $vatno
);
$this->db->where('student_id', $studentid);
$query = $this->db->update('data_student_master', $updatevatsl);
return $query;
}