Get php checkbox data from multiple tables - php

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.

Related

Display selected row on another page, on button click

I have a table reading out a few columns of a db, with an edit button in each row. All I am trying to do is, when the edit button is clicked, that row is displayed with all of its columns in another page. Any help would be great. I know it's simple, but I can get it to work. I am able to navigate to the next page, but not sure how to carry the id of the specific row and read that out. Thanks.
index. php
<form id="mainform" action="editFunding2.php">
<div class='row'>
<div class='col-12'>
<div class='table table-responsive'>
<table class='table table-striped table-bordered datatable active' id='grantTable'>
<thead>
<tr>
<th>FP ID</th>
<th>Short Title</th>
<th>PI</th>
<th>Department</th>
<th>Funding Type</th>
<th>Change Funding Type</th>
</tr>
</thead>
<tbody>
<?php
//Foreach loop iterates through each column of $getallrows function
foreach($allRows as $rowID => $rowInfo){ ?>
<tr>
<td><?php echo $rowInfo['fpID'];?></td>
<td><?php echo $rowInfo['shortTitle'];?></td>
<td><?php echo $rowInfo['PI'];?></td>
<td><?php echo $rowInfo['Department'];?></td>
<td><?php echo $rowInfo['fundingType'];?></td>
<!--Create dynamic id -->
<?php $rdDrop = "ddgrantType_".$rowInfo['fpID'];?>
<td>
<button type="submit" class="btn btn-danger" name="action" value='Change Funding Type'>Change Funding</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</form>
My editFunding2.php page
<div class='row'>
<div class='col-12'>
<div class='table table-responsive'>
<table class='table table-striped table-bordered datatable active' id='grantTable'>
<thead>
<tr>
<th>FP ID</th>
<th>Short Title</th>
<th>PI</th>
<th>Department</th>
<th>Division</th>
<th>Sponsor Name</th>
<th>Project Start</th>
<th>Project End</th>
<th>Funding Type</th>
<th>Yes/No</th>
<th>Proper Type If No</th>
<!-- <th>Exclusions</th>
<th>FP Durtation</th> -->
<th>Comment</th>
</tr>
</thead>
<tbody>
<?php
session_start();
$editRow = $_REQUEST['fpID'];
$mssql = mysqli_query("SELECT * FROM spacing.METADATA_WEBFORM WHERE FUNDING_PROPOSAL_ID = $fpID' ") or die (mysqli_error());
while($rowInfo = mysqli_fetch_array($mssql)) {
?>
<tr>
<td><?php echo $rowInfo['fpID'];?></td>
<td><?php echo $rowInfo['shortTitle'];?></td>
<td><?php echo $rowInfo['PI'];?></td>
<td><?php echo $rowInfo['Department'];?></td>
<td><?php echo $rowInfo['Division'];?></td>
<td><?php echo $rowInfo['sponsorName'];?></td>
<td><?php echo $rowInfo['Date_Project_Start']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['Date_Project_End']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['fundingType'];?></td>
<?php } ?>
<!--Create dynamic id -->
<?php $rdDrop = "ddgrantType_".$rowInfo['fpID'];?>
<td>
<div class="form-check">
<label class="form-check-label">
<input type="radio" class='form-check-input' name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="Yes" id="rdYes" onclick="disable('<?php echo $rdDrop;?>')" checked/> Yes
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class='form-check-input' type="radio" name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="No" id="rdNo" onclick="enable('<?php echo $rdDrop;?>')"/> No
</label>
</div>
</td>
<td>
<div class="dropdown">
<select class='form-control' name="ddgrantGroup[<?php echo $rowInfo['fpID'];?>]" id="<?php echo $rdDrop;?>" disabled>
<option value=''>Select Proper Funding Type</option>
<option value="Corporate Sponsor">Corporate Sponsor</option>
<option value="Federal">Federal</option>
<option value="Foundation Selected">Foundation Selected</option>
<option value="Internally Funded">Internally Funded</option>
<option value="State/Local">State/Local</option>
</select>
</div>
</td>
<td>
<div class="comment">
<textarea class="form-control" aria-label="With textarea" name="grantComment[<?php echo $rowInfo['fpID'];?>]" id="grantComment" placeholder="Comments"></textarea>
</div>
</td>
</tr>
</tbody>
</div>
</div>
</div>
<div class='row'>
<div class='col-12 text-right'>
<button type="submit" class="btn btn-secondary formsubmitbttn" name="action" value='save'>Save</button>
<button type="submit" class="btn btn-primary formsubmitbttn" name="action" value='complete'>Complete and Save</button>
</div>
</div>
</div>
Change your button to a
My Link to xx
In editFunding2.php :
$editRow = $_GET['id'];

Call all posts from Wordpress database with category names

Our Wordpress site are infected and we are deleted all files. we have now only database and want to extract all information from db and afther this copy on other site.
so I am using this php class to connect db and call some information. I am calling now posts with data, but need categories also.
For example:
Post Title, Post Content, Post Category
here is my working code without category names
$posts = $db->get('c3624322676f_wp_posts');
but I have no idea how to call categories names here.
This is my html + foreach
<table class="table table-hover">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Title</th>
<th>Type</th>
<th>Content</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post) : ?>
<tr>
<th scope="row"><?php echo $count++; ?><a href=""></th>
<td><?php echo $post['post_title']; ?></td>
<td><?php echo $post['post_type']; ?></td>
<td><?php echo strip_tags ($post['post_content']); ?></td>
<td>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $post['ID']; ?>" />
<input style="cursor:pointer;" type="submit" class="btn btn-primary" value="Delete" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
This should work
<table class="table table-hover">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Title</th>
<th>Type</th>
<th>Content</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post) : ?>
<tr>
<th scope="row"><?php echo $count++; ?><a href=""></th>
<td><?php echo $post['post_title']; ?></td>
<td><?php echo $post['post_type']; ?></td>
<td><?php echo strip_tags ($post['post_content']); ?></td>
<?php
$separator = ',';
$output = '';
$categories = get_the_category($post['post_title']);
if ($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>
<td>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $post['ID']; ?>" />
<input style="cursor:pointer;" type="submit" class="btn btn-primary" value="Delete" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Retrieve data from checkboxes

I want to get all the checked data from my table. I'm using checkboxes in my table. When I click "change status", it will change my role status.
But I have problems with retrieving the checkboxes values. In the code below, it failed to update my data. Checkboxes value that I retrieve is NULL. How can I solve this problem?
Model
function deaktifRole($id_role,$editBy)
{
$data = array(
'update' =>date('Y-m-d H:i:s'),
'updateBy' =>$editBy,
'flag' => '0'
);
$this->db->where('id_role',$id_role);
$this->db->update('tbl_role',$data);
}
Controller
function deaktifRole()
{
$session_data = $this->session->userdata('logged_in');
$editBy = $session_data['username'];
foreach ($this->input->post['pilih'] as $value) {
$this->Role->deaktifRole($value->value,$editBy);
}
redirect('Home/Role');
echo '<script>alert("Your form was successfully submitted!");</script>';
}
View
<div class="x_panel">
<div class="x_title">
<h2>Manage Staff Role</small></h2>
<?php echo form_open_multipart('Home/deaktifRole');?>
<div align="right">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Add</button>
<button type="submit" class="btn btn-primary">Change Status</button>
</div>
<div class="clearfix"></div>
</div>
<div class="x_content">
<table id="datatable-checkbox" class="table table-striped table-bordered bulk_action">
<thead>
<tr>
<th><input type="checkbox" id="check-all" class="flat"></th>
<th>No</th>
<th>Role</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php $j=0; foreach ($RoleList as $rows)
{
$j++;
?>
<tr>
<td><input type="checkbox" class="flat" name="pilih[]" value="<?php echo $rows['id_role']; ?>"></td>
<td><?php echo $j; ?></td>
<td><?php echo $rows['role']; ?></td>
<td>Aktif</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>
As $this->input->post('pilih') is just an associative array, you can access it directly without ->value properties. Try use this code :
foreach ($this->input->post('pilih') as $value) {
$this->Role->deaktifRole( $value,$editBy );
}
use $this->input->post('pilih');
to fetch checkbox data.

How to print specific data on Codeigniter

Screenshoot 1 contain all of my data on database table while screenshoot 2 is detail of someone data whenever i click on search icon on screenshoot 1.
What i need is, how to print only someone data without pass the ID/parameter on my url for calling the controller? Btw, im using DOMPDF as my pdf generator.
Here my code :
Model
public function view_kartu($kode_pasien)
{
$query = $this->db->query("SELECT * FROM tb_pasien where kode_pasien='$kode_pasien' LIMIT 1");
return $query->result_array();
}
Controller
public function cetakkartu($id) {
//set a value for $kode_pasien
$kode_pasien = $id;
// Load all views as normal
$data['title'] = "Data Pasien | Praktik Dokter Umum";
$data['kartu_pasien'] = $this->a_model->view_kartu($kode_pasien);
$this->load->view('cetak-kartu', $data);
// Get output html
$html = $this->output->get_output();
// Load library
$this->load->library('dompdf_gen');
// Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream("cetak-kartu" . ".pdf", array ('Attachment' => 0));}
View for all data in my database table (screenshoot 1)
<div class="box">
<div class="box-body">
<div class="btn-group">
<a href="<?=base_url();?>index.php/a_controller/regismanual"/>
<button class="btn-success btn">
Tambah Pasien</button>
</a>
</div>
<br>
<br>
<div class="table-responsive">
<table id="rekam" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Kode Pasien</th>
<th>Nama Pasien</th>
<th>Email Pasien</th>
<th>Alamat Pasien</th>
<th>Tanggal Lahir</th>
<th>Umur</th>
<th>JK</th>
<th>No. Telp</th>
<th>Opsi</th>
</tr>
</thead>
<tr>
<tbody>
<?php
if(isset($pasien)){
$no=0; foreach ($pasien as $isi): $no++;
# code...
?>
<td><?php echo $isi->kode_pasien?></td>
<td><?php echo $isi->nama_pasien?></td>
<td><?php echo $isi->email_pasien?></td>
<td><?php echo $isi->alamat_pasien?></td>
<td><?php echo $isi->tanggal_lahir?></td>
<td><?php echo $isi->umur?></td>
<td><?php echo $isi->kode_jk?></td>
<td><?php echo $isi->no_telp?></td>
<td>
<a href="<?=base_url()?>index.php/a_controller/updatepasien/<?php echo $isi->kode_pasien?>">
<button type="button" class="btn-success btn"><span class="glyphicon glyphicon-pencil"></span></button>
</a>
<a href="<?=base_url()?>index.php/a_controller/deletepasien/<?php echo $isi->kode_pasien?>" onclick="return confirm('Apakah anda yakin akan menghapus data ini?')">
<button type="button" class="btn-inverse btn"><span class="glyphicon glyphicon-trash"></span></button>
</a>
<a href="<?=base_url()?>index.php/a_controller/detail/<?php echo $isi->kode_pasien?>">
<button type="button" class="btn-warning btn"><span class="glyphicon glyphicon-search"></span></button>
</a>
</td>
</tr>
</tbody>
<?php endforeach;}?>
</table>
</div>
View for my detail data (screenshoot 2)
<div class="box">
<div class="box-body">
<div class="btn-group">
<a href="<?php echo base_url("index.php/a_controller/cetakkartu/4");?>"/>
<button class="btn-success btn">
Cetak Kartu Berobat</button>
</a>
</div>
<br>
<br>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<?php
if(isset($kartu)){
$no=0; foreach ($kartu as $isi): $no++;
# code...
?>
<tr>
<td>Kode Pasien</td>
<td><?php echo $isi->kode_pasien?></td>
</tr>
<tr>
<td>Nama Pasien</td>
<td><?php echo $isi->nama_pasien?></td>
</tr>
<tr>
<td>Email Pasien</td>
<td><?php echo $isi->email_pasien?></td>
</tr>
<tr>
<td>Alamat Pasien</td>
<td><?php echo $isi->alamat_pasien?></td>
</tr>
<tr>
<td>Tanggal Lahir</td>
<td><?php echo $isi->tanggal_lahir?></td>
</tr>
<tr>
<td>Umur</td>
<td><?php echo $isi->umur?></td>
</tr>
<tr>
<td>Jenis Kelamin</td>
<td><?php echo $isi->kode_jk?></td>
</tr>
<tr>
<td>No. Telp</td>
<td><?php echo $isi->no_telp?></td>
</tr>
<?php endforeach;}?>
</table>
</div>
</div>
</div>
Like you see, i need to pass someone ID like this
<a href="<?php echo base_url("index.php/a_controller/cetakkartu/4");?>"/>
<button class="btn-success btn">
Cetak Kartu Berobat</button>
</a>
It will only print someone with ID = 4 , it makes me cant print others person detail data. Did i need to change the ID whenever i want to print someone data? It's really hard.
anyone can help me?
<a href="<?php echo base_url("index.php/a_controller/cetakkartu").$kartu[0]['kode_pasien'];?>"/>
<button class="btn-success btn">
Cetak Kartu Berobat
</button>
</a>

Codeigniter Update cart

I have the problem with update function in CodeIgniter. The cart is not updating and don't understand why. Can you help me to find a solution for this issue?
This is my Update function
public function update($in_cart = null) {
$data = $_POST;
$this->cart->update($data);
//show cart page
redirect('cart','refresh');
}
This is my form inside sidebar.php
<form action="cart/update" method="POST">
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items) : ?>
<input type="hidden" name="<?php echo $i.'[rowid]'; ?>" value="<?php echo $items['rowid']; ?>" />
<tr>
<td><input type="text" style="color:black; text-align:center;margin-bottom:5px;" name="<?php $i.'[qty]'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" size="3"></td>
<td><?php echo $items['name']; ?></td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td></td>
<td class="right"><strong>Total</strong></td>
<td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<br>
<p><button class="btn btn-default" type="submit">Update Cart</button>
<a class="btn btn-default" href="cart">Go to Cart</a></p>
</form>
Try using $this->input->post() to get all form posted data.
https://ellislab.com/codeigniter/user-guide/libraries/input.html

Categories