error on inputing multiple data to database based on session - php

so i create a shopping cart with session based and then it success input to database but the data inputted is wrong.
the incorrect data after i input on database is barang_id & kuantitas.
here's the code :
if (isset($_SESSION['items'])) {
foreach ($_SESSION['items'] as $key => $value) {
$barang_id = $_SESSION['items'][$key];
$kuantitas = $value;
$query_barang = mysqli_query($conn, "SELECT * FROM `barang` WHERE `id` = '$barang_id'");
// ambil data dari data barang
$rs_barang = mysqli_fetch_array($query_barang);
$harga = $rs_barang['harga'];
$jumlah_harga = $harga * $kuantitas;
$total += $jumlah_harga;
mysqli_query($conn, "INSERT INTO `item_penjualan` (`penjualan_id`, `barang_id`, `harga`, `kuantitas`, `jumlah_harga`) VALUES ('$penjualan_id', '$barang_id', '$harga', '$kuantitas', '$jumlah_harga')");
}
}
and here's the cart code :
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="viewer">
<tr>
<th align="left" scope="col">Kode Barang</th>
<th align="left" scope="col">Nama Barang</th>
<th align="right" scope="col">Harga</th>
<th align="right" scope="col">Qty</th>
<th align="right" scope="col">Jumlah Harga</th>
<th align="right" scope="col">Aksi</th>
</tr>
<?php
$total = 0;
mysqli_select_db($conn,$database_conn);
if (isset($_SESSION['items'])) {
foreach ($_SESSION['items'] as $key => $val) {
$query = mysqli_query($conn, "select barang.id, barang.nama, barang.harga from barang where barang.id = '$key'");
$rs = mysqli_fetch_array($query);
$jumlah_harga = $rs['harga'] * $val;
$total += $jumlah_harga;
?>
<tr>
<td><?php echo $rs['id']; ?></td>
<td><?php echo $rs['nama']; ?></td>
<td align="right"><?php echo number_format($rs['harga']); ?></td>
<td align="right"><?php echo number_format($val); ?></td>
<td align="right"><?php echo number_format($jumlah_harga); ?></td>
<td align="right">+ | - | Hapus</td>
</tr>
<?php
mysqli_free_result($query);
}
}
?>
<tr>
<td> </td>
<td> </td>
<td align="right"> </td>
<td align="right"> </td>
<td align="right"><?php echo number_format($total); ?></td>
<td align="right">Clear</td>
</tr>
<tr>
<td colspan="6">
<hr/>
Checkout
</td>
</tr>
</table>

Related

Table inside nested Table

I am having a nested table with a while loop, I want to add one more nested table in the same row:
Now I want to add one more nested table as each cd contains more than one data like below:
My code is as follows
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td><?php echo $rowcd['well_no'] ?></td>
<td><?php echo $rowcd['well_name'] ?></td>
<td>
<table border="1" width="100%">
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
/* I want to add one more nested table here*/
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
I tried some thing like this,after my second while loop
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
<td>
<table>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<tr>
<td><?php echo $rowl['logs'] ?></td>
</tr>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
but the result was messed up. I am confused, where I want to end my while loop, I think.
Finally i got as i wish, and i am sharing the code as below
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div class="container">
<table border="1" align="center" border-collapse="collapse">
<thead>
<tr >
<th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th width="100">CD No:</th>
<th width="150">Logs</th>
<th width="100">Bottom Depth</th>
<th width="100">Top Depth</th>
<th width="100">Date of Log</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td align="center"><?php echo $rowcd['well_no'] ?></td>
<td align="center"><?php echo $rowcd['well_name'] ?></td>
<td colspan="5">
<table rules="all">
<tr>
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<td width="100" align="center"><?php echo $rowcd['cd_no'] ?></td>
<td colspan="4">
<table rules="all">
<tr>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<td width="155"><?php echo $rowl['logs'] ?></td>
<td width="105" align="center"><?php echo $rowl['bottom'] ?></td>
<td width="100" align="center"><?php echo $rowl['top'] ?></td>
<td width="100" align="right"><?php echo $rowl['date'] ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
?>
</table>
</td>
<?php
}
}
?>
</tr>
</table>
I hope this is what you meant as per your data table shown above
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<tr>
<td>id</td>
<td>well</td>
<td>name</td>
<td>
<table border="1" width="100%">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
</table>
</td>
<td>
<table border="1" width="100%">
<tr>
<td>Log1</td>
</tr>
<tr>
<td>Log2</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

