how to insert php cart data into mysql database - php

I have developed shopping cart in PHP but while inserting cart data into the database I got an error of undefined index. Can anybody please tell me how do I solve it? I have called all the three values using POST method and these all are also present in the database still i am getting this error.Why?
Code:
reviewcart.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "midata";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$code=$_POST['code'];
$quantity=$_POST['quantity'];
$sql = "INSERT INTO order_final (name, code, quantity)
VALUES ('$name', '$code', '$quantity')";
if ($conn->query($sql) === TRUE) {
}
else{
}
}
else{ echo 'query doesnt execute';
}
?>
<div class="container">
<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["code"] == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
?>
<div class="c-layout-sidebar-content ">
<!-- BEGIN: PAGE CONTENT -->
<div class="c-content-title-1 c-margin-t-30">
<h3 class="c-font-uppercase c-font-bold c-center ">Place Order</h3>
<div class="c-content-panel">
<div class="c-body">
<div class="row">
<div class="col-md-12">
<table class="table table-condensed">
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="reviewcart.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>
<table class="col-md-12" cellpadding="10" cellspacing="1" >
<tbody class="col-md-12">
<tr>
<form action="" method="post">
<th class="col-md-3" style="text-align:center;"><strong>Name</strong></th>
<th class="col-md-3" style="text-align:center;"><strong>Code</strong></th>
<th class="col-md-3" style="text-align:center;"><strong>Quantity</strong></th>
<th class="col-md-3" style="text-align:center;"><strong>Price</strong></th>
<th class="col-md-3" style="text-align:center;"><strong>Action</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><strong><?php $name=$_POST['name'] ; echo $item["name"]; ?></strong></td>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><?php $code=$_POST['code'] ; echo $item["code"]; ?></td>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><?php $quantity=$_POST['quantity'] ; echo $item["quantity"]; ?></td>
<!-- <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><?php $price=$_POST['price'] ; echo $price ?></td> -->
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">Remove Item</td>
</tr>
<!-- <?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
-->
<tr>
<td colspan="5" align=right><input type="submit" name="submit" value="submit" /></td>
</tr></form>
</tbody>
</table>
<?php
}
?>
</div>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

If we want to post any data via form mean we must want to use input tag then only we can accept it values on global arrays like $_POST, $_GET, $_FILES.
<tr>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
<strong><?php echo $item["name"]; ?></strong>
<input type="hidden" name="name" value="<?php echo $item['name']?>">
</td>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
<?php echo $item["code"]; ?>
<input type="hidden" name="code" value="<?php echo $item['code']?>">
</td>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
<?php echo $item["quantity"]; ?>
<input type="hidden" name="quantity" value="<?php echo $item['quantity']?>">
</td>
<!-- <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
<?php $price=$_POST['price'] ; echo $price ?>
</td> -->
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
<a href="reviewcart.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">
Remove Item
</a>
</td>
</tr>

Related

PHP table in collapse

