Fix Table Output for PHP - php

I am trying to make a table in PHP.
There is form where I am taking in Value - Market, Production Date, Sales Date and sending it in POST to Table to get it populated.
At the POST I am calling a set of data from production table from MySQL and populating in the same table.
I am able to populate the table from SQL and Form POST Action but I am not able to align them as per the required output format.
Please help in finding fix for the below Code:
<?php
if(isset($_POST['for_post']))
{
try
{
?>
<div class="card-box" style="padding-left:5px;padding:10px;padding-bottom:50px">
<div class="row">
<div class="col-sm-12">
<?php
if(isset($_POST['for_post_market'])){ $pname = $_POST['for_post_market']; }
if(isset($_POST['for_post_prod_date'])){ $pcat = $_POST['for_post_prod_date']; }
if(isset($_POST['for_post_sale_date'])){ $pprice = $_POST['for_post_sale_date']; }
$query = 'SELECT * FROM product';
$stmt = $DB_con->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[$row['prod_cat_name']][] = $row['prod_name'];
}
?>
<table id="invoices" border="1">
<thead>
<th>Category</th>
<th>Product</th>
<th>Production Date</th>
<th>Sales Date</th>
<th>Market</th>
</thead>
<tbody>
<?php
foreach($result as $id => $invoices) {
echo '<tr>';
echo '<td rowspan='. count($invoices) . '>' . $id . '</td>';
$count = 0;
foreach ($invoices as $invoice) {
if ($count != 0) {
echo '<tr>';
}
echo "<td>$invoice</td>";
echo "<td>".$pcat."</td>";
echo "<td>".$pprice."</td>";
$count++;
}
}
$a=count($pname);
for($i=0;$i<$a;$i++)
{
echo "<td>".$pname[$i]."</td></tr>";
}
echo "</tbody>";
echo "</table>";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
else
{ ?>
</div>
</div>
</div>
<div class="card-box" style="padding-left:5px;padding:10px;padding-bottom:50px">
<div class="row">
<div class="col-sm-12">
<form class="form-inline" method="post">
<div class="form-group m-r-10">
<label for="exampleInputName2">Market : </label>
<div class="input-group">
<select class="selectpicker" multiple data-selected-text-format="count" data-style="btn-white" name="for_post_market[]">
<?php
$stmt = $DB_con->prepare('Select * from location');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['location_name'].'</option>';
}
?>
</select>
<div>
&nbsp
<div class="form-group m-r-10">
<label for="exampleInputEmail2">Production Date : </label>
<div class="input-group">
<input type="text" class="form-control" placeholder="mm/dd/yyyy" id="datepicker-autoclose" name="for_post_prod_date">
<span class="input-group-addon bg-custom b-0 text-white"><i class="icon-calender"></i></span>
</div>
</div>
<div class="form-group m-r-10">
<label for="exampleInputEmail2">Sales Date : </label>
<div class="input-group">
<input type="text" class="form-control" placeholder="mm/dd/yyyy" id="datepicker2-autoclose" name="for_post_sale_date">
<span class="input-group-addon bg-custom b-0 text-white"><i class="icon-calender"></i></span>
</div>
</div>
<button type="submit" class="btn btn-default waves-effect waves-light btn-md" id="for_post" name="for_post">
Submit
</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
The Current Output that I am able to get from this code is :
What I am trying to get is :
Updated Changes Suggested and worked:
<table id="invoices" border="1">
<thead>
<th>Category</th>
<th>Product</th>
<th>Production Date</th>
<th>Sales Date</th>
<th>Market</th>
<th>Input</th>
</thead>
<tbody>
<?php
$a=count($pname);
foreach($result as $id => $invoices) {
echo '<tr>';
echo '<td rowspan='. count($invoices)*$a . '>' . $id . '</td>';
$count = 0;
foreach ($invoices as $invoice) {
if ($count != 0) {
echo '<tr>';
}
echo '<td rowspan='. count($pname) . '>' . $invoice . '</td>';
echo '<td rowspan='. count($pname) . '>' . $pcat . '</td>';
echo '<td rowspan='. count($pname) . '>' . $pprice . '</td>';
for($i=0;$i<$a;$i++)
{
echo '<td>'. $pname[$i] . '</td>';
echo '<td><input type="text" class="form-control"></td></tr>';
}
$count++;
}
}
echo '</tbody>';
echo '</table>';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
else
{ ?>

You have to set Category column rowspan to count($invoices)*count($pname) and set rowspan to Product, Production date and Sales date columns to count($pname)

Related

How to select all option for search to show in the below table?

I am creating the form to select all option to show in the below table, I want to search when I am selected the option show in the table. My part to do is let me to onchange "Transaction Type" then show the below table. How to function can let me to select all option to show in the below table? Hope anyone can give me example or edit in my coding to guide me how to do it. I want the output can follow my select date range and transaction type to show the table.
transaction_history file to show the frontend create the form and pass the data to the backend.
<?php
$system_user_type = $user_type;
$lang = $_COOKIE["Language"];
$new_cur_date = date("d-m-Y", strtotime($cur_date));
require_once("language/lang_transaction_" . $lang . ".php");
?>
<html>
<head>
</head>
<body>
<div class="row">
<div class="col-lg-12">
<div class="box form-group">
<header>
<h5><?php echo $language["LIST_TITLE1"]; ?></h5>
<!-- .toolbar -->
<div class="toolbar">
<nav style="padding: 8px;">
<a href="javascript:;" class="btn btn-default btn-xs collapse-box">
<i class="fa fa-minus"></i>
</a>
</nav>
</div><!-- /.toolbar -->
</header>
<section class="content">
<div class="col-lg-12 form-group" >
<label for="text1" class="form-group control-label col-lg-2"><?php echo $language['type']; ?>:</label>
<div class="col-lg-2">
<select id="select_type" class="form-group form-control required" onchange="show_table();">
<option value="transfer" selected><?php echo $language["transfer"]; ?></option>
<option value="withdraw"><?php echo $language["withdraw"]; ?></option>
<option value="upgrade"><?php echo $language["upgrade"]; ?></option>
<option value="register">Register</option>
<option value="receive"><?php echo $language["receive"]; ?></option>
</select>
</div>
<div class="col-lg-8"></div>
</div>
<div class="col-lg-12 form-group">
<label for="text1" class="form-group control-label col-lg-2">Date Range:</label>
<div class="col-lg-2">
<?php echo custom_period_opt(); ?>
</div>
<label for="text1" class="form-group control-label col-lg-2">Date Created</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="start_date" name="start_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
<label for="text1" class="form-group control-label col-lg-2">To</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="end_date" name="end_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
</div>
<div class="col-lg-12" style="text-align:center; padding-bottom:10px; padding-top:10px;">
<button id="search_button" type="button" class="btn btn-sm btn-primary" onclick="search2_('search', 'bill_table', 'billing');">Search</button>
<button id="clear" type="button" class="btn btn-sm btn-default" onclick="clearData()">Clear</button>
</div>
<div class="" id="table_result">
</div>
</section>
</div>
</div>
</div>
</body>
</html>
<script>
$(function() {
show_table();
$("#select_type").on("change", function() {
show_table();
});
});
function show_table() {
//alert(123);
var select_type = $("#select_type").val();
$.ajax({
url: "?f=transaction_table",
type: "POST",
data: {
select_type: select_type
},
before_send: function() {
show_overLay();
//$('#patient_result').html('');
},
success: function(data) {
hide_overLay('');
//alert(data);
if (data) {
$("#table_result").html("");
$("#table_result").append(data);
//
$('.dataTable').dataTable();
} else {
alert("Please fill in the field.");
}
}
});
}
</script>
<style>
.myClass
{
display: none;
}
</style>
transaction_table file to show the backend function send to frontend page.Below is my coding:
<?php
$select_type = $_POST['select_type'];
if ($select_type == "withdraw") {
echo '<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>';
$i = 1;
$select_transfer = 'SELECT * FROM withdrawal_record bp WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['withdraw_amount'] . '</td>';
echo '</tr>';
}
echo " </tbody>
</table>";
}elseif ($select_type == "transfer") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>To Type</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM transfer_history_bp WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
if($rs_transfer['point_type']=="2"){
$to_type="Register Point";
}elseif($rs_transfer['point_type']=="3"){
$to_type="Entertainment Point";
}elseif($rs_transfer['point_type']=="4"){
$to_type="Business Point";
}
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $to_type . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "upgrade") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM upgrade_point WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "register") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM transfer_history_rp WHERE user_id = ' . $user_id . ' and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" and use_type=1 ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "receive") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM extra_point WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}
?>
Below is my output, this output just can follow what I select the transaction type to onchange to show in the table:
Below is my database information:
If I understand your question correctly, then you need to include these additional parameters in your request as you already to with select_type.
First, get the additional values in theshowTable function, also add them to the request data:
function show_table() {
var select_type = $("#select_type").val();
var start_date = $("#start_date").val();
var end_date = $("#end_date").val();
$.ajax({
url: "?f=transaction_table",
type: "POST",
data: {
select_type: select_type,
start_date: start_date,
end_date: end_date
},
// ...
}
then, receive the values in your PHP file like you already do with select_type:
<?php
$select_type = $_POST['select_type'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
// ...
Finally, you have to adjust your queries accordingly. As you already have and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" in your queries, it's just a matter of replacing the hardcoded dates with the ones you received. Do not forget to sanitize your input dates! If possible use prepared statements.
Note 1: Your script and style tags are outside of the html part of your site. This is not valid. These tags belong into either the head or the body tag.
Note 2: There are whitespaces before your opening html tag. You may wan't to avoid this too, since it can trigger IE into quirks mode. Simply change this
?>
<html>
to this
?><html>

How can I put a form for each row of a table

Here's the thing, I need to update a selected row from a table, so I'm putting a form for each row of it (Every single row has an update button) and when I click update, it doesn't submit, actually, doesn't do anything.
Here's my code, I'll be grateful with the solution.
<div class="table-responsive">
<table class="table table-condensed">
<thead class="">
<tr>
<th>ID</th>
<th>Project</th>
<th>Type</th>
<th>Kick-Off Date</th>
<th>Deadline Date</th>
<th>Current Ninja</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$q = $_GET['q'];
$sql="SELECT pr.project_name, pr.project_type, pr.project_start_date, pr.project_end_date, us.nombre, st.id_status, pr.id_project FROM NT_Projects as pr LEFT JOIN Project_Status as st on st.id_status = pr.id_status LEFT JOIN NT_Clients as cl on cl.id_client = pr.id_client LEFT JOIN usuarios as us on us.username = pr.username WHERE cl.id_client = $q";
$result = mysql_query($sql) or die(mysql_error());
$upt = 1;
while($row = mysql_fetch_array($result)) {
echo '
<div id="update-project">
<form method="post" action="sistema/_actions/updateProject.php" id="res-update-proj-'.$upt.'">';
$kickoff = date('m/d/Y', strtotime($row[2]));
$deadline = date('m/d/Y', strtotime($row[3]));
echo '<tr>';
echo '<td width="95px">
<input class="form-control" name="id_Proj" type="text" value="'.$row[6].'" readonly>
</td>';
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $kickoff . "</td>";
echo "<td>" . $deadline . "</td>";
echo "<td>" . $row[4] . "</td>";
echo '<td width="225px">';
echo '<select class="form-control" name="proj_Status">';
$qStatus = "SELECT * FROM Project_Status;";
$exStatus = mysql_query($qStatus);
while($rStatus = mysql_fetch_array($exStatus))
{
if($row[5] == $rStatus[0])
echo '<option value="'.$rStatus[0].'" selected>'.$rStatus[1].'</option>';
else
echo '<option value="'.$rStatus[0].'">'.$rStatus[1].'</option>';
}
echo '</select>
</td>
<td class="text-center">
<button type="submit" class="btn btn-sm btn-primary btn-UProj" value="res-update-proj-'.$upt.'">Update</button>
<div id="res-update-proj-'.$upt.'" style="width: 100%; margin:0px; padding:0px;"></div>
</td>
</tr>
</form>
</div>';
$upt = $upt + 1;
}
?>
</tbody>
</table>
</div>
That code is being called from another HTML with ajax
You cannot mix table tags with divs and forms according to the web standards. If you do, browser's HTML-parser will mix your tags in unpredictable way.
The only solution I know is to utilize CSS:
<div style="display: table">
<form style="display: table-row">
<div> style="display: table-cell"></div>
</form>
</div>

PHP Shopping Cart Total Price Not Calculating Correctly

Interesting issue here...don't know where the problem is because I am using the code from another site that I have. The first site works perfectly and no problems...but when I converted it over to my new site, the total price amount won't calculate properly.
QUESTION: Am I missing something when I switched over my code? If I put the code below into the while loop it works ok with only ONE item, but not multiple and it doesn't look right if I change the location to this.
<div class="column text-lg">Subtotal: <span class="text-medium">$<?php echo $totalamount; ?></span></div>
Here is some images and the code that I have:
OLD WORKING CODE:
<?php
if ( ! isset($totalamount)) {
$totalamount=0;
}
$totalquantity=0;
if (!session_id()) {
session_start();
}
include ('core/connectdb.php');
$sessid = session_id();
$query = "SELECT * FROM cart WHERE cart_sess = '$sessid'";
$results = mysqli_query($connect, $query) or die (mysql_query());
if(mysqli_num_rows($results)==0)
{
echo '<div id="content" class="float_r"><div align="center"><h3>Your cart is empty.</h3> You can find our items on our product page.</div></div><div class="cleaner"></div>';
}
else
{
?>
<div id="content" class="float_r">
<div align="center"><h1>Shopping Cart</h1></div>
<table border="1" align="center" cellpadding="5">
<tr><td> Item Code</td><td>Quantity</td><td>Item Name</td><td>Price</
td><td>Total Price</td>
<?php
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
extract($row);
echo "<tr><td>";
echo $cart_itemcode;
echo "</td>";
echo "<td><form method=\"POST\" action=\"cart.php?action=change&icode=
$cart_itemcode\"><input type=\"text\" name=\"modified_quantity\" size=\"2\"
value=\"$cart_quantity\">";
echo "</td><td>";
echo $cart_item_name;
echo "</td><td>";
echo '$' . $cart_price . '';
echo "</td><td>";
$totalquantity = $totalquantity + $cart_quantity;
$totalprice = number_format($cart_price * $cart_quantity, 2);
$totalamount=$totalamount + ($cart_price * $cart_quantity);
echo '$' . $totalprice . '';
echo "</td><td>";
echo "<input type=\"submit\" name=\"Submit\" value=\"Change quantity\">
</form></td>";
echo "<td>";
echo "<form method=\"POST\" action=\"cart.php?action=delete&icode=$cart_itemcode\">";
echo "<input type=\"submit\" name=\"Submit\" value=\"Delete Item\"></form>
</td></tr>";
}
echo "<tr><td >Total</td><td>$totalquantity</td><td></td><td></td><td>";
$totalamount = number_format($totalamount, 2);
echo '$' . $totalamount . '';
echo "</td></tr>";
echo "</table><br>";
echo "<div style=\"width:400px; margin:auto;\">You currently have " .
$totalquantity . " product(s) selected in your cart</div> ";
?>
<table border="0" style="margin:auto;">
<tr>
<td><button style="font-family:verdana; font-size:150%;" onclick="goBack()">Go Back</button></td>
<td style="padding: 10px;">
<form method="POST" action="cart.php?action=empty">
<input type="submit" name="Submit" value="Empty Cart"
style="font-family:verdana; font-size:150%;" >
</form>
</td><td>
<?php include('cart_upload.php'); ?>
</td></tr></table>
</div>
<div class="cleaner"></div>
<?php
}
?>
NEW NON-WORKING CODE:
<?php
if ( ! isset($totalamount)) {
$totalamount=0;
}
$totalquantity=0;
if (!session_id()) {
session_start();
}
include ('core/connectdb.php');
$sessid = session_id();
$query = "SELECT * FROM cart WHERE cart_sess = '$sessid'";
$results = mysqli_query($connect, $query) or die (mysql_query());
if(mysqli_num_rows($results)==0)
{
echo '<div"><div align="center"><h3>Your cart is empty.</h3> You can find our items on our product page.</div></div>';
}
else
{
?>
<!-- Page Title-->
<div class="page-title">
<div class="container">
<div class="column">
<h1>Cart</h1>
</div>
<div class="column">
<ul class="breadcrumbs">
<li>Home
</li>
<li class="separator"> </li>
<li>Cart</li>
</ul>
</div>
</div>
</div>
<!-- Page Content-->
<div class="container padding-bottom-3x mb-1">
<!-- Shopping Cart-->
<div class="table-responsive shopping-cart">
<table class="table">
<thead>
<tr>
<th>Product Name</th>
<th class="text-center">Quantity</th>
<th class="text-center">Subtotal</th>
<th class="text-center"><a class="btn btn-sm btn-outline-danger" href="#">Clear Cart</a></th>
</tr>
</thead>
<tdbody>
<?php
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
extract($row);
$cart_price = number_format($cart_price);
echo '<tr>';
echo '<td>';
echo '<div class="product-item"><a class="product-thumb" href="shop-single.php?item=' . $cart_itemcode . ' "><img src="' . $cart_imagename . '" alt="' . $cart_item_name . '"></a>';
echo '<div class="product-info">';
echo '<h4 class="product-title">' . $cart_item_name . '</h4>';
echo '</div>';
echo '</div>';
echo '</td>';
echo '<td class="text-center">';
echo '<div class="count-input">';
echo '$' . $cart_price . ' Each';
echo "<form method=\"POST\" action=\"cart.php?action=change&icode=
$cart_itemcode\"><input type=\"text\" name=\"modified_quantity\" size=\"2\"
value=\"$cart_quantity\"><br\><input type=\"submit\" name=\"Submit\" value=\"Update\">
</form>";
echo '</div>';
echo '</td>';
$totalquantity = $totalquantity + $cart_quantity;
$totalprice = number_format($cart_price * $cart_quantity);
$totalamount= number_format($totalamount + ($cart_price * $cart_quantity));
echo '<td class="text-center text-lg text-medium">$' . $totalprice . '</td>';
echo '<td class="text-center"><a class="remove-from-cart" href="cart.php?action=delete&icode=' . $cart_itemcode . '" data-toggle="tooltip" title="Remove item"><i class="icon-cross"></i></a></td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
</div>
<div class="shopping-cart-footer">
<div class="column text-lg">Subtotal: <span class="text-medium">$<?php echo $totalamount; ?></span></div>
</div>
<div class="shopping-cart-footer">
<div class="column"><a class="btn btn-outline-secondary" onclick="goBack()"><i class="icon-arrow-left"></i> Back to Shopping</a></div>
<div class="column"><a class="btn btn-primary" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-circle-check" data-toast-title="Your cart" data-toast-message="is updated successfully!">Update Cart</a><a class="btn btn-success" href="checkout-address.php">Checkout</a></div>
</div>
</div>
It looks like you're adding together formatted numbers, which is bound to be trouble. Don't do that. Keep your values as raw as possible internally and only format then if and when you display them to the user:
$totalquantity = $totalquantity + $cart_quantity;
$totalprice = $cart_price * $cart_quantity;
$totalamount = $totalamount + ($cart_price * $cart_quantity);
echo '<td class="text-center text-lg text-medium">$' . number_format($totalprice) . '</td>';
Remember, messy code is where bugs hide. Keep things clean, as if you're working in a kitchen. Always, always keep things organized. If you're stuck on a chunk of code and aren't sure why it works, the first thing you should do is clean it up. Then keep cleaning. Sometimes in the process of re-organizing the mistake becomes obvious.
So I found out the problem...at the top of my page I had this code:
if ( ! isset($totalamount)) {
$totalamount=0;
}
APPARENTLY for some reason the code was considering it to have value of 2.
Once I added this down towards the bottom before my while loop:
$totalamount=0;
Like magic it works properly now even with formatted numbers. I wanted to keep the formatted numbers since I didn't want any cents in my prices, just whole dollar amounts.

why multiple data doesn't insert in this code

I have a tbl_employee table and tbl_time table, I want to insert multiple data insert for attendance but when I click submit button it's insert only single data . but where is the problem ,help me to find out this..this is insert code
require './db_connect.php';
class Time extends Db_connect {
protected $link;
public function __construct() {
$this->link = $this->database_connection();
}
public function attendance_insert($data) {
extract($data);
$cur_date = date('Y-m-d');
foreach ($time_attendance as $attn_key => $attn_value) {
if ($attn_value == 'P') {
$SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance)VALUES('$attn_key','$cur_date','P')";
$atten_date=mysqli_query($this->link, $SQL);
} else if ($attn_value == 'A') {
$SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance)VALUES('$attn_key','$cur_date','A')";
$atten_date=mysqli_query($this->link, $SQL);
}
if ($atten_date) {
$massage = "<div class='alert alert-success text-center'><h5>Attendance insert successfully</h5></div>";
return $massage;
} else {
die('Attendance insert query problem' . mysqli_error($this->link));
}
}
}
}
this is html code
<?php
require_once './time.php';
$obj_time = new Time()
$massage = '';
if (isset($_POST['btn'])) {
$massage = $obj_time->attendance_insert($_POST);
}
$employee_view = $obj_employee->employee_all_view();
?>
<div class="container-fluid">
<div class="row">
<center>
<span style="font-size:1.8em;">Attendance form</span>
</center>
</div>
</div>
<hr/>
<?php echo $massage; ?>
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-body panel-default">
<div class="well text-center" style="font-size:15px;">
<strong>Date :</strong>
<?php $current_date = date('Y-M-d');
echo $current_date; ?>
</div>
<form class="form-horizontal" method="post">
<table class="table table-striped table-responsive text-center">
<tr>
<td><b>Serial</b></td>
<td><b>Name</b></td>
<td><b>ID</b></td>
<td><b>Attendance</b></td>
</tr>
<?php
$i = 0;
while ($employee_info = mysqli_fetch_assoc($employee_view)) {
extract($employee_info);
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $employee_first_name . ' ' . $employee_last_name; ?></td>
<td><?php echo $employee_id; ?></td>
<td>
<input type="radio" name="time_attendance[<?php echo $employee_id; ?>]" value="P">P
<input type="radio" name="time_attendance[<?php echo $employee_id; ?>]" value="A">A
</td>
</tr>
<?php } ?>
<tr>
<td colspan="4">
<input type="submit" class="btn btn-primary btn-block" name="btn" value="submit"/>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
This :
foreach($time_attendance as $attn_key => $attn_value){
$SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance)
VALUES('$attn_key','$cur_date','A')";
$atten_date = mysqli_query($this->link, $SQL);
if ($atten_date) {
$massage = "<div class=''><h5>Attendance insert successfully</h5></div>";
return $massage;
}
}
Literally ask to break the foreach with return $massage; if you query succeed... So yes, you'll only have one record.
Put your return outside the foreach.

