codeigniter - remove specific table row without delete it in database - php

im a newbie for codeigniter. i need some help to solve my problem.
so i have a table filled with data that i GET from database (which only display the last timestamp for category "no_registrasi"). and there is an option in "Selesai" to remove the row in the html table without delete it in database. i already tried to find the solution, but i have no idea how to do this.
this is my table html
<table id="tabel-pas" class="table table-striped table-bordered dt-responsive wrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>Waktu</th>
<th>No. Registrasi</th>
<th>No. Rekam Medis</th>
<th>Nama Dokter</th>
<th>Nama Pemilik</th>
<th>Nama Hewan</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach($daftar_inap_aktif as $row){
echo "<tr>";
echo '<td>'.$row->waktu.'</td>';
echo "<td>".$row->no_registrasi."</td>";
echo "<td>".$row->no_rm."</td>";
echo "<td>".$row->username."</td>";
echo "<td>".$row->nama_pemilik."</td>";
echo "<td>".$row->nama_hewan."</td>";
echo '<td>';
echo 'Tambah | Selesai';
echo'</td>';
echo"</tr>";$no++;
}
?>
</tbody>
</table>
here is my model
function daftar_inap_aktif()
{
$this->db->select('*');
$this->db->select_max('waktu');
$this->db->from('inputrminap');
$this->db->group_by('no_registrasi');
$this->db->order_by('waktu', 'DESC');
$query = $this->db->get();
return $query->result();
}
and here is my controller
function daftar_inap()
{
$data['daftar_inap_aktif'] = $this->rekammedis_model->daftar_inap_aktif();
$this->load->view('daftar_inap_view', $data);
}

I am not sure what are you trying to accomplish, you mean remove that row for that particular session or permanently until set it to display again by admin or so.
Let me give it a shot.
**Removing it temporarily(hiding).. What I would do in that case:
Your View Code:
Selesai';
// Considering that you have base url setup and you have id of that row.
And then in Controller:
Public function some_method(){
$id = $this->uri->segment(3);
$this->some_model->hide_rows_with_id($id);
redirect('url_for_that_view');
}
And finally model:
function hide_rows_with_id($id)
{
$this->db->select('*');
$this->db->select_max('waktu');
$this->db->from('inputrminap');
$this->db->where('id !=', $id); // Added this
$this->db->group_by('no_registrasi');
$this->db->order_by('waktu', 'DESC');
$query = $this->db->get();
return $query->result();
}
This is just an idea, you may want to add more code to model if "id" is an array.
And of course, if the page is reopened from the scratch, all results will be displayed again. To hide it permanently you can create a new column with name something like "hidden" and insert '1' if was hidden through view.. and then in model:
$this->db->where('hidden !=', 1);

Add a unique id for each row you have, And link it with the href you have
e.g HTML:
<tr id="row-1">
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>Delete</td>
</tr>
Then in your javascript code add something like this:
$('.del_row').on('click', function (e) {
e.preventDefault();
var $rowId = $(this).data('row-id');
$('#row-'+$rowId).remove();
});

So that keep an extra column call status. When its active set it 0 and when its deaxtive set it 1.
So in your query where you load data you have to add new where clause.
WHERE status=0
Note : set status column as int or boolen.

Related

How to Query multiple variable to show in a View in Codeigniter

