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");
} ?>
Related
Right now I am writing a program in core PHP, but I am not able to write a perfect oops concept as I m rewriting the code many times.
for Eg.
public function all()
{
$init = new Database;
$sql = "SELECT * FROM categories";
// Result
$result = $init->conn->query($sql);
if ($result->num_rows > 0) {
?>
<table class="table table-hover">
<thead>
<tr>
<th>Action</th>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td>
<form action="update_categories.php?update_id=<?php echo $row['id'] ?>" method="post">
<button type="submit" class="btn btn-primary" name="update">Update</button>
</form>
<form action="delete_categories.php?id=<?php echo $row['id'] ?>" method="post">
<button type="submit" name="delete" class="btn btn-danger">Delete</button>
</form>
</td>
<td><?php echo $row["id"] ?></td>
<td><?php echo $row["category_name"] ?></td>
<td><?php echo $row["category_description"] ?></td>
</tr>
<?php
} ?>
</tbody>
<?php
} else {
?>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">no result</th>
<td>no result</td>
<td>no result</td>
</tr>
</tbody>
<?php
}
}
So here you can see I have to fetch the data from the database but in this function, I have included Html too so if you want to add database data to another place I have to write the function for that again and fetch the values.
Is there any way I can only fetch the value and i can use the function to print it wherever I want.
i am successfully fetching data from model named user_model to my controller named as login_controller. as shown below
in user_model:
function fetchData()
{
$this->db->select('DeviceName, DeviceType,RegistrationDateTime,LastUpdateDateTime,LastPushNotificationSent');
$query = $this->db->get('userinfo');
$res=$query->result();
if($res)
{
return $res;
}
else
{
return NULL;
}
}
in my controller i have:
function displayDatabase()
{
$data['tableInfo']=$this->user_model->fetchData();
$this->load->view('adminPanel_view',$data);
}
where in adminPanel_view i am doing this:
<table class="table table-striped table-bordered table-hover table-full- width" id="sample_1">
<thead>
<tr>
<th>Serial No.</th>
<th class="hidden-xs">Device Name</th>
<th>Device Type</th>
<th>RegistrationDateTime </th>
<th>LastUpdateTime</th>
<th>LastPushNotificationSent</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php //print_r ($tableInfo);exit;?>
<?php foreach($tableInfo as $r): ?>
<tr>
<?php echo $r->DeviceName ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
but i am unable to show the data in table format. What am i doing wrong here.The data is displayed like this
Please try this in your view file...
<table class="table table-striped table-bordered table-hover table-full- width" id="sample_1">
<thead>
<tr>
<th>Serial No.</th>
<th class="hidden-xs">Device Name</th>
<th>Device Type</th>
<th>RegistrationDateTime </th>
<th>LastUpdateTime</th>
<th>LastPushNotificationSent</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php //print_r ($tableInfo);exit;?>
<?php foreach($tableInfo as $r): ?>
<tr>
<td><?php echo $r->no ?></td>
<td><?php echo $r->DeviceName ?></td>
<td><?php echo $r->DeviceType ?></td>
<td><?php echo $r->RegistrationDateTime ?></td>
<td><?php echo $r->LastUpdateTime ?></td>
<td><?php echo $r->LastPushNotificationSent ?></td>
<td><?php echo $r->Actions ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Hope this will help you.
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>
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 table code:
<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>
Why not just use -
$EMPLOYEES = array_unique($EMPLOYEES);
Try this
<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 $emp='';
foreach($EMPLOYEES as $employee){
if($emp!=$employee->username){ # if username contain duplicate values
?>
<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 }$emp=$employee->username;} ?>
</tbody>
</table>
Good day. Im trying to have some functionality on my project, but im stuck with this.
I want to display my database depending on what the user select
Here is my html code:
<table>
<tr><td>Select Request Status:</td>
<td><select class="form-control" name="status" style="width:200px;">
<option value="1">Pending Request</option>
<option value ="2">Requests On-Process</option>
<option value="3">Unassigned Requests</option>
<option>All Requests</option>
</select></td>
</tr>
</table>
And here is my php code:
if ($status = $_SESSION['status'] == 'Pending Request')
{
$query = "select * from tblrequest where status='Pending Request'";
$request = mysql_query($query)or die(mysql_error());?>
<table class="table">
<tr>
<th>Request ID</th>
<th>Requestor</th>
<th>Department</th>
<th>Category</th>
<th>Assigned to</th>
<th>Status</th>
<th>Created Date</th>
<th>Created Time</th>
</tr>
<?php while($row = mysql_fetch_array($request)){
?>
<tbody class="table table-hover">
<tr>
<td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
<td style="width:100px;"><?php echo $row['Requestor']; ?></td>
<td style="width:100px;"><?php echo $row['Department']; ?></td>
<td style="width:50px;"><?php echo $row['category']; ?></td>
<td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
<td style="width:100px;"><?php echo $row['status']; ?></td>
<td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
<td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
</tr>
</tbody>
<?php
}
}?>
Im a newbie to php so please help me.
Can you try this,
HTML:
<form method="post">
<table>
<tr><td>Select Request Status:</td>
<td>
<?php $Statuses = array("0"=>"All Requests", "1"=>"Pending Request", "2"=>"Requests On-Process", "3"=>"Unassigned Requests" );?>
<select class="form-control" name="status" style="width:200px;" onchange="this.form.submit();">
<?php foreach($Statuses as $key=>$status):?>
<?php
$selected ="";
$statusPost = $_POST['status'];
if($key == $statusPost): $selected =" selected"; endif;
?>
<option value="<?php echo $key;?>" <?php echo $selected;?>><?php echo $status;?></option>
<?php endforeach;?>
</select></td>
</tr>
</table>
</form>
PHP:
if (isset($_POST['status'])){
$status= $_POST['status'];
$Where ='';
if($status!='0'){
$Where =" where status='".$Statuses[$status]."' ;
}
}
$query = "select * from tblrequest $Where";
$request = mysql_query($query)or die(mysql_error());?>
<table class="table">
<tr>
<th>Request ID</th>
<th>Requestor</th>
<th>Department</th>
<th>Category</th>
<th>Assigned to</th>
<th>Status</th>
<th>Created Date</th>
<th>Created Time</th>
</tr>
<?php while($row = mysql_fetch_array($request)){
?>
<tbody class="table table-hover">
<tr>
<td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
<td style="width:100px;"><?php echo $row['Requestor']; ?></td>
<td style="width:100px;"><?php echo $row['Department']; ?></td>
<td style="width:50px;"><?php echo $row['category']; ?></td>
<td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
<td style="width:100px;"><?php echo $row['status']; ?></td>
<td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
<td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
</tr>
</tbody>
<?php
}
?>
put select dropdown in a form with id,action,method etc.
give id to a select element(say "mySelect")
Now, in jquery,
$("#mySelect").on("change",function(){
if($("#mySelect").val() != ""))
{
$("#form1").submit();
}
})