views:

60

answers:

1

i work with php/Mysql

i have table with two cols (date , cash) with values like {7/2001:100$, 12/2001:50$ , 1/2002:30$ , 5/2002:90$ , 6/2003:80$,9/2003:20$ } i would like to make cash flow table that have cols (Jan,Feb,Mar,............Dec) and row for every year in the date array and the value of cash in the table cells like blow .

How to display this array {7/2001:100$, 12/2001:50$ , 1/2002:30$ , 5/2002:90$ , 6/2003:80$,9/2003:20$ } as blow ??????

         |Jan  | Feb | Mar | Apr | May | Jun  | Jul  | Aug | Sep  | Oct | Nov | Dec |
--------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2001    |  0  | 0   | 0   | 0   |  0  |  0   | 100$ |  0  |  0   |  0  |  0  |  50$ |
--------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2002    | 30$ | 0   | 0   | 0   | 90$ |  0   | 0    |  0  |  0   |  0  |  0  |  0   |
--------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2003    | 0   | 0   | 0   | 0   |  0  | 80$  |   0  |  0  |  20$ |  0  |  0  |   0  |

ok this the code that retrive rows from database and draw it in a table (date ,cash) like the format of the array

   mysql_connect("localhost","",""); 
   mysql_select_db(""); 
   $var3=$_REQUEST['order_num']; 
   $cashtotal=0; 
   $summat = mysql_query("SELECT * FROM cash_flow WHERE order_num='$var3'"); 
   while ($row = mysql_fetch_assoc($summat)) { 
        print "<tr>"; 
        print "<td>$row[cash_date]</td>"; 
        print "<td>$row[cash]</td>"; 
        print "</tr>"; 
   } 
A: 
$array = array ("7/2001:100$", "12/2001:50$" , "1/2002:30$" , "5/2002:90$" , "6/2003:80$","9/2003:20$" );

    $prefill = array_fill(1,12,0);
    $years_array = array();

    foreach($array as $value){
        list($date , $amount) = explode(":" , $value);
        list($month , $year) = explode("/" , $date);

        if(!$years_array[$year]) $months_data = $prefill;
        else $months_data = $years_array[$year];

        $months_data[$month] = $amount;
        $years_array[$year] =   $months_data;
    }

    echo "| Jan  | Feb | Mar | Apr | May | Jun  | Jul  | Aug | Sep  | Oct | Nov | Dec | <br>";
    ksort($years_array);
    foreach($years_array as $year => $year_row){
        echo $year." | ";
        echo implode(" | " , $year_row);
        echo "<br>";
    }

This should give you the data in the format you desire, ofcourse you will need to introduce a table to make the col widths correct.

Edited: In light of data provided create your array via the DB and pass it on to the above code, probably something like this:

$array = array(); 
   while ($row = mysql_fetch_assoc($summat)) { 
        $array[] = $row[cash_date].":".$row[cash];
   } 

Ofcourse you can improve on the above 2 code blocks , but this should give you the idea.

Sabeen Malik
it works perfect Thanks alot.
ahmed