While Loop For Table PHP

I'm trying to do this in while loop PHP, number of assignments are unlimited so rowspan should also adjust itself with the number of rows, is there any proper way to do it with minimum numbers of line?
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Assignment No</th>
<th>Student Name</th>
<th>Assignment Marks</th>
<th>Overall Result</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">S1</td>
<td align="center">5</td>
<td rowspan="3" align="center">B Grade</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">S1</td>
<td align="center">8</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">S1</td>
<td align="center">7</td>
</tr>
</tbody>
</table>
Try This:
<? foreach($dataarray as $data)
{?>
<tbody>
<tr>
<td align="center"><? echo $data[0]; ?></td>
<td align="center"><? echo $data[1]; ?></td>
<td align="center"><? echo $data[2]; ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="3" align="center"><? echo $data[3]; ?></td>
<? } ?>
<? } ?>
and put if condition for last column
Assuming that data comes in a linear fashion (ie a plain one dimensional array), and that the grade is not in the array, I'd do something like this:
<?
$sizeofArray = count($data);
$rowspan = floor($sizeofArray/3);
for($arrCnt = 0; $arrCnt < $sizeofArray; $arrCnt +=3)
{?>
<tbody>
<tr>
<td align="center"><?=$data[$arrCnt]; ?></td>
<td align="center"><?=(($arrCnt+1 < $sizeofArray)?$data[$arrCnt+1]:" "); ?></td>
<td align="center"><?=(($arrCnt+2 < $sizeofArray)?$data[$arrCnt+2]:" "); ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="<?=$rowspan?>" align="center"><?=$grade; ?></td>
<? } ?>
<? } ?>
EDIT: added "padding" code, to avoid index out of bounds error
Based on your case, rowspan is different from one student to another. What you would want to do, is group data by student. The number of assignments per student is what will give you rowspan value.
While ... {
$data['student'][] = ['name' => $row -> name]; // all student inf
$data['student][]['assignments'][] = ['id' => $row -> assignement_id];//student assigments
}
Hope this help or put in the right direction to resolve your problem.
use foreach to loop
foreach ($data as $student){
$rowspan = count($student['assignments'];
foreach ($student['assigments'] as $assignment ){
// html table
}
}

Getting the option value without using submit button

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();
}
})

Pulling last years data from a table