PHP Beginner Column not found 1054 Unknown column

The goal is to code php files so I have the ability to perform CRUD functions on categories.
I don't know what I'm doing wrong and why I'm getting an this error . Check the image below!
But after changing it i get Column not found: 1054 Unknown column 'category_category_id' in 'field list' on line 43?
Line 43
$q->execute(array($category_category_id,$category_name,$id));
updateCategory.php
<?php
require 'database.php';
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( null==$id ) {
header("Location: index.php");
}
if ( !empty($_POST)) {
// keep track validation errors
$category_nameError = null;
$category_category_idError = null;
// keep track post values
$category_category_id = $_POST['id'];
$category_name = $_POST['category_name'];
// validate input
$valid = true;
if (empty($category_category_id)) {
$category_idError = 'Please enter Category id';
$valid = false;
}
if (empty($category_name)) {
$category_nameError = 'Please enter Category name';
$valid = false;
}
// update data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE category set category_category_id = ?, category_name = ? WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($category_category_id,$category_name,$id));
Database::disconnect();
header("Location: index.php");
}
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM category where id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$category_category_id = $data['id'];
$category_name = $data['category_name'];
Database::disconnect();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="span10 offset1">
<div class="row">
<h3>Update a Category</h3>
</div>
<form class="form-horizontal" action="updateCategory.php?id=<?php echo $id?>" method="post">
<div class="control-group <?php echo !empty($$category_category_idError)?'error':'';?>">
<label class="control-label">Category Id</label>
<div class="controls">
<input name="id" type="text" placeholder="Category Id" value="<?php echo !empty($category_category_id)?$category_category_id:'';?>">
<?php if (!empty($$category_category_id)): ?>
<span class="help-inline"><?php echo $$category_category_id;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($category_nameError)?'error':'';?>">
<label class="control-label">Category Name</label>
<div class="controls">
<input name="category_name" type="text" placeholder="Category Name" value="<?php echo !empty($category_name)?$category_name:'';?>">
<?php if (!empty($category_nameError)): ?>
<span class="help-inline"><?php echo $category_nameError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Update</button>
<a class="btn" href="index.php">Back</a>
</div>
</form>
</div>
</div> <!-- /container -->
</body>
</html>
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<h3>Product Menu</h3>
</div>
<div class="row">
<p>
Create
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Id</th>
<th>CategoryId</th>
<th>Brand</th>
<th>Name</th>
<th>Barcode</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
include_once 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM product ORDER BY id ASC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_id'] . '</td>';
echo '<td>'. $row['brand'] . '</td>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['barcode'] . '</td>';
echo '<td>'. $row['price'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<div class="container">
<div class="row">
<h3>Category Menu</h3>
</div>
<div class="row">
<p>
Create
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Category Id</th>
<th>Category Name</th>
</tr>
</thead>
<?php
include_once 'database.php';
$pdo = Database::disconnect();
$pdo = Database::connect();
$sql = 'SELECT * FROM category ORDER BY id ASC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_name'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="readCategory.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
<!-- <div class="container">
<div class="row">
<h3>PHP CRUD Grid</h3>
</div>
<div class="row">
<p>
Create
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>CategoryId</th>
<th>Catengory Name</th>
</tr>
</thead>
$sql2 = 'SELECT * FROM category ORDER BY id DESC';
foreach ($pdo->query($sql2) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_name'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="readCategory.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
</div>
}
Database::disconnect();
?>
</tbody>
</table> -->
You need to initialize the variable in the code like this
require 'database.php';
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( null==$id ) {
header("Location: index.php");
}
$category_category_id = "";
$category_name = "";
if ( !empty($_POST)) {
// keep track validation errors
$category_nameError = null;
$category_category_idError = null;
// keep track post values
$category_category_id = $_POST['id'];
$category_name = $_POST['category_name'];
// validate input
$valid = true;
if (empty($category_name)) {
$category_nameError = 'Please enter Category name';
$valid = false;
}
if (empty($category_category_id)) {
$category_idError = 'Please enter Category id';
$valid = false;
}
// update data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE category set id = ?, category_name = ? WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($category_category_id,$category_name,$id));
Database::disconnect();
header("Location: index.php");
}
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM category where id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$category_category_id = $data['id'];
$category_name = $data['category_name'];
Database::disconnect();
}
also change the input field name
<input name = "category_name" id="catogortId" type = "text" placeholder = "Category Name" value = "<?php echo !empty($category_name)?$category_name:'';?>">
You don't have put name attribute value in form so put it like :
<input name="category_name" type="text" placeholder="Category Name" name="category_name" value="<?php echo !empty($category_name)?$category_name:'';?>">
You do not have category_name field.
Change:
<input name="category_id" type="text" placeholder="Category Name" value="<?php echo !empty($category_name)?$category_name:'';?>">
To
<input name="category_name" type="text" placeholder="Category Name" value="<?php echo !empty($category_name)?$category_name:'';?>">
You need to change name = "category_id" to name = "category_name" in updateCategory.php file
Change
<input name = "category_id" type = "text" placeholder = "Category Name" value = "<?php echo !empty($category_name)?$category_name:'';?>">
TO
<input name = "category_name" type = "text" placeholder = "Category Name" value = "<?php echo !empty($category_name)?$category_name:'';?>">

Categories