Add input form inside a table - php

So inside my table before tbody tag, I inserted a form inside tbody. However while submitting its form, the url is getting btnchange.php?qty=1 instead of btnchange.php?prod_id=cartno.
shopcart.php
<?php // start
$stmt = $DB_con->prepare("SELECT * FROM cart WHERE cartuser = '$userid' and cartpend='IN CART'");
$stmt->execute();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row); //end ?>
<form method="get" action="btnchange.php?prod_id=<?php echo $row['cartno'] ?>">
<tbody>
<tr>
<td><?php echo $row['cartno']; ?></td>
<td><?php echo $row['cartname']; ?></td>
<td><?php echo $row['cartprice']; ?></td>
<td><input type="text" name="qty" value="<?php echo $row['cartqty']; ?>"></td>
<td><?php echo $row['cartsub']; ?></td>
<td><span class="label label-primary"><?php echo $row['cartpend']; ?></span></td>
<td><button type="submit" name="submit">Submit Quantity</button></b>
<td><a class="btn btn-danger btn-xs" href="?delete_id=<?php echo $row['cartno']; ?>" title="click for delete" onclick="return confirm('Sure to Delete?')"><i class="fa fa-trash-o"></i> Remove</a></td>
</tr>
</tbody>
</form>
This is my btnchange.php code, this is what i used to get the prod_id code and the qty input from shopcart.php.
btnchange.php
//get prod id
if(isset($_GET['prod_id']) && !empty($_GET['prod_id']))
{
$prod_code = $_GET['prod_id'];
$newqty = $_GET['qty'];

You can't put query parameters in the action attribute. You need to use a hidden input instead:
<?php // start
$stmt = $DB_con->prepare("SELECT * FROM cart WHERE cartuser = '$userid' and cartpend='IN CART'");
$stmt->execute();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row); //end ?>
<form method="get" action="btnchange.php">
<input type="hidden" name="prod_id" value="<?php echo $row['cartno']; ?>">
<tbody>
<tr>
<td><?php echo $row['cartno']; ?></td>
<td><?php echo $row['cartname']; ?></td>
<td><?php echo $row['cartprice']; ?></td>
<td><input type="text" name="qty" value="<?php echo $row['cartqty']; ?>"></td>
<td><?php echo $row['cartsub']; ?></td>
<td><span class="label label-primary"><?php echo $row['cartpend']; ?></span></td>
<td><button type="submit" name="submit">Submit Quantity</button></b>
<td><a class="btn btn-danger btn-xs" href="?delete_id=<?php echo $row['cartno']; ?>" title="click for delete" onclick="return confirm('Sure to Delete?')"><i class="fa fa-trash-o"></i> Remove</a></td>
</tr>
</tbody>
</form>

Related

How to show image in table using PHP

I tried the following code to show my SQL database data in a table in my dashboard
<tbody>
<?php
while ($row = $result->fetch_assoc()):?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><img src="<?php echo $row['image']; ?>" width=100px height=100px></td>;
<td><?php echo $row['class']; ?></td>
<td><?php echo $row['reg_no']; ?></td>
<td><?php echo $row['capacity']; ?></td>
<td><?php echo $row['oname']; ?></td>
<td><?php echo $row['province']; ?></td>
<td><?php echo $row['district']; ?></td>
<td><?php echo $row['contact']; ?></td>
<td><a href="#" class="btn btn-warning btn-circle editbtn">
<i class="fas fa-edit"></i></a></td>
<td><a href="#" class="btn btn-info btn-circle viewbtn">
<i class="fas fa-eye"></i></a></td>
<td><a href="#" class="btn btn-danger btn-circle deletebtn">
<i class="fas fa-trash"></i></a></td>
</tr>
<?php endwhile; ?>
</tbody>
But that image column image does not show has an image
Result show like this:
my result
How to solve that problem
To me it seems your are inserting to the src attribute the context of the file rather then the URL of the image.
you are saving the image content it self in the DB,
check this out :
https://www.codexworld.com/upload-store-image-file-in-database-using-php-mysql/

Ajax table doesn't show anything