I have my little project coding with Codeigniter, I am just stuck in querying the total count of specific column to show in a View. Here are some pieces of my code where I get stuck:
Model:
function get_dailyprob()
{
$date = date('d-m-Y');
$q1 = $this->db->select('client')->like('created_at', $date)->group_by('client')->get('histprob')->result();
$q2 = $this->db->like('created_at', $date)->count_all_results('histprob'); //HERE I HAVE NO IDEA HOW TO GET A TOTAL COUNT OF EACH ROWS THAT ALREADY GROUPED BY COLUMN client
return array('dp_client' => $q1, 'dp_count' => $q2);
}
Controller:
$data['dailyprobs'] = $this->skejuler_m->get_dailyprob();
View:
<table class="table table-hover">
<?php if ($dailyprobs['dp_count'] > 0) {
foreach ($dailyprobs['dp_client'] as $dpc): ?>
<tr>
<td><?php echo $dpc->client; ?></td>
<td><span style="font-size:16px" class="badge bg-red"><?php echo $dailyprobs['dp_count']; ?></span></td>
</tr>
<?php endforeach; ?>
<tr><td><?php } else { echo 'No Issues!'; } ?></td></tr>
</table>
$dpc->client results some rows and each row has different count (filtered by query in Model)
$dailyprobs['dp_count'] is currently showing the whole results, whereas I need showing a total count grouped by client
Sorry if the explanation was confusing. I just added an image of View. As shown in the picture, both American Standard / Grohe & App Sinarmas have each 2 in total number = 4, whereas the actual total row is 1 of each column (client) = 2. I am sorry for my English.
It just solved by changing the query in Model:
function get_dailyprob()
{
$date = date('d-m-Y');
$query = $this->db->select('*, COUNT(*) as cnt')->like('created_at', $date)->group_by('client')->get('histprob');
return $query->result();
}
Then use foreach in View to get Count and Rows Result.

Want to put same value in whole column in HTML-PHP table

I want to put same value in one column in whole table.
The table looks like this
Table Image
For suppose, I want to put status as Community owner for all Lvl7's.
Here's the table code,
<table class="table" id="myTable">
<thead>
<tr class="header">
<th width="60%"></th>
<th width="20%"></th>
<th width="20%"></th>
</tr>
</thead>
<?php
$query = $config->prepare("SELECT playerName, playerLevel FROM `playerdata` WHERE playerLevel = 7 ORDER BY playerName DESC");
$query->execute();
while($data = $query->fetch())
{
echo "<tr><td>".$data['playerName']."</td>";
echo "<td>".$data['playerLevel']."</td></tr>";
}
?>
</table>
How do I put the value as community owner for this table.
I have same tables like these for all levels.
I dont know how to echo the third column using and or print the same value for all rows in Status column.
When I tried to code, it only printed the value Community Owner to the last row of table.
P.S:- I am rookie in all this stuff, any type of help is appreciated.
while($data = $query->fetch())
{
echo "<tr><td>".$data['playerName']."</td>";
echo "<td>".$data['playerLevel']."</td>";
switch ($data['playerLevel'])
{
case 7:
echo "<td>Community owner</td></tr>";
break;
default:
echo "<td>Not defined</td></tr>";
break;
}
}
//Changed data['playerLevel'] to $data['playerLevel'] from the original post in switch statement, it worked :D

how to get all rows in column to insert db in codeigniter