I'm having trouble to collapse the next row in table. My code works only for the first row of the table where ever I click the collapse button.
Code :
<table id="yourEvent" class="table table-hover">
<thead>
<tr>
<td style="text-align: center" width="20px"><strong>No.</strong></td>
<td><strong>Attendee Name</strong></td>
</tr>
</thead>
<?php while($row = mysqli_fetch_array($resultAttendeeList))
{ ?>
<tbody>
<tr>
<td class="counterCell" style="text-align: center" width="20px"></td>
<td><button type="button" class="btn btn-xs bg-maroon" style="width: 20px" data-toggle="collapse" data-target="#divCol"><i class="fa fa-plus"></i></button> <?php echo $row['fullname'];?>
<div class="collapse" id="divCol">
<table>
<tr>
<td>Address: <?php echo $row['address']; ?></td>
</tr>
<tr>
<td>Contact: 0<?php echo $row['contact']; ?></td>
</tr>
</table>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
You are using same ID for multiple divs in your loop. You can create incremental int variable and concatenate in your div IDs.
try this code: added $ctr variable increments through loop. Also, you should start your loop after the <tbody> tag.
<tbody>
<?php $ctr=1;
while($row = mysqli_fetch_array($resultAttendeeList))
{ ?>
<tr>
<td class="counterCell" style="text-align: center" width="20px"></td>
<td><button type="button" class="btn btn-xs bg-maroon" style="width: 20px" data-toggle="collapse" data-target="#divCol_<?=$ctr?>"><i class="fa fa-plus"></i></button> <?php echo $row['fullname'];?>
<div class="collapse" id="divCol_<?=$ctr?>">
<table>
<tr>
<td>Address: <?php echo $row['address']; ?></td>
</tr>
<tr>
<td>Contact: 0<?php echo $row['contact']; ?></td>
</tr>
</table>
</div>
</td>
</tr>
<?php $ctr=$ctr+1; } ?>
Please try this is complete code. Please give dynamic id. make sure when you process any jquery/javascript operation, divid should always unique. Otherwise event will not bind for all element.
<?php
$servername = "host";
$username = "user";
$password = "pass";
// Create connection
$conn = mysqli_connect($servername, $username, $password, 'testdb');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM table ORDER BY ID limit 5";
$result = mysqli_query($conn, $sql);
// Numeric array
//$row = mysqli_fetch_array($result);
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<table id="yourEvent" class="table table-hover">
<thead>
<tr>
<td style="text-align: center" width="20px"><strong>No.</strong></td>
<td><strong>Attendee Name</strong></td>
</tr>
</thead>
<tbody>
<?php
$j = 1;
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td class="counterCell" style="text-align: center" width="20px"><?php echo $j; ?></td>
<td>
<button type="button" class="btn btn-xs bg-maroon" style="width: 20px" data-toggle="collapse" data-target="#divCol_<?php echo $j; ?>">
<i class="fa fa-plus"></i>
</button> <?php echo $row['EMP_FULLNAME']; ?>
<div class="collapse" id="divCol_<?php echo $j; ?>">
<table>
<tr>
<td>Address: <?php echo $row['EMP_ADDRESS']; ?></td>
</tr>
<tr>
<td>Contact: 0<?php echo $row['EMP_MOBILE']; ?></td>
</tr>
</table>
</div>
</td>
</tr>
<?php
$j++;
}
?>
</tbody>
</table>
</body>
</html>

How can I change the background color of div if the condition is met?

Here is the code for notification code:
<?php if(isset($notification) && $notification->result() >0 ){?>
<div class="panel-heading pull-right col-md-6"
style="height:140px; margin-top:-140px; background-color:white; overflow-y: scroll;">
<h5><strong>Student with 3 Consecutive Absences</strong></h5>
<table class="table">
<thead>
<th>Course Code-Section</th>
<th>Student Name</th>
<th>View</th>
</thead>
<tbody>
<?php foreach($notification->result() as $notif)
{
if($notif->Total >=3)
{
?>
<tr>
<td><?php echo $notif->Course_Code_Section?</td>
<td><?php echo $notif->Student?></td>
<td>
<form action="<?php echo base_url();?>index.php/attendance/check" method="post" target="_blank">
<input type="hidden" name="CID" value="<?php echo $notif->CID;?>">
<button class="btn btn-xs btn-success"><i class="icon-eye-open"></i> View</button>
<?php echo form_close();?>
</td>
</tr>
</div>
<?php }?>
<?php }?>
</tbody>
</table>
</div>
<?php }?>
Here is the notification view, the default background color is white. Therefore, I want it to make the background color red when the condition met.
Try this:
<?php
$style = '';
if(isset($notification) && $notification->result() > 0 )
{
$style = 'style="color:red"';
// Put your css style property here
}
?>
Html:
<div <?php echo $style ?>>
</div>
You can do as -
<?php
if(your condition)
{
?>
<style>
#your_div
{
background:red !important;
}
</style>
<?php
}
else
{
?>
<style>
#your_div
{
background:white !important;
}
</style>
<?php
}
?>
If your condition is true in PHP you can just this line in your tag:
<tr bgcolor="#FF0000">
I don't know if my html code will appear or not. Please refer this site:
https://www.w3schools.com/tags/att_tr_bgcolor.asp
Please try this code ,i hope this is your condition
<?php
$bcolor ='background-color:white';
if(isset($notification) && $notification->result() >0 ){
$bcolor ='background-color:white';
}?>
<div class="panel-heading pull-right col-md-6"
style="height:140px; margin-top:-140px; <?php echo $bcolor ;?> overflow-y: scroll;">
<h5><strong>Student with 3 Consecutive Absences</strong></h5>
<table class="table">
<thead>
<th>Course Code-Section</th>
<th>Student Name</th>
<th>View</th>
</thead>
<tbody>
<?php foreach($notification->result() as $notif)
{
if($notif->Total >=3)
{
?>
<tr>
<td><?php echo $notif->Course_Code_Section?</td>
<td><?php echo $notif->Student?></td>
<td>
<form action="<?php echo base_url();?>index.php/attendance/check" method="post" target="_blank">
<input type="hidden" name="CID" value="<?php echo $notif->CID;?>">
<button class="btn btn-xs btn-success"><i class="icon-eye-open"></i> View</button>
<?php echo form_close();?>
</td>
</tr>
</div>
<?php }?>
<?php }?>
</tbody>
</table>
</div>
try
<?php
$bg_red = '';
if (condition) {
$bg_red = '<style="background-color: red;">';
}
?>
<tr <php? echo $bg_red; ?>>
<td></td>
<td></td>
<td></td>
</tr>