Hello I'm trying to update this scholarship application box. However when the year changed to 2013 it only displays scholarship applicant info from 2013. I'd like it to display info from 2012. I tried messing around with the date but I cant seem to figure it out. Any help would be greatly appreciated!
<?php
$appYear = date("Y").'-'.(date("Y")+1);
$sql = 'select * from sApplication where studentID = "'.$database->iPrep($_SESSION['ID']).'" AND appYear = "'.$appYear.'" LIMIT 1';
$appID = Scholarship::iFindSQL($sql);
$total = count($appID);
if ($total > 0)
{
$app = array_shift($appID);
}
else
{
$app = 0;
}
?>
<li id="item-2">
<div id="appStatus">
<h3>Application Status</h3>
<blockquote>
<?php if ($app->submitted == ('0000-00-00') || !isset($app->submitted)) { ?>
<table style="border:1px solid #000;" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="50%"><strong>Scholarship<br /> 2013-2014</strong></td>
<td width="50" align="right"> <a style="font-size:16px;" href="welcome.php? app=Scholar">Apply Now</a></td>
</tr>
<tr>
<td><strong>Date Submitted</strong></td>
<td align="right"> </td>
</tr>
<tr>
<td><strong>References</strong></td>
<td align="right"> </td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td> </td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<tr>
<td><strong>Scholarship 2012-2013</strong></td>
<td> </td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td> </td>
</tr>
</table>
<?php } else { ?>
<table style="border:1px solid #000;" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="90%"><strong>Scholarship 2013-2014</strong></td>
<td width="10%" align="right"> </td>
</tr>
<tr>
<td><strong>Date Submitted</strong></td>
<td align="right"><?=dbOutDate($app->submitted)?></td>
</tr>
<tr>
<td><strong>References</strong> </td>
<td align="right"></td>
</tr>
<tr>
<td colspan="2">
<?php
$refs = Reference::iFindSQL("Select * from reference where appID = '".$app->ID."'");?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php foreach($refs as $ref) { ?>
<tr> <td> <small><?php if($ref->rType == 'Academic Reference'){ echo 'Academic/Artistic/Professional'; } else { echo 'Community Service'; } ?></small></td> <td align="right"><?=$ref->status?></td></tr>
<?php } ?>
</table>
</td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td align="right">
<?php
if ($app->complete == 'Approved') { echo 'Approved'; }
if ($app->complete == 'Declined') { echo 'Declined'; }
if ($app->complete == 'Pending') { echo 'Pending'; }
if ($app->complete == 'Incomplete') { echo 'Incomplete'; }
Remove the WHERE clause that limits the year. Use ORDER BY to sort by year, descending.
$sql = 'select * from sApplication
where studentID = "'.$database->iPrep($_SESSION['ID']).
'" ORDER BY appYear DESC LIMIT 1';

display text with if/else statement if value not found in mysql table?

I'm trying to find a way of displaying an else or if statement in this code so that if the record/value doesn't exist in the mysql table then it will echo a piece of text like 'ask me'.
heres the code:
<?php
$rates_set = get_rates();
while ($rates = mysql_fetch_array($rates_set)) {
?>
<table width="110%" border="0" cellspacing="5" cellpadding="5">
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="col">Rates</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money in</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money Out</th>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost</th>
<td align="center" valign="middle">£<?php echo "{$rates['labour']} "; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['material']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost/th>
<td align="center" valign="middle">£<?php echo "{$rates['money']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['expense']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">Overnight</th>
<td align="center" valign="middle">£<?php echo "{$rates['charges']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['fees']}"; ?></td>
</tr>
</table>
<?php } ?>
hope someone can help. thank you.
Try this,
<?php echo isset($rates['fees'])?"{$rates['fees']}":"ask me"; ?>
You can loop through the values in the $rates array and if empty, assign a default:
$rates_set = get_rates();
while ($rates = mysql_fetch_array($rates_set)) {
// Set defaults
foreach($rates as $key => $value)
{
if(strlen(trim($value)) == 0)
{
// Assign default
$rates[$key] = 'Ask Me';
}
}
?>
There is some tweaking to be done with this method, such as how you determine a value is "empty"...but essentially, this will get the job done.
The other option is to handle it locally within the markup using the ternary operator:
<td align="center" valign="middle">£
<?php isset($rates['money']) ? $rates['money'] : 'Ask Me'; ?>
</td>
<?php
/* Use This */
$rates_set = get_rates();
if (mysql_num_rows($rates_set)>0) {
while ($rates = mysql_fetch_array($rates_set)) {
?>
<table width="110%" border="0" cellspacing="5" cellpadding="5">
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="col">Rates</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money in</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money Out</th>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost</th>
<td align="center" valign="middle">£<?php echo "{$rates['labour']} "; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['material']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost/th>
<td align="center" valign="middle">£<?php echo "{$rates['money']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['expense']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">Overnight</th>
<td align="center" valign="middle">£<?php echo "{$rates['charges']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['fees']}"; ?></td>
</tr>
</table>
<?php }
} else {
echo 'Ask me';
}
?>
You can use this
<?php
$rates_set = get_rates();
if(mysql_num_rows($rates_set)<1) {
echo "Ask me";
}else{
while ($rates = mysql_fetch_array($rates_set)) {
//your code
}
}
<?php echo ( (!empty($rates['labour'])) ? $rates['labour'] : 'Default Text'); ?>
Is that what you are looking for?
Edit:
Alter the top of the loop like this:
while ($rates = mysql_fetch_array($rates_set)) {
if (empty($rates_set)) continue;

Categories