I'm trying to insert all rows which showing view in a table but it insert only one row with value '1' not all rows and not that id's contain in a column:
view:
echo"<table class='table table-hover type-list2'id='traineeList'>
<tr class='success'>
<th>Trainee ID</th>
<th>Trainee Name</th>
<th>Present</th>
</tr>";
foreach($list['trainee'] as $row){
echo "
<tr><td>".str_pad($row->TraineeID,7,"0", STR_PAD_LEFT)."</td>
<td>".$row->Name."</td>
<td><input type='checkbox' name='.$row->TraineeID[]' value='".$row->TraineeID."' checked></td>
</tr>";
}
echo "</table>";
controller:
public function insertAttendance(){
$data = array('TraineeID'>=$this->input->post('TraineeID'));
$attnDate=$this->input->post('attnDate');
$classHour= $this->input->post('classHour');
foreach ($data as $id){
$query="INSERT INTO `tbl_attn_temp` (TraineeID, Date, classHour) VALUES ('".$id."','".$attnDate."','".$classHour."')";
$this->db->query($query);
redirect('attendance/index/');
}
see the given link for your question
http://codefury.net/2009/12/insert-multiple-rows-into-a-database-with-codeigniter/
Your redirect function should be placed outside of your foreach loop, probably. Looking at your code, your foreach loop will run only once and then redirect you to another page.
Please try with this
foreach($_POST['TraineeID'] as $key => $id) {
$attnDate=$_POST['attnDate'][$key];
$classHour= $_POST['classHour'][$key];
$query="INSERT INTO `tbl_attn_temp` (TraineeID, Date, classHour) VALUES ('".$id."','".$attnDate."','".$classHour."')";
$this->db->query($query);
}
redirect('attendance/index/');

editable table cell using angularJS

Is there a way to show data inside td and allow user to edit it in a way that the change will take place in the sql (MySQL in my case) table?
im creating a table from database using angularJS:
Creating The json file from the table:
$result = mysql_query("select * from `user script settings`");
//Create an array
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array['user_id'] = $row['user id'];
$row_array['script_id'] = $row['script id'];
$row_array['cron_format'] = $row['cron format'];
$row_array['schedule_last_update'] = $row['schedule last update'];
$row_array['next_execution_time'] = $row['next execution time'];
$row_array['script_exec'] = $row['script_exec'];
//push the values in the array
array_push($json_response,$row_array);
}
echo json_encode($json_response);
showing it in the table:
<table class="table table-bordered table-hover">
<thead>
<td>user name</td>
<td>script name</td>
<td>cron format</td>
<td>schedule last update</td>
<td>next execution time</td>
<td>script exec</td>
</thead>
<tbody>
<tr ng-repeat="x in data">
<td>{{x.user_id}}</td>
<td>{{x.script_id}}</td>
<td>{{x.cron_format}}</td>
<td>{{x.schedule_last_update}}</td>
<td>{{x.next_execution_time}}</td>
<td>{{x.script_exec}}</td>
</tr>
</tbody>
</table>
<script>
function customersController($scope,$http) {
$http.get("getData.php")//test3.php return a json file of users table
.success(function(response) {$scope.data = response;});
}
</script>
I want to allow user to change the cron format value and to update the table ...is there "angularish" way to due that?? if not i'll appreciate guidance for a php solution, thx!

How to write PHP inside HTML when the HTML is wrapped with PHP

I am putting a whole HTML content inside a PHP variable and inside that content I am trying to echo some data fetching from database. But the problem is I cannot make the database query and echo the reuslt as the whole thing is wrapped with PHP and then HTML.(if it sounds confusing please check the code below.)
Is there any way to make the database query outside the PHP variable and echo it. Although I have already tried querying the database outside the variable but as it turned out this time >> $row['examdate'] is creating the problem. It shows an syntax error.
I am doing all these to generate PDF using DOMPDF. After getting the variable I will pass it to my controller to generate the pdf.
<?php $variable= " I need your help, I want to echo this <br>
<table id=\"table-6\" width=\"100%\">
<tr>
<th>Exam Date</th>
<th>Exam Type</th>
<th>Subject</th>
</tr>
$this->db->select('*');
$this->db->from('marks');
$this->db->where('studentid', $studentid);
$this->db->where('examdate >=', $daterange1);
$this->db->where('examdate <=', $daterange2);
$this->db->order_by('examdate','DESC');
$query = $this->db->get('');
if ($query->num_rows() > 0)
{
$row = $query->row_array();
<tr>
<td> echo $row['examdate']</td> ///****this line*****
<td>12</td>
<td>12</td>
<td>100</td>
</tr>
</table>
"; ?>-<< variable ends here
You need to separate the filling of your variable from the execution of your PHP logic.
Append the data at a later stage instead of trying assign everything in one single step.
Here is a modified code:
<?php
$variable = " I need your help, I want to echo this <br>
<table id=\"table-6\" width=\"100%\">
<tr>
<th>Exam Date</th>
<th>Exam Type</th>
<th>Subject</th>
</tr>
";
// now execute the database logic
$this->db->select('*');
$this->db->from('marks');
$this->db->where('studentid', $studentid);
$this->db->where('examdate >=', $daterange1);
$this->db->where('examdate <=', $daterange2);
$this->db->order_by('examdate','DESC');
$query = $this->db->get('');
if ($query->num_rows() > 0)
{
$row = $query->row_array();
// append the data to the existing variable using ".="
// and include the examdate
$variable .= "
<tr>
<td>{$row['examdate']}</td> ///****this line*****
<td>12</td>
<td>12</td>
<td>100</td>
</tr>
";
}
// append the closing table tag to the variable using ".=" again
$variable .= "</table>";
// output $variable
echo $variable;
?>
 

Categories