updating value and displaying the value on existing select option

first,I have table display from database, in each row it has update button.
second, i have a four (4) collapsible select option form.
third, the select option form has value from database. this value is also present in the column on the table above(first).
Now what i want is when i update the value from the row on the table, the collapsible form will open then the value from the table will display on the select option form.
Here's my code. the code below cant open the collapsible form and display the value on the select option form.
displaytable.php
<?php
$output='';
$noresult='';
if (isset($_POST['search']))
{
$globalpid = $_POST['search'];
$query = mysql_query("SELECT * FROM transaction where patientid ='$globalpid' order by trandate asc") or die ("could not search");
$count = mysql_num_rows($query);
$globalpid = mysql_real_escape_string($globalpid);
if($count == '' && $countname==0)
{
?>
<?php require_once 'search.php'; ?>
<div class="container" style="padding-left:30px">
<br><p><font color="#0B3861"><?php echo "There was no search results!"; ?> </font>
</div>
</div>
<?php }else{
?>
<?php
$total = '';
$add=mysql_query("SELECT SUM(tranamount) from transaction where patientid = $globalpid");
while($row=mysql_fetch_array($add))
{
$totalamount=$row['SUM(tranamount)'];
}
require_once 'transactionheadermenu.php';
?>
<div class="container" style="padding-top:0px; padding-left:30px; padding-right:30px;">
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title"><font color="#2B547E"><?php echo "$outputname";?></font></h3></div>
<div class="panel-body">
<?php require_once 'billpatientquery.php';?>
<?php
echo "<table class='table table-hover';>
<tr>
<td style='border:1px solid; color:gray' width='145px' align='center'>Patient ID</td>
<td style='border:1px solid; color:gray' width='145px' align='center'>Date</td>
<td style='border:1px solid; color:gray' width='600px' align='center'>Item / Description</td>
<td style='border:1px solid; color:gray' width='90px' align='center'>Quantity</td>
<td style='border:1px solid; color:gray' width='90px' align='center'>Price</td>
<td style='border:1px solid; color:gray' width='150px' align='center'>Amount</td>
<td style='border:1px solid; color:gray' width='30px' align='center'>Update</td>
<td style='border:1px solid; color:gray' width='30px' align='center'>Delete</td>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "
<tr>
<td style='border:1px solid; color:gray' width='145px' align='center'>".$row['patientid']."</td>
<td style='border:1px solid; color:gray' width='145px' align='center'>".$row['trandate']."</td>
<td style='border:1px solid; color:gray' width='600px'>".$row['trandescription']."</td>
<td style='border:1px solid; color:gray' width='90px' align='center'>".$row['tranquantity']."</td>
<td style='border:1px solid; color:gray' width='90px' align='right'>".number_format($row['tranunitprice'],2)."</td>
<td style='border:1px solid; color:gray' width='150px' align='right'>".number_format($row['tranamount'],2)."</td>
<td style='border:1px solid; color:gray'><a href='updatebillindex.php?update=$row[id]'>update</a></td>
<td style='border:1px solid; color:gray'><button class='btn btn-default btn-sm';><a href='updatebillindex.php?update=$row[0]'>edit
<span class='glyphicon glyphicon-trash'></span></a></button></td>
</tr> ";
}
echo "</table>";
?>
</div>
</div>
</div>
<?php }
} ?>
updatebillindex.php
?php
if (isset($_GET['update']))
//
{
$id = $_GET['update'];
// //$tranid = $_POST['update'];
//$patientid = $_POST['update'];
//$trandate = $_POST['update'];
//$getname = $_POST['update'];
$query = mysql_query("SELECT * FROM transaction where id = '$id'") or die ("could not search");
$count = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
$id = $rows['id'];
$tranid = $rows['tranid'];
$trandate = $rows['trandate'];
$patientid = $rows['patientid'];
$transactiontype = $rows['transactiontype'];
$trandescription = $rows['trandescription'];
$tranquantity = $rows['tranquantity'];
$tranunitprice = $rows['tranunitprice'];
$tranamount =$rows['tranamount'];
$gettrandescription = $rows['trandescription'];
}
}
if (isset($_POST['selectmedicine'])) {
$gettrandescription=$_POST['medicineid'];
}
if (isset($_POST['selectroomquantity'])) {
$tranquantity=$_POST['quantity'];
}
?>
<script type="text/javascript">
$('#collapseone').collapseone({
toggle: true
});
</script>
<option value="<?php echo $trandescription; ?>" <?php if($trandescription==$gettrandescription){ echo "selected";} ?> ><?php echo $gettrandescription; ?></option>
<option value="<?php echo $tranquantity; ?>" <?php if($tranquantity==$tranquantity){ echo "selected";} ?> ><?php echo $tranquantity; ?></option>
<form action="billindex.php" method="POST">
<input type="hidden" name="id" value="<?php echo "$id"; ?>">
<input type="hidden" name="tranid" value="<?php echo "$tranid"; ?>">
<input type="hidden" name="patientid" value="<?php echo "$patientid"; ?>">
<input type="hidden" name="trandate" value="<?php echo "$trandate"; ?>">
<input type="hidden" name="transactiontype" value="<?php echo "$transactiontype"; ?>">
<input type="hidden" name="trandescription" value="<?php echo "$trandescription"; ?>">
<input type="hidden" name="tranquanity" value="<?php echo "$tranquantity"; ?>">
<input type="hidden" name="tranunitprice" value="<?php echo "$tranunitprice"; ?>">
<input type="hidden" name="tranamount" value="<?php echo "$tranamount"; ?>">
<input type="submit" name="update" value="Update">
</form>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingTwo">
<h4 class="panel-title">
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Medicine
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="panel-body">
<form action="transaction.php" method="POST">
<div class="col-md-6">
<div class="panel panel-info">
<div class="panel-heading"><h7>Medicine Type : </h7></div>
<div class="panel-body">
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename">
<option id="0" style="width:100px"></option>
<?php
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicinename'] . '"';
echo ' value="' . $row['medicineid'] . '"';
if($row['medicineid'] == $row['medicinename']) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>';
}
?>
</select>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-info">
<div class="panel-heading panel-height:20px":><h7>Quantity : </h7></div>
<div class="panel-body">
<select name="selectmedicinequantity" class="form-control col-md-4" id="quantityname">
<option id="0" style="width:100px"></option>
<?php
$medicinequantity = mysql_query("SELECT * FROM quantity");
while ($displayquantity = mysql_fetch_array($medicinequantity)) {
$quantityid = $displayquantity['id'];
$quantityname = $displayquantity['quantityname'];
//$quantityprice = $displayquantity['quantityprice'];
?>
<option id=" <?php echo $displayquantity['quantityid']; ?>"><?php if($displayquantity['quantityid'] == $displayquantity['quantityname']) echo 'selected="selected"'; ?><?php echo $displayquantity['quantityname'] ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>

page not displaying correctly

I have the code below. It runs fine until the last point. It seems to just just off and stop displaying the rest of the page after the // Close Main while loop comment and I cannot seem to work out why.
<?php
//show everything
error_reporting(E_ALL);
//using php.ini and ini_set()
ini_set('error_reporting', E_ALL);
require_once('header.tpl');
$my_id = '19';
mysql_connect("localhost","*********","*********") or die (mysql_error());
mysql_select_db("*********") or die (mysql_error());
?>
<!-- Navbar/popouts/notloggedin end -->
<script language="javascript" type="text/javascript">
function toggleChecks(field) {
if (document.myform.toggleAll.checked == true){
for (i = 0; i < field.length; i++) {
field[i].checked = true;
}
} else {
for (i = 0; i < field.length; i++) {
field[i].checked = false;
}
}
}
$(document).ready(function() {
$(".toggle").click(function () {
if ($(this).next().is(":hidden")) {
$(".hiddenDiv").hide();
$(this).next().slideDown("fast");
} else {
$(this).next().hide();
}
});
});
function markAsRead(msgID) {
$.post("markAsRead.php",{ messageid:msgID, ownerid:<?php echo $my_id; ?> } ,function(data) {
$('#subj_line_'+msgID).addClass('msgRead');
// alert(data); // This line was just for testing returned data from the PHP file, it is not required for marking messages as read
});
}
</script>
<style type="text/css">
.hiddenDiv {
display:none
}
#pmFormProcessGif {
display:none
}
.msgDefault {
font-weight:bold;
}
.msgRead {
font-weight:100;
color:#666;
}
</style>
<div class="container">
<div class="row">
<!-- /Welcome text -->
<div class="span12">
<div class="navbar navbar-inverse" style="position: static;">
<div class="navbar-inner">
<div class="container"><a class="btn btn-navbar" data-toggle="collapse" data-target=".navbar-inverse-collapse"><span class="icon-bar"></span> <span class="icon-bar"></span><span class="icon-bar"></span></a>
<div class="nav-collapse collapse navbar-inverse-collapse">
<ul class="nav">
<li>Inbox</li>
<li>Sent</li>
<li>Deleted</li>
<form class="navbar-search pull-left" action="">
<input type="text" id="inbox_search" class="search-query span2" placeholder="Search Emails" />
</form>
</ul>
<ul class="nav pull-right">
<li><i class="icon-file icon-white"></i></li>
<li class="dropdown"> <i class="icon-wrench icon-white"></i>
<ul class="dropdown-menu">
<li>Create New Folder</li>
<li>Message Templates</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<!-- /navbar-inner -->
</div>
</div>
</div>
<div class="row">
<div class="span12">
<table class="fulltable">
<tr>
<td class="fulltable_topbar" colspan="2"></td>
</tr>
<tr>
<td class="fulltable_title"><div></div></td>
</tr>
<tr>
<td class="fulltable_content" style="padding-top:10px"><div>
<!-- START THE PM FORM AND DISPLAY LIST -->
<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table width="94%" border="0" align="center" cellpadding="4">
<tr>
<td width="3%" align="right" valign="bottom"><img src="images/crookedArrow.png" width="16" height="17" alt="Develop PHP Private Messages" /></td>
<td width="97%" valign="top"><input type="submit" name="deleteBtn" class="btn btn-small btn-invese" id="deleteBtn" value="Delete" />
<span id="jsbox" style="display:none"></span></td>
</tr>
</table>
<table width="96%" border="0" align="center" cellpadding="4" style="background-image:url(images/headerStrip.jpg); background-repeat:repeat-x; border: #999 1px solid;">
<tr>
<td width="4%" valign="top"><input name="toggleAll" id="toggleAll" type="checkbox" onclick="toggleChecks(document.myform.cb)" /></td>
<td width="20%" valign="top">From</td>
<td width="58%" valign="top"><span class="style2">Subject</span></td>
<td width="18%" valign="top">Date</td>
</tr>
</table>
<?php
///////////End take away///////////////////////
// SQL to gather their entire PM list
$sql = mysql_query("SELECT * FROM private_messages WHERE to_id='$my_id' AND recipientDelete='0' ORDER BY id DESC LIMIT 100");
while($row = mysql_fetch_array($sql) **or die(mysql_error())**){ <error was the or die!
$date = strftime("%b %d, %Y",strtotime($row['time_sent']));
if($row['opened'] == "0"){
$textWeight = 'msgDefault';
} else {
$textWeight = 'msgRead';
}
$fr_id = $row['from_id'];
// SQL - Collect username for sender inside loop
$ret = mysql_query("SELECT id, username FROM members WHERE id='$fr_id' LIMIT 1");
while($raw = mysql_fetch_array($ret)){ $Sid = $raw['id']; $Sname = $raw['username']; }
?>
<table width="96%" border="0" align="center" cellpadding="4">
<tr>
<td width="4%" valign="top"><input type="checkbox" name="cb<?php echo $row['id']; ?>" id="cb" value="<?php echo $row['id']; ?>" /></td>
<td width="20%" valign="top"><?php echo $Sname; ?></td>
<td width="58%" valign="top"><span class="toggle" style="padding:3px;"> <a class="<?php echo $textWeight; ?>" id="subj_line_<?php echo $row['id']; ?>" style="cursor:pointer;" onclick="markAsRead(<?php echo $row['id']; ?>)"><?php echo stripslashes($row['subject']); ?></a> </span>
<div class="hiddenDiv"> <br />
<?php echo stripslashes(wordwrap(nl2br($row['message']), 54, "\n", true)); ?> <br />
<br />
REPLY<br />
</div></td>
<td width="18%" valign="top"><span style="font-size:10px;"><?php echo $date; ?></span></td>
</tr>
</table>
<hr style="margin-left:20px; margin-right:20px;" />
<?php
}// Close Main while loop
?>
</form>
<!-- END THE PM FORM AND DISPLAY LIST --> </div></td>
</tr>
<tr>
<td class="fulltable_bottom" colspan="2"></td>
</tr>
</table>
</div>
</div>
</body>
</br>
</br>
<?php require_once('footer.tpl'); ?>
why the ; after } (please remove)
} // Close Main while loop
same error here:
if($row['opened'] == "0"){
$textWeight = 'msgDefault';
} else {
$textWeight = 'msgRead';
};
You added some semicolons ; (which are not needed) after both of your loop closin curly braces }
};
$fr_id = $row['from_id'];
Should be
}
$fr_id = $row['from_id'];
And also
};// Close Main while loop
Has to be
}// Close Main while loop
just a heads up. While your bug seems to have been cleanly solved, I would suggest getting an IDE that can detect these types of issues. This way you won't spend the rest of your career looking for semi-colons. This is a link to Aptana, which is a code editor that has a lot of built in error detecting. Basically red lines pop up under lines that contain basic errors, things like a missing ; or ). Good luck dude.
http://www.aptana.com/
$sql = mysql_query("SELECT * FROM private_messages WHERE to_id='$my_id' AND recipientDelete='0' ORDER BY id DESC LIMIT 100");
while($row = mysql_fetch_array($sql) **or die(mysql_error())**){ <error was the or die!
the error is above :)

Grab multidimensional array value in to table using foreach loop

This is my code; I will tell you the issue then.
<?php
//checking for perfect loging
session_start();
$user_name = $_SESSION['username'];
$user_pass = $_SESSION['password'];
require ("connection.php");
if ( $user_name == '' ) {
header('location:home.php');
exit();
}
/////////////////////////////////////
?>
<?php
//collecting posted variables by pressing addanother button
if(isset($_POST['adnother'])) {
$date = $_POST['date'];
$billnmbr = $_POST['billnmbr'];
$itemcode = $_POST['itemcode'];
$itemname = $_POST['itemname'];
$exdate = $_POST['exdate'];
$eachprice = $_POST['eachprice'];
$itmtotal = $_POST['itmtotal'];
$wasFound=false;
$i=0;
// check for >>>>>>>>>>> if the session bill_array array is not set or cart array is empty <<<<<<<<<<<<<<<
if(!isset($_SESSION["bill_array"]) || count($_SESSION["bill_array"]) < 1 ) {
// Run if the bill_array is empty or not set
$_SESSION["bill_array"]= array(1 => array("date"=> $date, "billnmbr"=> $billnmbr, "itemcode"=> $itemcode, "itemname"=> $itemname, "exdate"=> $exdate, "eachprice"=> $eachprice, "itmtotal"=> $itmtotal));
} else {
// Run if the bill has at least one item in it
foreach($_SESSION["bill_array"] as $each_item) {
$i++;
while(list($key,$value)=each($each_item)){
if($key=="itemcode" && $value == $itemcode){
// That item is in cart already so push a error message in to screen
$wasFound = true;
?>
<script type="text/javascript">
var error = "<?= $wasFound ?>";
if(error == "true") {
alert("You trying to add same item twice")
}
</script>
<?php
}//close if condition
}//close while loop
}//close foreach loop
//if the next item is not in the bill and then add it to the bill_array
if($wasFound==false){
array_push($_SESSION["bill_array"],array("date"=> $date, "billnmbr"=> $billnmbr, "itemcode"=> $itemcode, "itemname"=> $itemname, "exdate"=> $exdate, "eachprice"=> $eachprice, "itmtotal"=> $itmtotal));
}//clos if condition
}//close else statment
}//close ifisset
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Front-end Billing</title>
<link href="main.css" rel="stylesheet" type="text/css" media="all" />
<style type="text/css">
.mainheder {
font-size: 36px;
font-weight: bolder;
color: #00C;
text-align: center;
}
#headertopic {
position:absolute;
left:304px;
top:1px;
width:395px;
height:44px;
z-index:1;
background-color: #999900;
}
#mainmenue {
position:absolute;
left:208px;
top:78px;
width:614px;
height:48px;
z-index:10000;
}
#dateandtime {
position:absolute;
left:790px;
top:251px;
width:208px;
height:82px;
z-index:1;
}
#redcross {
position:absolute;
left:377px;
top:321px;
width:247px;
height:239px;
z-index:0;
}
#usrlogin {
position:absolute;
left:4px;
top:216px;
width:265px;
height:184px;
z-index:1;
}
#address {
position:absolute;
left:2px;
top:462px;
width:204px;
height:155px;
z-index:2;
}
#logdas {
position:absolute;
left:10px;
top:92px;
width:197px;
height:58px;
z-index:1;
}
.logdas {
font-weight: bold;
color: #00F;
}
#pagetheam {
position:absolute;
left:332px;
top:49px;
width:341px;
height:24px;
z-index:1;
text-align: center;
font-size: 18px;
font-weight: bolder;
color: #0CF;
text-decoration: underline;
}
#billingitems {
position:absolute;
left:11px;
top:186px;
width:489px;
height:293px;
z-index:1;
}
#printlayout {
position:absolute;
left:531px;
top:186px;
width:211px;
height:322px;
z-index:1;
text-align: center;
}
.printlayoutshopname {
font-size: 14px;
font-weight: bold;
color: #000;
}
.billaddrs {
font-size: 10px;
}
.bilnmbr {
font-size: 10px;
font-weight: bold;
text-align: left;
}
</style></head>
<body>
<div id="wrap">
<div id="headertopic" class="mainheder">Accoutnig for Phamacy</div>
<p> </p>
<p> </p>
<div id="mainmenue"> <?php // include 'index.html' ;?> </div>
<div id="logdas"><p class="logdas">Logged in as : <?php echo $user_name; ?></p>
<p class="logdas">Date : <?php echo date("Y-m-d") ?></p>
</div>
<div id="pagetheam">Front-end Billing </div>
<div id="billingitems">
<form id="form1" name="form1" method="post" action="biling.php">
<?php $data = mysql_query("SELECT * FROM billnumber ") ;
$info = mysql_fetch_array( $data );
$oldnumber = $info['bill_number'];
$oldnumber= $oldnumber + 1;
$Transaction_number = "PM". $oldnumber ;
?>
<table width="490" height="282" border="0">
<tr>
<td width="160"><input type="hidden" name="date" id="date" value=" <?php echo date("Y-m-d") ?> " /><?php echo date("Y-m-d") ?></td>
<td width="47"> </td>
<td width="69"> </td>
</tr>
<tr>
<td width="206">Bill Number</td>
<td width="160"><input type="hidden" name="billnmbr" id="billnmbr" value="<?php echo $Transaction_number; ?>" /><?php echo $Transaction_number; ?></td>
<td width="47"> </td>
<td width="69"> </td>
</tr>
<tr>
<td>Item Code</td>
<td><input type="text" name="itemcode" id="itemcode" tabindex="1" /></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Item Name</td>
<td><input type="text" name="itemname" id="itemname" tabindex="2" /></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Expier Date</td>
<td><input type="text" name="exdate" id="exdate" /></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Item Price</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Each :
<input type="text" name="eachprice" id="eachprice" /></td>
<td>Total :
<input type="text" name="itmtotal" id="itmtotal" /></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="adnother" id="adnother" value="add another item" tabindex="5" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit" /></td>
<td> </td>
</tr>
</table>
</form>
</div>
<div id="printlayout">
<p><span class="printlayoutshopname">Yasitha Pharacy<br />
</span><span class="billaddrs">22,Colombathanthiri Mawatha, Ethulkotte, Kotte.</span></p>
<table width="211" border="0">
<tr>
<td width="103"><span class="bilnmbr">Bill No: <?php echo $Transaction_number; ?></span></td>
<td colspan="2" class="bilnmbr">Date: <?php echo date("Y-m-d") ?></td>
</tr>
<?php
if(isset($_POST['adnother'])) {
//taking items in the bill to variables if the bill_array is not empty.
$cartOutput="";
if(!isset($_SESSION["bill_array"]) || count($_SESSION["bill_array"]) < 1 ) {
$cartOutput = "<h2 align='center'> Bill is still empty </hd>";
}
else {
$i = 0;
foreach ($_SESSION["bill_array"]as $each_item ): ?>
<tr>
<td class="bilnmbr">Item code: </td>
<td colspan="2" class="bilnmbr"><?php echo $each_item['itemcode']; ?></td>
</tr>
<tr>
<td colspan="3" class="bilnmbr">Item Name:</td>
</tr>
<tr>
<td colspan="3" class="bilnmbr"><?php echo $each_item['itemname']; ?></td>
</tr>
<tr class="bilnmbr">
<td>Each</td>
<td width="98">Qty</td>
<td width="98">Total</td>
</tr>
<tr class="bilnmbr">
<td><?php echo $each_item['eachprice']; ?></td>
<td>20</td>
<td><?php echo $each_item['itmtotal']; ?></td>
</tr>
</table>
<?php endforeach; ?>
<?php
}
}
////////////////////////////////////////
?>
<p>
</p>
</div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<?php //closing div for wrapper?>
</div>
</body>
</html>
I used two dimensional array for insert item details to a session array, after inserting a item I want its details to be displayed in a separate table (right hand side) there is no problem with one item, when I add a item it is viewing in the perfect place, but when I add the second item it is going under that table without creating new row. What is the wrong with what I had done?
I found my error
i ended my foreach loop like this
</tr>
</table>
<?php endforeach;
}
}
////////////////////////////////////////
?>
but now i corrected to this way
</tr>
<?php endforeach;
}
}
////////////////////////////////////////
?>
</table>
now it's all ok.... thank you guys for wasting your time for my silly mistake... sorry.... :(

Categories