How to select id from the datatables using checkbox - php

Heres my code: HTML with Server side to get data from the database. How do I get id from the datatables using checkbox option?
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<h4>
<th></th> <!-- For checkbox column-->
<th>ID Number</th>
<th>Lastname</th>
<th>Firstname</th>
<th>Middlename</th>
<th>Gender</th>
<th>Course</th>
<th>Department</th>
<th>Address</th>
<th>Contact</th>
<th>Birthdate</th>
<th>View</th>
<th>Events</th>
</h4>
</tr>
</thead>
<tbody>
<?php
include 'pgconnect.php';
$mydata = mysqli_query($conn,"SELECT * FROM student") or die(mysql_error());
while($record = mysqli_fetch_assoc($mydata))
{
$id=$record['id'];
?>
<tr>
<td><input type="checkbox"></td>
<td class="id"><?php echo $record['id'] ?></td>
<td class="lname"><?php echo $record['last_name'] ?></td>
<td class="fname"><?php echo $record['first_name'] ?></td>
<td class="mname"><?php echo $record['mid_name'] ?></td>
<td class="sex"><?php echo $record['gender'] ?></td>
<td class="course"><?php echo $record['course'] ?></td>
<td class="dept"><?php echo $record['department'] ?></td>
<td class="add"><?php echo $record['home_address'] ?></td>
<td class="contact"><?php echo $record['contact'] ?></td>
<td class="bdate"><?php echo $record['birthdate'] ?></td>
<td> View Records </td>
<td> <a title='Click here to update record.'href='#edit' data-toggle='modal'><i class="fa fa-pencil fa-fw"></i></a>
<a title='Click here to add record.' href='#'><i class="fa fa-plus fa-fw"></i></a>
<a title='Click here to delete record.' href='delete.php?del=<?php echo $record['id'] ?>'><i class="fa fa-trash-o fa-fw"></i></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
Here's my script:
$(document).ready(function(){
$('#dataTables-example').dataTable();
});
I tried to get the ID but still it didn't works. Hope you can help me.

In your work, you get the ID in table. your should grab it from the checkbox if you use jquery, as in;
<script>
$(document).ready(function(){
document.getElementByID("ckmyid").innerHTML;
});
</script>
Note:add Selector ID in your checkbox, first, say, "ckmyid" so the code will be like this:
<input id="ckmyid" type="checkbox" value="<?php echo $record['id'] ?>">
Based on your structure, you can grab it directly from the link in your view records link if you want to place the ID there,as in;
<td> View Records </td>

Related

instant delete when the button is cliked

im trying to delete from a datatable when i click the remove button the data will be removed for a quick action in the admin side but my code dose not work i tried to fix it but i don't see any problem in the code here is my code
<div class="table-responsive">
<table id="datas" class="table table-striped table-bordered fixed" style="width:100%">
<thead style="color:black;" >
<th>id</th>
<th>Company Name</th>
<th>Product Name</th>
<th>Weight</th>
<th>Price Per Gram</th>
<th>Quantity</th>
<th>Type</th>
<th>Category</th>
<th>Product Price</th>
<th>Image</th>
<th>Total Price</th>
<th class="text-center">Actions</th>
</thead>
<?php
$get = mysqli_query($conn,"SELECT * FROM stock;");
?>
<tbody>
<?php
while ($row=mysqli_fetch_array($get)) {
$id=$row['id'];
$company=$row['company_name'];
$name=$row['product_name'];
$weight=$row['weight'];
$price_per_gram=$row['price_per_gram'];
$quantity=$row['quantity'];
$type=$row['type'];
$category=$row['category'];
$price=$row['product_price'];
$img=$row['img'];
$total=$row['total_price'];
?>
<tr>
<td ><?php echo $id;?></td>
<td><?php echo $company;?></td>
<td><?php echo $name;?></td>
<td><?php echo $weight;?> g</td>
<td><?php echo $price_per_gram;?> $</td>
<td><?php echo $quantity;?></td>
<td><?php echo $type;?></td>
<td><?php echo $category;?></td>
<td><?php echo $price;?></td>
<td>
<img src="product_img/<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;">
</td>
<td><?php echo $total;?></td>
<td style="width: 20px"> Edit
<button class="btn btn-danger" name="delete" type="submit" value="<?php echo "$id" ?>">Delete</button> </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
if (isset($_POST['delete'])) {
$delete = mysqli_query($conn,"DELETE FROM stock WHERE id= ".$_POST['delete']." ");
header("location:viewstcok.php");
} ?>
i give the delete button a value when clicked so it will delete with that value but it did not seems to work can any one help me
you have to placed table inside <form></form> tag
<div class="table-responsive">
<form>
<table id="datas" class="table table-striped table-bordered fixed" style="width:100%">
<thead style="color:black;" >
<th>id</th>
<th>Company Name</th>
<th>Product Name</th>
<th>Weight</th>
<th>Price Per Gram</th>
<th>Quantity</th>
<th>Type</th>
<th>Category</th>
<th>Product Price</th>
<th>Image</th>
<th>Total Price</th>
<th class="text-center">Actions</th>
</thead>
<?php
$get = mysqli_query($conn,"SELECT * FROM stock;");
?>
<tbody>
<?php
while ($row=mysqli_fetch_array($get)) {
$id=$row['id'];
$company=$row['company_name'];
$name=$row['product_name'];
$weight=$row['weight'];
$price_per_gram=$row['price_per_gram'];
$quantity=$row['quantity'];
$type=$row['type'];
$category=$row['category'];
$price=$row['product_price'];
$img=$row['img'];
$total=$row['total_price'];
?>
<tr>
<td ><?php echo $id;?></td>
<td><?php echo $company;?></td>
<td><?php echo $name;?></td>
<td><?php echo $weight;?> g</td>
<td><?php echo $price_per_gram;?> $</td>
<td><?php echo $quantity;?></td>
<td><?php echo $type;?></td>
<td><?php echo $category;?></td>
<td><?php echo $price;?></td>
<td>
<img src="product_img/<?php echo $img; ?>" style="height:5rem;width:5rem;border-radius:10px;">
</td>
<td><?php echo $total;?></td>
<td style="width: 20px"> Edit
<button class="btn btn-danger" name="delete" type="submit" value="<?php echo "$id" ?>">Delete</button> </td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
<?php
if (isset($_POST['delete'])) {
$delete = mysqli_query($conn,"DELETE FROM stock WHERE id= ".$_POST['delete']." ");
header("location:viewstcok.php");
} ?>

How to display multiple rows from another table on the same line when fetching data into table using php

I have two tables in mysql, one table holds pics been snapped from webcam with image descriptions and another table which holds the user's registration details.
The image below will explain better.
This is the photos table with a track_id of the user
This is the user registration table with a unique id
On the the photos table the user details appeared twice with different description of the image uploaded
here is the result of the fetched details from both tables
My objective is to make the pictures appear side by side but when I fetched from database it was duplicating the information due to the fact that the user appeared twice on the photos table. And when I used group by it only displayed one picture.
Here is my source code
<?php require_once('queries.php'); ?>
<?php include("header.php"); ?>
<?php include("head.php"); ?>
<?php include("sidenavs.php"); ?>
<?php include("topnavs.php"); ?>
<div class="wrapper">
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12">
<h4 class="form-signin-heading">Users Details.</h4>
<hr />
<span class="glyphicon glyphicon-pencil"></span> Add User
<hr />
<div class="content-loader">
<table cellspacing="0" width="100%" id="example" class="table display table-striped table-hover table-responsive">
<thead>
<tr>
<th>#ID</th>
<th>Name</th>
<th>dob</th>
<th>gender</th>
<th>bvn</th>
<th>business name</th>
<th>contact address</th>
<th>Town/City</th>
<th>lga</th>
<th>State</th>
<th>phone 1</th>
<th>phone 2</th>
<th>email</th>
<th>products & services rendered</th>
<th>sub society name</th>
<th>position</th>
<th>mem id no</th>
<th>next kin name</th>
<th>relationship</th>
<th>phone of next kin</th>
<th>payment status</th>
<th>photo</th>
<th>signature</th>
<th>date registered</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
require_once 'db/dbconfig.php';
$i =1;
$stmt = $db_con->prepare("SELECT * FROM users ORDER BY id DESC");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
$idm = $row['id'];
$stmts = $db_con->prepare("SELECT * FROM fotos where track_id ='$idm' group by $idm");
$stmts->execute();
$rows=$stmts->fetch(PDO::FETCH_ASSOC);
?>
<tr>
<td><?php echo $i; $i++;?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['bvn'];?></td>
<td><?php echo $row['business_name'];?></td>
<td><?php echo $row['contact_addr'];?></td>
<td><?php echo $row['Town_City'];?></td>
<td><?php echo $row['lga'];?></td>
<td><?php echo $row['State'];?></td>
<td><?php echo $row['phone_1']; ?></td>
<td><?php echo $row['phone_2']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['products_services_rendered'];?></td>
<td><?php echo $row['sub_society_name']; ?></td>
<td><?php echo $row['position']; ?></td>
<td><?php echo $row['mem_id_no'];?></td>
<td><?php echo $row['next_kin_name'];?></td>
<td><?php echo $row['relationship'];?></td>
<td><?php echo $row['phone_of_next_kin'];?></td>
<td><?php if($row['payment_status'] == 'Paid'){echo '<span class="alert-success" style="padding:3px; ">Paid</span>';}else{echo '<span class="alert-warning" style="padding:3px; ">Unpaid</span>';}?></td>
<td><img src="fotos/<?php if($rows['des'] == 'Photo'){echo $rows['id_foto'];}?>" width="50" height="50"></td>
<td><img src="fotos/<?php if($rows['des'] == 'Signature'){echo $rows['id_foto'];} ?>" width="50" height="50"></td>
<td><?php echo $row['date_registered'];?></td>
<td class="td-actions text-right">
<!--<a id="<?php echo $row['id']; ?>" class="edit-link btn btn-primary btn-simple btn-xs" rel="tooltip" href="#" title="Edit User"><i class="material-icons">edit</i></a>-->
<a id="<?php echo $row['id']; ?>" class="delete-link btn-danger btn-simple btn-xs" rel="tooltip" href="#" title="Delete">
<span class="glyphicon glyphicon-trash"></span>
</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php include("footer2.php"); ?>

Passing Form Data to separate PHP Page

I have a table with multiple columns (index.php). One column is a checkbox. Whenever the checkbox is checked, it displays another row where you can select a quantity. You can then hit a button called "Add to Order" and it will take you to a confirmation page (index-order.php) where I want it to display each row along with all of the data in that specified row that has the checkbox checked. Currently, I am getting no errors in my console, but no data is being displayed at all.
What do I need to change to make this happen? Here is what I have so far.
Index.php code:
<form name="form1" method="POST" action="index-order.php">
<section id="addToOrder">
<button type="submit" class="order" id="order" name="order" value="AddToOrder">Add to Order</button>
</section>
<br>
<div id="my-div2" class="ui-widget">
<div class="ui-widget">
<table id="merchTable" cellspacing="5" class="sortable">
<thead>
<tr class="ui-widget-header">
<th class="sorttable_nosort"></th>
<th class="sorttable_nosort">Loc</th>
<th class="merchRow">Report Code</th>
<th class="merchRow">SKU</th>
<th class="merchRow">Special ID</th>
<th class="merchRow">Description</th>
<th class="merchRow">Quantity</th>
<th class="sorttable_nosort">Unit</th>
<th style="display: none;" class="num">Quantity #</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td class="ui-widget-content"><input type="checkbox" class="check" name="check"></td>
<td name="rows[0][0][loc]" class="loc ui-widget-content" id="loc-<?php echo intval ($row['Loc'])?>"><?php echo $row['Loc'];?></td>
<td name="rows[0][0][rp-code]" class="rp-code ui-widget-content" align="center" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
<td name="rows[0][0][sku]" class="sku ui-widget-content" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
<td name="rows[0][0][special-id]" class="special-id ui-widget-content" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
<td name="rows[0][0][description]" class="description ui-widget-content" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
<td name="rows[0][0][quantity]" class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
<td name="rows[0][0][unit]" class="unit ui-widget-content" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
<td name="rows[0][0][quant]" style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" name="value" id="test"></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</form>
Index-order.php:
<?php if(isset($_POST['rows'])): ?>
<table cellspacing="20">
<tr align="center">
<th>Loc</th>
<th>Report Code</th>
<th>SKU</th>
<th>Special ID</th>
<th>Description</th>
<th>Quantity</th>
<th>Unit</th>
<th>Quantity #</th>
</tr>
<?php
foreach($_POST['rows'][0] as $row):
?>
<tr align="center">
<td><?php echo $row['loc']; ?></td>
<td><?php echo $row['rp-code']; ?></td>
<td><?php echo $row['sku']; ?></td>
<td><?php echo $row['special-id']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quant']; ?></td>
</tr>
<?php
endforeach;
?>
</table>
I am okay with the precedent answer, I never heard about this kind of method with PHP and it doesn't seems to be the right solution. Anyway, the following post would maybe help you : How to get value from td's via $_POST.
You cannot transfer datas through POST by using td ; but an alternative would be to use the "hidden" type of forms element :
<form action="script.php" method="post">
<td class=".."><input type="hidden" name="td1" value="...">value</td>
...
</form>
In PHP, you'll grab the data with the $_POST array and the td1 name :
<?php var_dump($_POST); ?>
Itwould in my opinion be the easier way to get what you want in a proper way ; the link I gave upper is also talking about DOMDocument, but it looks more complex to manage with.

how can i create a download button on it?

//a table to display file from the database
<thead class="thead-inverse">
<tr>
<th>Description </th>
<th>Category</th>
<th>Upload By</th>
<th>Date</th>
<th></th>
</tr>
<thead>
<tbody class="sc">
<?php
// fetch the records from db
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['fdesc']; ?></td> //filename
<td><?php echo $row['category']; ?></td>
<td><?php echo $row['username']; ?></td> //user who have upload the file
<td><?php echo $row['fdatein']; ?></td>
//the download button
<td><a href="" ><button type="button" class="btn btn-unique active" data-toggle="tooltip" data-placement="right" title="Download"><i class="fa fa-download"></i> // get the specific to download
</button></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
you can add the id in the the link url like so:
<a href="d1.php?fileId=<?php echo $row['fileID']; ?>" >
then in d1.php the id will be in the variable $_GET['fileID']

Put the "$message" inside the table in codeigniter

I have a problem i want to put the $message inside the table that is a equivalent as a "Search not found".
Here is my picture outside the table $message = Search not found
View:
<div class="z table-responsive" >
<table class=" table table-hover" >
<thead >
<tr >
<th>ID Number</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
<th>Sex</th>
</tr>
</thead>
<?php if ( isset($message) ){
echo $message;
} else{ foreach($results as $row){
?>
<tbody>
<tr>
<td><?php echo $row-> Idnumber ?></td>
<td class="text-capitalize "><?php echo $row -> Firstname ?></td>
<td class="text-capitalize"><?php echo $row->Middlename ?></td>
<td class="text-capitalize"><?php echo $row-> Lastname ?></td>
<td class="text-capitalize"><?php echo $row-> Sex?></td>
<td>
Option
</td>
</tr>
</tbody>
<?php }} ?>
</tbody>
</table>
</div>
You are using wrong end tags.
This will print Search not found inside the table with colspan 5 and center aligned
Note: Most of time we check where foreach value is empty ($results). But in your case you are checking some thing else ($message).
Changes
<tr> <!-- Changed -->
<td colspan="5" align="center"><?php echo $message; ?> </td>
</tr>
if ( isset($message) ){ is changed to if (!empty($message)){
Final code
<div class="z table-responsive" >
<table class=" table table-hover" >
<thead >
<tr >
<th>ID Number</th>
<th>First name</th>
<th>Middle name</th>
<th>Last name</th>
<th>Sex</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($message)) # improved
{
?>
<tr> # Changed
<td colspan="5" align="center"><?php echo $message; ?> </td>
</tr>
<?php
}
else
{
foreach($results as $row)
{
?>
<tr>
<td><?php echo $row-> Idnumber ?></td>
<td class="text-capitalize "><?php echo $row -> Firstname ?></td>
<td class="text-capitalize"><?php echo $row->Middlename ?></td>
<td class="text-capitalize"><?php echo $row-> Lastname ?></td>
<td class="text-capitalize"><?php echo $row-> Sex?></td>
<td>
<a href="<?php echo site_url('viewstudentinalpha/viewspecific/'.$row->Id) ?>" class="btn btn-info " style="font-size: 18px;" type="submit" name="submit" role="button">
Option
</a>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
Edit 01
In Controller
function search_keyword()
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$keyword = $this->input->post('keyword');
$data['results'] = $this->model_adminlogin->search($keyword);
$this->load->view('result_view',$data);
}
In View
Alter your if like this.
Copy all codes i have added in above for your view. and only change this few lines.
<?php
if (!empty($results)) # Change
{
?>
<tr> # Change
<td colspan="5" align="center"> Search not found </td> # Change
</tr>
<?php
}
else
{
This isn't an issue with CodeIgniter so much as your code. What you are trying to do with the code is to put the message between the <thead> and <tbody>, and not in any table tags.
This is the same as trying to put it between rows in a table, and therefore gets interpreted as not being part of the table, and drawn outside of the table as a result.
If you put the message into the <tbody>, it will go in the right place, so you can do:
<?php if ( isset($message) ){
echo "<tbody><tr><td colspan='5'>" . $message . "</td></tr></tbody>";
}....
try this
<tbody>
<?php
if ( isset($result )
// or if (!isset($message)) //depend on your code
{
foreach($results as $row){
?>
<tr>
<td><?php echo $row-> Idnumber ?></td>
<td class="text-capitalize "><?php echo $row -> Firstname ?></td>
<td class="text-capitalize"><?php echo $row->Middlename ?></td>
<td class="text-capitalize"><?php echo $row-> Lastname ?></td>
<td class="text-capitalize"><?php echo $row-> Sex?></td>
<td>
Option
</td>
</tr>
<?php }}
else
//or elseif(isset($message)) //depend on your code
{
echo $message; ?>
}
</tbody>
</table>
</div>

Categories