I'm creating a table using ajax and php but there's one problem, the table isn't showing in my div. I'm really new to ajax so I don't really fully understand it yet.
Here's my div:
<div class="body" id="live-data">
</div>
Here's the ajax code:
$(document).ready( function() {
function fetch_data() {
$.ajax({
url:"fetch.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
});
And here's fetch.php:
<?php
include('../global/db.php');
$output = '';
$sql ="SELECT * FROM students WHERE status = '0' AND stud_grade = '$level_id' ORDER BY date_enrolled DESC";
$result = mysqli_query($db, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th width="135" class="noExport">Action</th>
<th width="90">LRN</th>
<th width="20">Level</th>
<th>Name</th>
<th width="20">Gender</th>
<th width="60">Type</th>
<th width="105" style="font-size: 14px!important;">Date Enrolled</th>
</tr>
</thead>
<tbody>';
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
$output .= '
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
';
}
}
else {
$output .= '
<tr>
<td colspan="12">Data not Found</td>
</tr>';
}
$output .= '
</tbody>
</table>
</div>';
echo $output;
?>
It would great if anyone could help because I just don't know why it doesn't work
Edit:
I've changed the code so it returns the data in the console tab and here's what shows up:
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th width="135" class="noExport">Action</th>
<th width="90">LRN</th>
<th width="20">Level</th>
<th>Name</th>
<th width="20">Gender</th>
<th width="60">Type</th>
<th width="105" style="font-size: 14px!important;">Date Enrolled</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
</tbody>
</table>
</div>
So it clearly returns the correct data but it just doesn't show up in the live-data div
Maybe take a look in your html where is your div. Your div id says "live-data" and in the ajax code you mentioned to fetch data for div id "#live_data" instead of "#live-data".
Maybe changing them for same name can solve your problem. I would use for thr div id and in the ajax same id names like "#liveData".
Example (same code, just edited to the right IDs, compare with your original):
Your HTML div
<div class="body" id="liveData">
</div>
Your ajax code
$(document).ready( function() {
function fetch_data() {
$.ajax({
url:"fetch.php",
method:"POST",
success:function(data){
$('#liveData').html(data);
}
});
}
fetch_data();
});
Just append the correct Id of the div tag in sucess function

codeigniter model function result array with WHERE clause filtering

I already have a working function in my model
//GETTING TOTAL PURCHASE
function total_portal_user_sale($user_id)
{
$return = 0;
$transection = $this->db->get('transection')->result_array();
foreach ($transection as $row) {
if ($row['buyer'] == $user_id) {
$return += $row['credit'];
}
}
return $return;
}
i am getting data from my database correctly but i need to filter result by where clause for example this query
$this->db->query("SELECT sum(credit) as total FROM transection
where buyer = $user_id and status like '%paid%'");
this is the same scenario but with data filtering, I don't know how to use my query with active record statement
if someone rewrite my
function total_portal_user_sale($user_id)
with my WHERE clause query i will appreciate it ...
this is how i am rendering data into tables in view file
<div class="panel-body" id="demo_s">
<table id="demo-table" class="table table-striped" data-pagination="true" data-show-refresh="false" data-ignorecol="0,6" data-show-toggle="false" data-show-columns="false" data-search="true" data-striped="true" data-filter-control="true" data-show-export="true" >
<thead>
<tr>
<th style="width:4ex"><input type="checkbox" id="users_idtog"/></th>
<th><?php echo translate('no');?></th>
<th><?php echo translate('image');?></th>
<th><?php echo translate('name');?></th>
<th><?php echo translate('email');?></th>
<th><?php echo translate('phone');?></th>
<th>Total<br>Sale</th>
<th>This<br>Month<br>Sale</th>
<th>Total Debit</th>
<th>Total<br>Profit</th>
<th><?php echo translate('creation');?></th>
<th>Monthly<br>Sale<br>Target</th>
<th>Coupon<br>Code</th>
<th>Coupon<br>Expiry<br>Date</th>
<th class="text-right"><?php echo translate('options');?></th>
</tr>
</thead>
<tbody >
<?php
$i = 0;
foreach($all_users as $row){
$i++;
?>
<tr>
<td><input type="checkbox" class="users_id" name="users_id[]" value="<?php echo $row['user_id']; ?>" /></td>
<td><?php echo $i; ?></td>
<td>
<img class="img-sm img-circle img-border"
<?php if(file_exists('uploads/user_image/user_'.$row['user_id'].'.jpg')){ ?>
src="<?php echo base_url(); ?>uploads/user_image/user_<?php echo $row['user_id']; ?>.jpg"
<?php } else if($row['fb_id'] !== ''){ ?>
src="https://graph.facebook.com/<?php echo $row['fb_id']; ?>/picture?type=large" data-im='fb'
<?php } else if($row['g_id'] !== ''){ ?>
src="<?php echo $row['g_photo']; ?>"
<?php } else { ?>
src="<?php echo base_url(); ?>uploads/user_image/default.png"
<?php } ?> />
</td>
<td><?php echo $row['username']; ?></td>
<td><a href="mailto:<?php echo $row['email']; ?>" target="_self" ><?php echo $row['email']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td class="text-right"><?php echo $this->crud_model->total_purchase($row['user_id']); ?></td>
<td class="text-right">
<?php
$Days = explode("-",date("d-m-Y"));
echo round($this->crud_model->sale_target($Days[0],$row['user_id']),2);
?>
</td>
<td class="text-right"><?php echo $this->crud_model->total_profit($row['user_id']); ?></td>
<?php
$ts = $this->crud_model->total_portal_user_sale($row['user_id']);
$tc = $this->crud_model->total_portal_user_profit($row['user_id']);
$tp = $ts - $tc
?>
<td class="text-right">
<?php echo $ts; ?>
</td>
<td class="text-right"><?php echo date('d M,Y',$row['creation_date']);?></td>
<td><?php echo $row['monthly_sale_target']; ?></td>
<td><?php echo $row['coupon_code']; ?></td>
<td><?php echo $row['expiry_date']; ?></td>
<td class="text-right">
<a class="btn btn-purple btn-xs btn-labeled fa fa-tag" data-toggle="tooltip"
onclick="ajax_modal('add_discount','<?php echo translate('give_target_discount'); ?>','<?php echo translate('adding_discount!'); ?>','add_discount','<?php echo $row['user_id']; ?>')" data-original-title="Edit" data-container="body">
<?php echo translate('give_discount');?>
</a>
<a class="btn btn-success btn-xs btn-labeled fa fa-wrench" data-toggle="tooltip"
onclick="ajax_modal('edit','<?php echo translate('edit_user'); ?>','<?php echo translate('successfully_edited!'); ?>','user_edit','<?php echo $row['user_id']; ?>')"
data-original-title="Edit" data-container="body">
<?php echo translate('edit');?>
</a>
<a onclick="delete_confirm('<?php echo $row['user_id']; ?>','<?php echo translate('really_want_to_delete_this?'); ?>')" class="btn btn-xs btn-danger btn-labeled fa fa-trash" data-toggle="tooltip"
data-original-title="Delete" data-container="body">
<?php echo translate('delete');?>
</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Hope this will help you :
function total_portal_user_sale($user_id)
{
$return = 0;
$this->db->select('*');
//$this->db->select('sum(credit) as total,buyer');
$this->db->from('transection');
$this->db->where('buyer',$user_id);
$this->db->like('status','paid');
$query = $this->db->get();
if ($query->num_rows() > 0 )
{
foreach ($query->result_array() as $row)
{
if ($row['buyer'] == $user_id)
{
$return += $row['credit'];
}
}
return $return;
}
}
You can also use $this->db->select_max('credit') for the same
For more : https://www.codeigniter.com/user_guide/database/query_builder.html

Select a table row from an action button in PHP

i presently have a table which is auto populated from my database, i have three action buttons connected to each row which are edit, delete, and view. Am presently using session to send my key value to the next page, so that i can use that key value to load that particular row's data. the problem am having is that my session is picking the unique id of the last row instead of the row that i selected. here is my code
<?php
$sql = "SELECT * FROM houses";
$q=$conn->query($sql);
while ($row = mysqli_fetch_array($q)) {
?>
<tr>
<td><?php echo $row['nickname']; ?></td>
<td><?php echo $row['state']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['house_type']; ?></td>
<td><?php echo $row['owner']; ?></td>
<td><?php echo $row['price']; ?></td>
<td>
<div class="btn-group">
<?php
$_SESSION['house'] = $row['nickname'];
echo $_SESSION['house'];
?>
<a class="btn btn-primary" href="edit_property.php"><i class="icon_plus_alt2"></i></a>
<a class="btn btn-success" href="view_property.php"><i class="icon_check_alt2"></i></a>
<a class="btn btn-danger" href="delete_property.php"><i class="icon_close_alt2"></i></a>
</div>
</td>
</tr>
<?php
}
?>
That's because you don't want to use sessions in this case, the browser must give you back the ID that you want to view or edit.
Change it like this:
<div class="btn-group">
<a class="btn btn-primary" href="edit_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_plus_alt2"></i></a>
<a class="btn btn-success" href="view_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_check_alt2"></i></a>
<a class="btn btn-danger" href="delete_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_close_alt2"></i></a>
</div>
</td>
Then you will find in $_GET['house'] the key to use, not in $_SESSION

Displaying the sum just in one row

I hav this sql query:
$sql = "SELECT (SELECT SUM(total_pay) FROM workers) total,workers. * FROM workers WHERE projects_id = ".$id;
And I want to display data into table rows:
$stmt = mysqli_query($con, $sql) or die($sql."<br/><br/>".mysqli_errno($con));
<?php while($rows = mysqli_fetch_array($stmt)){ ?>
<form action="update_del.php" method="post">
<th>Payments</th>
<th>Date</th>
<th>Project</th>
<th width="25%">Delete</th>
<tr>
<?php while($rows = mysqli_fetch_array($stmt)){ ?>
<tr>
<td align="center"><?php echo $rows['total_pay']?></td>
<td align="center"><?php echo $rows['date_of_pay']?></td>
<td align="center"><?php echo $name['project_name'] ?></td>
<td align="center"><!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
<input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
</td>
</tr>
</tr>
<tr>
<td colspan="3">Total <?php echo $name['project_name']?></td>
<td><?php echo $rows['total'] ?></td>
</tr>
<?php } ?>
</table>
The problem is, that the sum of the payment is repeated with every row displayed, so how can I display the sum query just once in the end of the table ? Should put it in a single query specified for the sum only ?
You can either remove the total from your query and use PHP to calculate, or you can just simply store the total and use it outside your while statement. Something like
<?php
$total = 0;
while($rows = mysqli_fetch_array($stmt)){
$total = $rows['total'];
?>
<tr>
<td align="center"><?php echo $rows['total_pay']; ?></td>
<td align="center"><?php echo $rows['date_of_pay']; ?></td>
<td align="center"><?php echo $name['project_name']; ?></td>
<td align="center">
<!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
<input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="3">Total <?php echo $name['project_name']; ?></td>
<td><?php echo $total; ?></td>
</tr>
</table>
Though since you're traversing all the results anyway it would be best to remove it from the SQL and calculate it.
$sql = "SELECT workers.* FROM workers WHERE projects_id = ".$id;
<?php
$total = 0;
while($rows = mysqli_fetch_array($stmt)){
$total += $rows['total_pay'];
?>
<tr>
<td align="center"><?php echo $rows['total_pay']; ?></td>
<td align="center"><?php echo $rows['date_of_pay']; ?></td>
<td align="center"><?php echo $name['project_name']; ?></td>
<td align="center">
<!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
<input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="3">Total <?php echo $name['project_name']; ?></td>
<td><?php echo $total; ?></td>
</tr>
If i understand your problem properly, then this might help you to generate sum in the last rows as you require.
"SELECT total_pay, date_of_pay,project_name
FROM workers WHERE project_id=".$id."
UNION
SELECT 'ALL', SUM(total_pay)
FROM workers";

Categories