Note that there are some explanatory texts on larger screens.

plurals
  1. POGrab multidimensional array value in to table using foreach loop
    primarykey
    data
    text
    <p>This is my code; I will tell you the issue then.</p> <pre><code>&lt;?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(); } ///////////////////////////////////// ?&gt; &lt;?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 &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; if the session bill_array array is not set or cart array is empty &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; if(!isset($_SESSION["bill_array"]) || count($_SESSION["bill_array"]) &lt; 1 ) { // Run if the bill_array is empty or not set $_SESSION["bill_array"]= array(1 =&gt; array("date"=&gt; $date, "billnmbr"=&gt; $billnmbr, "itemcode"=&gt; $itemcode, "itemname"=&gt; $itemname, "exdate"=&gt; $exdate, "eachprice"=&gt; $eachprice, "itmtotal"=&gt; $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" &amp;&amp; $value == $itemcode){ // That item is in cart already so push a error message in to screen $wasFound = true; ?&gt; &lt;script type="text/javascript"&gt; var error = "&lt;?= $wasFound ?&gt;"; if(error == "true") { alert("You trying to add same item twice") } &lt;/script&gt; &lt;?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"=&gt; $date, "billnmbr"=&gt; $billnmbr, "itemcode"=&gt; $itemcode, "itemname"=&gt; $itemname, "exdate"=&gt; $exdate, "eachprice"=&gt; $eachprice, "itmtotal"=&gt; $itmtotal)); }//clos if condition }//close else statment }//close ifisset ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Front-end Billing&lt;/title&gt; &lt;link href="main.css" rel="stylesheet" type="text/css" media="all" /&gt; &lt;style type="text/css"&gt; .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; } &lt;/style&gt;&lt;/head&gt; &lt;body&gt; &lt;div id="wrap"&gt; &lt;div id="headertopic" class="mainheder"&gt;Accoutnig for Phamacy&lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;div id="mainmenue"&gt; &lt;?php // include 'index.html' ;?&gt; &lt;/div&gt; &lt;div id="logdas"&gt;&lt;p class="logdas"&gt;Logged in as : &lt;?php echo $user_name; ?&gt;&lt;/p&gt; &lt;p class="logdas"&gt;Date : &lt;?php echo date("Y-m-d") ?&gt;&lt;/p&gt; &lt;/div&gt; &lt;div id="pagetheam"&gt;Front-end Billing &lt;/div&gt; &lt;div id="billingitems"&gt; &lt;form id="form1" name="form1" method="post" action="biling.php"&gt; &lt;?php $data = mysql_query("SELECT * FROM billnumber ") ; $info = mysql_fetch_array( $data ); $oldnumber = $info['bill_number']; $oldnumber= $oldnumber + 1; $Transaction_number = "PM". $oldnumber ; ?&gt; &lt;table width="490" height="282" border="0"&gt; &lt;tr&gt; &lt;td width="160"&gt;&lt;input type="hidden" name="date" id="date" value=" &lt;?php echo date("Y-m-d") ?&gt; " /&gt;&lt;?php echo date("Y-m-d") ?&gt;&lt;/td&gt; &lt;td width="47"&gt;&amp;nbsp;&lt;/td&gt; &lt;td width="69"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="206"&gt;Bill Number&lt;/td&gt; &lt;td width="160"&gt;&lt;input type="hidden" name="billnmbr" id="billnmbr" value="&lt;?php echo $Transaction_number; ?&gt;" /&gt;&lt;?php echo $Transaction_number; ?&gt;&lt;/td&gt; &lt;td width="47"&gt;&amp;nbsp;&lt;/td&gt; &lt;td width="69"&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Item Code&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="itemcode" id="itemcode" tabindex="1" /&gt;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Item Name&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="itemname" id="itemname" tabindex="2" /&gt;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Expier Date&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="exdate" id="exdate" /&gt;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Item Price&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;Each : &lt;input type="text" name="eachprice" id="eachprice" /&gt;&lt;/td&gt; &lt;td&gt;Total : &lt;input type="text" name="itmtotal" id="itmtotal" /&gt;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&lt;input type="submit" name="adnother" id="adnother" value="add another item" tabindex="5" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="submit" name="submit" id="submit" value="Submit" /&gt;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/div&gt; &lt;div id="printlayout"&gt; &lt;p&gt;&lt;span class="printlayoutshopname"&gt;Yasitha Pharacy&lt;br /&gt; &lt;/span&gt;&lt;span class="billaddrs"&gt;22,Colombathanthiri Mawatha, Ethulkotte, Kotte.&lt;/span&gt;&lt;/p&gt; &lt;table width="211" border="0"&gt; &lt;tr&gt; &lt;td width="103"&gt;&lt;span class="bilnmbr"&gt;Bill No: &lt;?php echo $Transaction_number; ?&gt;&lt;/span&gt;&lt;/td&gt; &lt;td colspan="2" class="bilnmbr"&gt;Date: &lt;?php echo date("Y-m-d") ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?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"]) &lt; 1 ) { $cartOutput = "&lt;h2 align='center'&gt; Bill is still empty &lt;/hd&gt;"; } else { $i = 0; foreach ($_SESSION["bill_array"]as $each_item ): ?&gt; &lt;tr&gt; &lt;td class="bilnmbr"&gt;Item code: &lt;/td&gt; &lt;td colspan="2" class="bilnmbr"&gt;&lt;?php echo $each_item['itemcode']; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="3" class="bilnmbr"&gt;Item Name:&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="3" class="bilnmbr"&gt;&lt;?php echo $each_item['itemname']; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr class="bilnmbr"&gt; &lt;td&gt;Each&lt;/td&gt; &lt;td width="98"&gt;Qty&lt;/td&gt; &lt;td width="98"&gt;Total&lt;/td&gt; &lt;/tr&gt; &lt;tr class="bilnmbr"&gt; &lt;td&gt;&lt;?php echo $each_item['eachprice']; ?&gt;&lt;/td&gt; &lt;td&gt;20&lt;/td&gt; &lt;td&gt;&lt;?php echo $each_item['itmtotal']; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;?php endforeach; ?&gt; &lt;?php } } //////////////////////////////////////// ?&gt; &lt;p&gt; &lt;/p&gt; &lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;?php //closing div for wrapper?&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload