Jump to content

Recommended Posts

Here's my code

 

<?php
           include 'library/config.inc.php';
           $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
           mysql_select_db($dbnamemain);
            
           $query1=mysql_query("SELECT folder_id, fol_name
                                FROM folders
                                JOIN cabinets ON(cabinets.cabinet_id=folders.cabinet_id)
                                WHERE cab_name='Bills'
                                ORDER BY fol_name ASC
                                ") or die(mysql_error());
           $col_width=100/count($row=mysql_fetch_array($query1));
           while($row=mysql_fetch_array($query1)) {
               $query2=mysql_query("SELECT bill_date_due, bill_amount
                                    FROM items
                                    WHERE folder_id=$row[folder_id]
                                    ORDER BY bill_date_due ASC
                                    ") or die(mysql_error());
               $dataset=array();
               while(list($date,$amt)=mysql_fetch_row($query2)) {
                   $t=strtotime($date);
                   $values=array ("year"=>date('Y',$t),
                                  "month"=>date('m',$t),
                                  "amount"=>$amt);
                   $dataset[$values[month]]=$values[amount];
               }
               $filename="images/graphs/$row[folder_id]$values[year].png";
               if($dataset!=NULL) { ignore this code }
               echo "<table width='100%' align='center' border='1'>
                       <tr>";
               while($row=mysql_fetch_array($query1)) {
                   echo "<th width='$col_width%'>$row[fol_name]</th>";
               }
               echo "</tr></table>";
               //else { echo "<i>Not data entered for this bill folder!</i>"; }
               //echo "<p>";
           }
           
           mysql_close($conn);
         ?>

 

Here's my table data:

 

cabinets

cabinet_id

cab_name

2

bills

 

folders

folder_id

fol_name

cabinet_id

2

telephone

2

10

water and electric

2

15

internet

2

16

credit card statements

2

 

items

item_id

item_name

bill_date_due

bill_amount

9

tel01

2008-10-15

100

11

tel02

2008-11-15

120

12

tel03

2008-12-15

200

13

inter01

2008-10-16

50

14

inter02

2008-11-16

50

 

Now with this code and these tables, this is what is getting returned.

 

<table width='100%' align='center' border='1'>

  <tr>

    <th width='25%'>Telephone</th>

    <th width='25%'>Water and Electric</th>

  </tr>

</table>

 

Why isn't it?

 

<table width='100%' align='center' border='1'>

  <tr>

    <th width='25%'>credit card statements</th>

    <th width='25%'>internet</th>

    <th width='25%'>telephone</th>

    <th width='25%'>water and electric</th>

  </tr>

</table>

Link to comment
https://forums.phpfreaks.com/topic/130623-solved-help-with-array-and-output/
Share on other sites

look coments in code

<?php
           include 'library/config.inc.php';
           $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
           mysql_select_db($dbnamemain);
            
           $query1=mysql_query("SELECT folder_id, fol_name
                                FROM folders
                                JOIN cabinets ON(cabinets.cabinet_id=folders.cabinet_id)
                                WHERE cab_name='Bills'
                                ORDER BY fol_name ASC
                                ") or die(mysql_error());
           $col_width=100/count($row=mysql_fetch_array($query1)); // in vaiable $row is 1st row of data (credit card)
           // change to $col_width=100/mysql_num_rows($query1);
           while($row=mysql_fetch_array($query1)) { //  in vaiable $row is 2nd row of data (internet)
               $query2=mysql_query("SELECT bill_date_due, bill_amount
                                    FROM items
                                    WHERE folder_id=$row[folder_id]
                                    ORDER BY bill_date_due ASC
                                    ") or die(mysql_error());
               $dataset=array();
               while(list($date,$amt)=mysql_fetch_row($query2)) {
                   $t=strtotime($date);
                   $values=array ("year"=>date('Y',$t),
                                  "month"=>date('m',$t),
                                  "amount"=>$amt);
                   $dataset[$values[month]]=$values[amount];
               }
               $filename="images/graphs/$row[folder_id]$values[year].png";
               if($dataset!=NULL) { ignore this code }
               echo "<table width='100%' align='center' border='1'>
                       <tr>"; // move this line before 1st while
               while($row=mysql_fetch_array($query1)) {//  in vaiable $row is 3rd row of data (telephone) and goes to end
                // remove this line
                   echo "<th width='$col_width%'>$row[fol_name]</th>";
               } // and remove this too
               echo "</tr></table>"; //move this line after end of while loop
               //else { echo "<i>Not data entered for this bill folder!</i>"; }
               //echo "<p>";
           }
           
           mysql_close($conn);
         ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.