www.webdeveloper.com
Results 1 to 13 of 13

Thread: Problem in using foreach and while loop in displaying data on table format

Threaded View

  1. #1
    Join Date
    Oct 2011
    Posts
    350

    Problem in using foreach and while loop in displaying data on table format

    Hi...

    I tried to use foreach in displaying my table header, but I encountered problem when I tried to display data on the first row , my query only display the last Sum for the last Comp.

    here is my code:
    PHP Code:
    <html>
    <head>
    <title>Half Shell</title>
    <link rel="stylesheet" type="text/css" href="kanban.css" />
    <?php
      error_reporting
    (E_ALL E_NOTICE);
      
    date_default_timezone_set("Asia/Singapore"); //set the time zone  
    $con mysql_connect('localhost''root','');

    if (!
    $con) {
        echo 
    'failed';
        die();
    }

    mysql_select_db("mes"$con);


    ?>

    <body>
    <form name="param" action="" method="post" onSubmit="return false">

    <div id="fieldset_PS">
    <?php
       
    echo "<table>";
       
       
    $sql "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp";
       
    $res_comp mysql_query($sql$con); 
       while(
    $row_comp mysql_fetch_assoc($res_comp)){
            
    $Comp[] = $row_comp['Comp'];
       }
       echo 
    "<th>&nbsp;</th>";
       foreach(
    $Comp AS $Comp){
        echo 
    "<th>$Comp</th>";
      }   

       echo 
    "<tr>
       <td>Total Kg/Compound</td>"
    ;
          
    $sql_sec "SELECT SUM(TotalKg) AS TotalKg FROM sales_order  WHERE Comp = '$Comp' ORDER BY Comp";
       
    $res_sec mysql_query($sql_sec$con);
       
       while(
    $row_sec mysql_fetch_assoc($res_sec)){
           
    $TotalKg[] = $row_sec['TotalKg'];

       }
       foreach(
    $TotalKg AS $TotalKg){
        echo 
    "<td>$TotalKg</td>
        </tr>"
    ;
      }   

       
    ?>
    I also attach the correct output that should be and the result from my code.

    Thank you
    Attached Images Attached Images

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles