How to use modal in Codeigniter, with separate data and certain id? - php

I have a view page (ikhtisar_view). And when I click one ID, it show modal (link to ikhtisar/detail) contain data for that ID only.
Here's my ikhtisar view td (contain data which I can click for detail):
<td><a href="<?php echo base_url();?>index.php/ikhtisar/detail" class='iframe'><?php echo $ikh['id']?></a></td>
This is my controller function:
public function detail()
{
$this->load->model('ikhtisar_model');
$detailr = $this->ikhtisar_model->trx();
$data['detail'] = $detailr;
$this->load->view('ikhtisar/trx_view',$data);
}
And this is my model function:
public function trx()
{
$ikh = $this->input->post('id');
$this->db->select('t.id_transaksi AS id, t.tgl_transaksi AS tgl, t.id_member AS memberid, t.nama AS nama, p.kode_produk AS kode, tp.hp_tujuan AS nohp, d.nama AS nama2, t.saldo_awal AS saw, tp.hpp AS hpp, t.saldo_akhir AS sak, tp.laba AS laba, t.ket AS ket, sr.ket AS ket2, sr.jenis AS sj', FALSE)
->from('transaksi_pulsa tp')
->join('transaksi t','tp.id_transaksi=t.id_transaksi', 'left')
->join('produk p','tp.kode_produk = p.kode_produk', 'left')
->join('set_report sr','t.status=sr.jenis', 'left')
->join('distributor d','tp.id_distributor=d.id_distributor and tp.com=d.com', 'left')
->where('t.id_transaksi', $ikh);
$ikhtisar = $this->db->get();
return $ikhtisar;
}
And finally this is my trx_view.php:
<table class="table table-striped table-hover table table-bordered" id="ikhtisar">
<thead style="display: table-header-group; vertical-align: middle; border-color: inherit;">
<tr>
<th>Status</th>
<th>No</th>
<th>Tanggal</th>
<th>Kode RS</th>
<th>Nama RS</th>
<th>Produk</th>
<th>Tujuan</th>
<th>Stok</th>
<th>Harga</th>
<th>Saldo Akhir</th>
<th>Laba</th>
</tr>
</thead>
<tbody>
<?php foreach($ikhlist->result_array() as $ikh): ?>
<tr>
<!--<td><?php echo $ikh['id']?></td>-->
<td style="font-size: 15px;" align="center"><?php if($ikh['sj'] == '1'){?><span><i class="glyphicon glyphicon-ok green"></i></span><?php }elseif($ikh['sj'] == '2'){ ?><span><i class="glyphicon glyphicon-remove red"><?php }else{ ?><span><i class="glyphicon glyphicon-question-sign yellow"><?php }?></i></span></td>
<td><?php echo $ikh['id']?></td>
<td><?php echo $ikh['tgl']?></td>
<td><?php echo $ikh['memberid']?></td>
<td><?php echo $ikh['nama']?></td>
<td><?php echo $ikh['kode']?></td>
<td><?php echo $ikh['nohp']?></td>
<td><?php echo $ikh['nama2']?></td>
<td class="text-right"><?php echo number_format($ikh['hpp'], 0, ",", ".")?></td>
<td class="text-right"><?php echo number_format($ikh['sak'], 0, ",", ".")?></td>
<td><?php echo $ikh['laba']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
But I got just a page (trx_view.php) with no data. It contain only header. So, how to show the correct data? In ikhtisar_view (before click id for detail), no problem.

Try to debug your code on your own,
try to print the query from the model, by using
echo $this->db->last->query()
Try to run that query on the above received query database, and check whether you are getting the data or not.
And then try to print the $detail variable in the view and check whether your view is actually getting the data or not.

