使用foreach循环将多维数组值添加到表中

时间:2012-07-05 09:06:09

标签: php multidimensional-array html-table

这是我的代码;我会告诉你那个问题。

<?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>&nbsp;</p>
<p>&nbsp;</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">&nbsp;</td>
      <td width="69">&nbsp;</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">&nbsp;</td>
      <td width="69">&nbsp;</td>
    </tr>
    <tr>
      <td>Item Code</td>
      <td><input type="text" name="itemcode" id="itemcode" tabindex="1" /></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Item Name</td>
      <td><input type="text" name="itemname" id="itemname" tabindex="2" /></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Expier Date</td>
      <td><input type="text" name="exdate" id="exdate" /></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Item Price</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>Each : 
        <input type="text" name="eachprice" id="eachprice" /></td>
      <td>Total : 
        <input type="text" name="itmtotal" id="itmtotal" /></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</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>&nbsp;</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>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<?php //closing div for wrapper?>
</div> 
</body>
</html>

我使用二维数组插入项目详细信息到会话数组,插入项目后我希望它的详细信息显示在一个单独的表(右侧)中,当我添加项目时,一个项目没有问题它正在完美的地方观看,但当我添加第二个项目时,它会在该表下面而不创建新行。我做了什么错了?

1 个答案:

答案 0 :(得分:0)

我发现了我的错误 我像这样结束了我的foreach循环

</tr>
</table>
  <?php endforeach; 

    }
     }
////////////////////////////////////////

?>

但现在我纠正了这种方式

</tr>
  <?php endforeach; 

    }
     }
////////////////////////////////////////

?>
</table>

现在一切都好......谢谢你们为我的愚蠢错误浪费你的时间......对不起...... :(