[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Adding Columns to a Dynamic Table using PHP

I am creating a a dynamic table .  I successfully dynamically generate all but two columns of the table from my first query with no problem.  However, I have reached a curve into the situation I can't figure out.  

I want to add two custom columns at the end of the table.  The first column will come from a second sql statement.  The last column in the table is a simple substraction formula for each row from the latter two columns (i.e col5 - col6 = col8.)  How do I join the data from the second query to the same table as column 6  and add the calculation as column 7 with headings?

1st query
$result = mysql_query("SELECT sampstat as Sampstat, version as Version, variable as Agecat, secondvalue as Sex, total as Total From quotasdetail where ssid = '$ssid' AND Batchnum = '$batch'
               AND market = '$siteid' AND controller = '$controller' order by 'version'", $conn);
if (!$result) {
   echo 'Could not run query: ' . mysql_error();
   exit;
}

// Create table from results

$number_of_fields=mysql_num_fields($result);
    echo "<table  border=1 cellspacing=0 cellpadding=0>";
$flag=0;
     while($row=mysql_fetch_array($result))
{
     if($flag==0)

     for($r=0;$r<$number_of_fields;$r++)
    echo "<td bgcolor=#E8E8E8><b>".mysql_field_name($result,$r)."</td>";
$flag=1;
    echo "<tr>";

     for($r=0;$r<$number_of_fields;$r++)

    echo "<td>&nbsp;".$row[$r]."</td>";
    echo "</tr>";
}
    echo "</table>";


Thanks for your help in advance.

Wayneray

0
wayneray
Asked:
wayneray
1 Solution
 
dukestaTAICommented:
It sounds linke you may want to consider joining the two queries using a JOIN statment.  Here is a link that has example JOIN statements, you probably will want to use the INNER JOIN.

http://dev.mysql.com/doc/refman/5.0/en/join.html

Good luck
0
 
waynerayAuthor Commented:
I agree with you I need to join two tables to get the output I want.  Additionally, I need to add a calculation added to the sql statement to complete my php table.  I am going to reask my question in the database section.  Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now