If I use this in my ikhtisar_view:
<td>
<form method="post" action="<?php echo base_url();?>index.php/ikhtisar/detail">
<input type="hidden" name="id"
value="<?php echo $ikh['id']; ?>" />
<input type="submit" value="<?php echo $ikh['id']; ?>" />
</form>
</td>
Instead of:
<td><a href="<?php echo base_url();?>index.php/ikhtisar/detail" class='iframe' name="id">
<input name="id" value="<?php echo $ikh['id']; ?>" />
</a></td>
The form-method succeed with exactly data I clicked (id). But, this is not modal. How to convert it to modal?
I already try add class='iframe' to that method, it didn't worked. Modal show up, but blank, no data, no header, no footer.

Related

Need to add a delete button to a html table to remove record from the database with a message

<table class="table admin-table list-table nurserytwo-table">
<thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>Code</td>
<td>Active</td>
<td>Edit</td>
</tr>
</thead>
<tbody>
<?php foreach($nurseries->result() as $nursery) { ?>
<tr>
<td><?php echo $nursery->id; ?></td>
<td><?php echo $nursery->name; ?></td>
<td><?php echo $nursery->code; ?></td>
<td><?php echo set_bool($nursery->active); ?></td>
<td><span class="glyphicon glyphicon-edit"></span> <?php echo
anchor('admin/nurseries/edit_nursery/'.$nursery->id, 'Edit', 'class="edit-
link"'); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
This is my current table code and I have had a good look around to see if I can work out how to do this but I am really new to php and can't seem to get my head around it. I need to add a delete button after the edit one and I know I could do this via a delete.php but no idea where to start. Any help would be much appreciated.
UPDATED:
This is what I have currently:
}elseif( $action == "delete_nursery_course" ){
if($id) {
$q = $this->db->where('id', $id)->delete('nursery_courses');
if($this->db->affected_rows() > 0) {
if($this->input->is_ajax_request()) {
$this->output->enable_profiler(FALSE);
echo "SUCCESS"; die();
}else{
set_flash_message('Nursery deleted successfully.',
'success');
}
}else{
if($this->input->is_ajax_request()) {
$this->output->enable_profiler(FALSE);
echo "Something went wrong. Please try again."; die();
}else{
set_flash_message('Something went wrong. Please try
again.', 'error');
}
}
And here is the html:
<table class="table admin-table list-table nurseryone-table">
<thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>Colour</td>
<td>Active</td>
<td>Edit</td>
<td>Delete</td>
</tr>
</thead>
<tbody>
<?php foreach($nursery_courses->result() as $nursery_course) { ?>
<tr>
<td><?php echo $nursery_course->id; ?></td>
<td><?php echo $nursery_course->name; ?></td>
<td><div style="background:<?php echo $nursery_course->colour; ?>"
class="course-colour"></div></td>
<td><?php echo set_bool($nursery_course->active); ?></td>
<td><span class="glyphicon glyphicon-edit"></span> <?php echo
anchor('admin/nurseries/edit_nursery_course/'.$nursery_course->id, Edit', 'class="edit-link"'); ?></td>
<td>
<span class="glyphicon glyphicon-trash"></span> <?php echo
anchor('admin/nurseries/delete_nursery_course/'.$nursery_course->id,
'delete', 'class="delete-link"'); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You could use only html and javascript with jquery to use ajax requests to make an edit and delete button.
HTML:
<td class="text-right">
<a class="edit btn btn-sm btn-default" href="javascript:;">
<i class="icon-note"></i>
</a>
<a class="delete btn btn-sm btn-danger" href="javascript:;">
<i class="icons-office-52"></i>
</a>
</td>
And in your javascript file you have to define the onclick events for this a buttons.
Here's a Plunker example with html and javascript code:
Editable Table
Also you could use jquery DataTables to make it easier and better.
Hope its help you.

Get php checkbox data from multiple tables

First I will tell what I want to achieve, and then I will explain how I was trying to do it.
I have two tables, one stores type of vessels with a description and their own Id. Then I have another table in wich the vessel type has been stored as text. My goal is to select each one (both records in both tables) with a checkbox in both and store in the table 2 the Id from the table 1.
I will introduce data, to help.
Table 1
1|Balandra|Some description
2|Bergantin|Some description
3|Whatever |Whatever.....
Table2
Balandra
Bergantin
Whatever
Then, I have created a php page that shows both tables with the checkbox I mentioned above. Checkboxes store the Table1 Id and Table2 vesseltypename.
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<a href=<?php echo '../vista/modificar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
pencil"></em></a>
<a href=<?php echo '../datos/borrar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
trash"></em></a>
</td>
<td class="hidden-xs"><?php echo
$navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid" value=<?
php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body paneltodo">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
So, I want to check both table records and store the table1 Id in the table2 idtypevessel field.
I thought that a php file could store both items and call the update function with those parameters, like this:
<?php
require './modelo.php';
$idnavio = $_GET['agruparid'];
$vessel = $_GET['agruparvessel'];
Any suggestions, because I think I have to do a button to submit this parameters, but it must be working on both tables, and I don't know how to access both foreach loop at the same time.
Thanks in advance.
E. Salas
review bellow reference code to submit multi selected checkbox values
for multi selected checkbox submission you must use [] operator after name attribute in html
index.php
<form action="/checkbox.php" method="post">
<strong>Cars:</strong><br>
<?php
$cars = array("Volvo", "BMW", "Toyota");
$colors = array("Red", "Green", "Black");
foreach($cars as $single){
?>
<input type="checkbox" name="cars[]" value="<?php echo $single; ?>">
<?php
}
<br>
<strong>colors:</strong><br>
foreach($colors as $single){
?>
<input type="checkbox" name="colors[]" value="<?php echo $single; ?>">
<?php
}
?>
<br>
<input type="submit" value="Submit!">
</form>
checkbox.php
<?php
echo "<pre>";
var_dump($_POST);
exit;
In your case:
<form action="/checkbox.php" method="post">
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-pencil"></em>
<em class="fa fa-trash"></em>
</td>
<td class="hidden-xs"><?php echo $navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid[]" value=<?php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel[]" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<input type="submit" value="Submit!">
</form>
Finally I solved this with Javascript. One of my partners helped me, I post this to help people in my situation.
I created a script, here is the code:
<script type="text/javascript">
function cogeNavioDyncoopnet(){
var checkedValueNavioD = null;
var inputElements = document.getElementsByClassName('checknaviod');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioD = inputElements[i].value;
break;
}
}//return checkedValueNavioD;
var input_nav_dyn = document.getElementById("nav_dyn");
input_nav_dyn.value = checkedValueNavioD;
}
function cogeNavioForSea(){
var checkedValueNavioFs = null;
var inputElements = document.getElementsByClassName('checknaviofs');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioFs = inputElements[i].value;
break;
}
}//return checkedValueNavioFs;
var input_nav_fs = document.getElementById("nav_fs");
input_nav_fs.value = checkedValueNavioFs;
}
</script>
Then I created a Form below, that collects values and sends them to my .php control file.
<div class="hidden-xs">
<form class="hidden-xs" method="POST"
action="../datos/actualizarnavios.php">
<input id="nav_dyn" type="text" name="idnaviodyncoop">
<input id="nav_fs" type="text" name="navioforsea" >
<input id="botonasignar" type="submit" name="enviardatosdynfs">
</form>
</div>
I hope this helps. Thanks for the feedback, as always.

How to get multiple selected checkbox values

I am workin on PHP codeigniter
See this attached image, here i need to get values of selected checkboxes and save in a database table by clicking on the button called CONFIRM
My view Code:
<?php foreach($studentlist as $llist){
echo form_open("trusts/MoveData?id=".$llist['id']); }?>
<button name="submit" type="submit" value="<?php echo #$studentlist1->id; ?>" class="btn btn-success btn-flat pull-right marginBot10px"><i class="icon_set_1_icon-76"></i> CONFIRM </button>
<?php echo form_close(); ?>
<table class="table table-bordered" id="employee_grid">
<thead>
<tr>
<!--<th><input type="checkbox" id="select_all"></th>-->
<th>Select</th>
<th>Student Name</th>
<th>Gender</th>
<th>Email</th>
<th>Phone</th>
<th>Program Id</th>
<th>Program Name</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
foreach($studentlist as $llist){
?>
<tr id="<?php echo $llist["id"]; ?>">
<td><input type="checkbox" name="ids[]" value="<?php echo $llist['id']; ?>"> </td>
<td><?php echo $llist['Name']; ?></td>
<td><?php echo $llist['Gender']; ?></td>
<td><?php echo $llist['Email']; ?></td>
<td><?php echo $llist['Phone']; ?></td>
<td><?php echo $llist['ProgramId']; ?></td>
<td><?php echo $llist['ProgramName']; ?></td>
<!-- <td></td>-->
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
How get selected checkbox values of this view in my controller?
When the form is submitted you an get the selected checkbox values like:
$selected = $this->input->post('ids');
here $selected is an array, so use foreach() to get its values.

MySQL is deleting the last row of html table / Code Igniter

This is my controller, that i use to send the row to the model:
function excluir(){
$codcliente = $this->input->post('codcliente');
$this->load->model("Cliente_class");
$this->Cliente_class->excluirCliente($codcliente);
redirect('cliente/busca');
}
my model:
function excluirCliente($codcliente){
$this->db->delete('cliente', array('codcliente' => $codcliente));
}
my table (view):
<form action="#" method="POST" id="form">
<div style="float: left;">
<input type="button" onclick="cadastro();" value="Novo cliente" name="botao" class="btn btn-primary">
</div>
<div class="dataTable_wrapper">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Código</th>
<th>Nome</th>
<th>Cidade</th>
<th>Telefone</th>
<th> </th>
</tr>
<tbody>
<?php
foreach ($records as $row){ ?>
<tr>
<td><input name="codcliente" id="codcliente" value="<?php echo $row->codcliente ?>"></td>
<td><?php echo $row->nome ?></td>
<td><?php echo $row->cidade ?></td>
<td><?php echo ($row->telefone == null) ? "-" : $row->telefone ?></td>
<td><center><input type="button" onclick="excluir();" value="Delete"></td>
</tr>
<?php } ?>
</tbody>
</form>
</thead>
</table>
and my JS:
function excluir(){
document.forms['form'].action = "<?php base_url();?>excluir";
document.forms['form'].submit();
}
its working fine, but the code is deleting my last row in relation with the table on html. i used the order by too see whats beeing deleted, and it works the same...
i dont know what's wrong
Your problem is that you are using the same form name name="codcliente" for all your rows and then submitting the whole form when you delete. The server sees the last value for codcliente which will be your last row and hence deletes it.
To fix, I suggest you use unique form names for each row for all your row fields. Eg. Instead of name="codcliente" use name="codcliente_<?php echo $row->id ?>". And then when you post to the server to delete, use a data-name=<?php echo $row->codcliente ?> on the delete button and in the onClick handler use $(this).data('name') to get the unique name of the field and post that to the server. I suggest you use ids instead of names as well.

PHP: Form echo's id in source but shows first id in link for all rows

I'm having an issue with a pop up form that has a field where the user needs to enter some info regarding a row of data.
The stuff he/she enters needs to be inserted into the table which I will do a Update query... But my issue is that the form echo's the id from the database in the source code but it doesn't show when I hover over the button.
After I press the button it only executes the query for the first id. And the others show only the 2nd and so forth until they are removed one by one. I don't know what I'm doing wrong:
<?php include_once('classes/profile.class.php');?>
<?php include_once('header.php');?>
<h1>
<?php _e('Traffic Fines Adjudication'); ?>
</h1>
<br>
<div class="tabs-left">
<?php
$result = "SELECT * FROM login_fines_adjudicated WHERE active = 0 ORDER BY date_issued";
$stmt = $generic->query($result);
//this function will take the above query and create an array
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
//do nothing
//with the array created above, I can create variables (left) with the outputted array (right)
$id = $row['id'];
$date = $row['date_added_db'];
$date_issued = $row['date_issued'];
$time_arrive = $row['time_arrived'];
$time_depart = $row['time_departed'];
$ref = $row['reference_code'];
$reason = $row['violation_reason'];
$location = $row['location'];
$bay = $row['bay_number'];
$licence = $row['licence'];
$punit = $row['parking_unit'];
$value = $row['value'];
$operator = $row['operator'];
$operator_id = $row['operator_id'];
//If date field is empty display nothing! Or sentence...
if(!empty($date))
{
?>
<table class="table">
<thead>
<tr>
<th>Photo</th>
<th>Date Added</th>
<th>Date Issued</th>
<th>Time Arrived</th>
<th>Time Departed</th>
<th>Reference Code</th>
<th>Violation Category</th>
<th>Location</th>
<th>Bay Number</th>
<th>Licence Plate</th>
<th>Parking Unit</th>
<th>Amount</th>
<th>Operator Name</th>
<th>Operator ID</th>
<th>Action</th>
</tr>
</thead>
<tr>
<td><img class="photo" src="photos/no-available.jpg" /></td>
<td><?php echo $date; ?><br /></td>
<td><?php echo $date_issued; ?></td>
<td><?php echo $time_arrive; ?></td>
<td><?php echo $time_depart; ?></td>
<td><?php echo $ref; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $location; ?></td>
<td><?php echo $bay; ?></td>
<td><?php echo $licence; ?></td>
<td><?php echo $punit; ?></td>
<td><?php echo $value; ?></td>
<td><?php echo $operator; ?></td>
<td><?php echo $operator_id; ?></td>
<td>
<form action="approve.php" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
<p>Approve</button></p>
</form>
<div id="reject-form" class="modal hide fade">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3><?php _e('Reject Reason'); ?></h3>
</div>
<div class="modal-body">
<div id="message"></div>
<form action="reject.php" method="post" name="rejectform" id="rejectform" class="form-stacked rejectform normal-label">
<div class="controlgroup rejectcenter">
<div class="control">
<input id="reject" name="reject" type="text"/>
</div>
</div>
<input type="hidden" name="delete_id" value="<?php echo $id; ?>" />
</form>
</div>
<div class="modal-footer">
<?php _e('Submit'); ?></button>
<p class="pull-left"><?php _e('Please give a short reason for rejecting.'); ?></p>
</div>
</div>
</form>
<p><a data-toggle="modal" href="#reject-form" id="rejectlink" tabindex=-1><button url="#" class="btn btn-primary">Reject</button></a></p>
</td>
</tr>
</table>
<?php
}
}
?>
</div>
<?php include ('footer.php'); ?>
Any help would be appreciated!
The problem is that you have many elements with the same id attribute. Genarally, it is a really bad practice to have more than elements with the same id, not to mention it does not conform to the HTML Spec, according to which "id = [...] This name must be unique in a document.". Therefore, you are strongly advised to fix all IDs mking sure it is unique (e.g. appending the current entry's ID).
What causes the specific problem you described in your question is this:
You define for every table a pop-up dialog (div), with id reject-form.
Then, you implement the "reject"-button as a link with href="#reject-form".
So, when you click on any "reject"-button, the user is taken to the first element found in the DOM with id="reject-form" (which is always the dialog for the first entry).
In order to fix that, you can append each entry's ID in the id attributes in two locations: the pop-up dialog and the "reject"-button:
// Replace that:
<div id="reject-form" class="modal hide fade">
// with this:
<div id="reject-form-<?php echo $id; ?>" class="modal hide fade">
// Replace that:
<a data-toggle="modal" href="#reject-form" id="rejectlink" tabindex=-1>
// with this:
<a data-toggle="modal" href="#reject-form-<?php echo $id; ?>" id="rejectlink" tabindex=-1>

Categories