Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Dynamic pivot with mysql data.

$
0
0

I'm struggling to implement the various pivoting methods I have found around the net with my specific query. I am completely new to database interations and almost as new with php. I was able to get some of the tutorials to work (using their database entries) when using the SQL query string in phpMyAdmin but am not sure how to turn that into php that I can execute in my browswer to get the same results.

 

My current code showing my currenty query string:

<?php
$ms = microtime(true);
$conn=odbc_connect('syspro','','');
if (!$conn)
  {exit("Connection Failed: " . odbc_error());}
$sql=
    "SELECT SysproCompanyJ.dbo.SorMaster.SalesOrder,
    SysproCompanyJ.dbo.SorMaster.OrderStatus,
    SysproCompanyJ.dbo.SorMaster.Branch,
    SysproCompanyJ.dbo.SorMaster.ReqShipDate,
    SysproCompanyJ.dbo.SorDetail.MStockCode,
    SysproCompanyJ.dbo.SorDetail.MBackOrderQty
    
    FROM SysproCompanyJ.dbo.SorMaster,
         SysproCompanyJ.dbo.SorDetail
    
    WHERE    SysproCompanyJ.dbo.SorMaster.SalesOrder = SysproCompanyJ.dbo.SorDetail.SalesOrder
        AND SysproCompanyJ.dbo.SorDetail.MStockCode LIKE 'CB%'
        AND SysproCompanyJ.dbo.SorMaster.CustomerPoNumber NOT LIKE 'RMA_'
        AND SysproCompanyJ.dbo.SorDetail.MBackOrderQty > '0'    
        AND (SysproCompanyJ.dbo.SorMaster.Branch = '60'
            OR SysproCompanyJ.dbo.SorMaster.Branch = '70')
        AND (SysproCompanyJ.dbo.SorMaster.OrderStatus = '1'
            OR SysproCompanyJ.dbo.SorMaster.OrderStatus = 'S'
            OR SysproCompanyJ.dbo.SorMaster.OrderStatus = 'F')        
        ";
    
$rs=odbc_exec($conn,$sql);
if (!$rs)
  {exit("Error in SQL");}

echo "<table class='sortable' border='1'>
<tr>
<th>Stock Code</th>
<th>Back Order Qty</th>
<th>Scheduled Ship Date</th>
</tr>";

while (odbc_fetch_row($rs))
  {
  $MStockCode=odbc_result($rs,"MStockCode");
  $MBackOrderQty=odbc_result($rs,"MBackOrderQty");
  $ReqShipDate=odbc_result($rs,"ReqShipDate");
    
  echo "<tr><td><center>$MStockCode</center></td>";
  echo "<td><center>".number_format($MBackOrderQty, 0)."</center></td>";
  echo "<td><center>".substr($ReqShipDate,0,10)."</center></td></tr>";

  /*
  echo "<tr><td><center>".odbc_result($rs,'SalesOrder')."</center></td>";
  echo "<td><center>".odbc_result($rs,'MStockCode')."</center></td>";
  echo "<td><center>".odbc_result($rs,'MStockDes')."</center></td>";
  echo "<td><center>".odbc_result($rs,'MBackOrderQty')."</center></td>";
  echo "<td><center>".substr(odbc_result($rs,"OrderDate"),0,10)."</center></td>";
  echo "<td><center>".substr(odbc_result($rs,"ReqShipDate"),0,10)."</center></td></tr>";
*/  

  }

odbc_close($conn);
echo "</table>";
$ms = microtime(true) - $ms;
echo $ms.' secs'; //seconds
echo "<br>";
echo ($ms * 1000).' millisecs'; //millseconds
echo "<br>";
echo "Memory Usage: " . (memory_get_usage()/1048576) . " MB \n";
?>

What I'm attempting to do is dynamicly pull the ReqShipDate for the column headings and then sum MBackOrderQty for each MStockCode at each change in ReqShipDate.

 

 

Example of current output:

+------------+----------------+---------------------+
| Stock Code | Back Order Qty | Scheduled Ship Date |
+------------+----------------+---------------------+
|   C-100    |       5        |       01/16/14      |
|   C-200    |       1        |       01/16/14      |
|   C-300    |       3        |       01/16/14      |
|   C-100    |       6        |       01/16/14      |
|   C-200    |       2        |       01/16/14      |
|   C-500    |       4        |       01/17/14      |
|   C-100    |       1        |       01/17/14      |
|   C-100    |       5        |       01/17/14      |
|   C-200    |       1        |       01/20/14      |
|   C-300    |       3        |       01/20/14      |
|   C-100    |       6        |       01/20/14      |
+------------+----------------+---------------------+

The desired output would be something like:

+-------+----------+----------+----------+
|       | 01/16/14 | 01/17/14 | 01/20/14 |
+-------+----------+----------+----------+
| C-100 |    11    |     6    |     6    |
| C-200 |     3    |          |     1    |
| C-300 |     3    |          |     3    |
| C-500 |          |     4    |          |
+-------+----------+----------+----------+

Any help is greatly appreciated or some more in depth tutotrials on the subject that implement it in php so I can view the full source is also a big help.


Viewing all articles
Browse latest Browse all 13200

Trending Articles