i have a two dimensional array, and i want to retrieve its value in to separate variables.
i’m using these codes, but it gives me a error saying
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, array given in
this is the code i’m using to assign values to a multidimensional session.
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;
//if the bill session variable is not set or cart array is empty
if(!isset($_SESSION["bill_array"]) || count($_SESSION["bill_array"]) < 1 ) {
// Run if the bill 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($wasFound==false){
array_push($_SESSION["bill_array"],array("date"=> $date, "billnmbr"=> $billnmbr, "itemcode"=> $itemcode, "itemname"=> $itemname, "exdate"=> $exdate, "eachprice"=> $eachprice, "itmtotal"=> $itmtotal));
}
}
and after assigning i’m trying to retrieve those date with these codes
<?php
$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 ) {
$i++;
echo $cartOutput = "<h2> Bill item $i </h2>";
while($row=mysql_fetch_assoc($each_item)){
$date1=$row["date"];
$billnmbr1=$row["billnmbr"];
$itemcode1=$row["itemcode"];
$itemname1=$row["itemname"];
$exdate1=$row["exdate"];
$eachprice1=$row["eachprice"];
$itmtotal1=$row["itmtotal"];
}
}
echo $date1;
echo $billnmbr1;
echo $itemcode1;
echo $itemname1;
echo $exdate1;
echo $eachprice1;
echo $itmtotal1;
}
?>
i can’t figure out where the error is. can anyone help me please
You are trying to loop an array as if it were a database resultset. You actually don’t need a loop here, you can just access the values directly, by key.
For example, try this: