Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help getting array results to print

Status
Not open for further replies.

canajun

Programmer
Sep 28, 2002
57
CA
I have tried this to get array results to print, but not having much luck..

$arr_tot = array();

if($units=='all')

$query = "SELECT e_group, sum(amount * quantity) as total FROM expense where e_date between '$arr1_date'".
" and '$dep1_date' GROUP BY e_group";
else
$query = "SELECT e_group, sum(amount * quantity) as total FROM expense where e_date between '$arr1_date'".
" and '$dep1_date' GROUP BY e_group";
$result=mysql_query($query,$db_connection);
while($row=mysql_fetch_assoc($result));
$arr_tot[$row['e_group']] = $row['total'];

mysql_free_result($result);
$total = $row['e_group'];




print "<br><br><table align='center' width='100%' cellspacing='1'
cellpadding='5' bgcolor='#ffffff' class='main'>";
print"<tr><td valign='top' colspan='4'>
<p align='left' class='titlefont'><b>
Invoiced Sales</b></p></td></tr>";
print "<table align='center' width='98%' cellspacing='1'
cellpadding='5' bgcolor='#000000' class='main'>
<tr bgcolor='#D0CBA5'>
<td><b>Deposit</b></td>
<td> <b>Fish</b> </td>
<td> <b>Misc</b> </td>
<td> <b>Snacks</b> </td>
<td> <b>Groceries</b> </td>
<td> <b>Cabin</b> </td>
<td> <b>Campsite</b> </td>
<td> <b>Rentals</b> </td>
<td> <b>GST</b> </td>
<td> <b>PST</b> </td>
<td> <b>HMT</b> </td>
<td> <b>Sales</b> </td>
</tr>";

print "<tr bgcolor='#ffffff'>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>".
"<td>". sprintf("%.2f",$total) . "</td>";
print "</table>";

I am totally lost.. any help would be appreciated. Trying my query in phpAdmin does produce the results I want.. just can't get them to print in the table.
 
Hi,
It's a little confusing without seeing the table...
You did seem to have a few parsing errors. Make sure that with your if statements that you place { and } to open and close statements. Same with your while statements. Then all the info you need printed from the dbase should go between the while {}. Hope it helps!!!

Code:
$arr_tot = array();

if($units=='all') {

$query = "SELECT e_group, sum(amount * quantity) as total FROM expense where e_date between '$arr1_date'".
  " and '$dep1_date' GROUP BY e_group";
}else{
$query = "SELECT e_group, sum(amount * quantity) as total FROM expense where e_date between '$arr1_date'".
  " and '$dep1_date' GROUP BY e_group";
}

$result=mysql_query($query,$db_connection);

print "<br><br><table align='center' width='100%' cellspacing='1'
cellpadding='5' bgcolor='#ffffff' class='main'>";

print "<tr><td valign='top' colspan='4'>
  <p align='left' class='titlefont'><b>
  Invoiced Sales</b></p></td></tr>";

print "<table align='center' width='98%' cellspacing='1'
   cellpadding='5' bgcolor='#000000' class='main'>
  <tr bgcolor='#D0CBA5'>
  <td><b>Deposit</b></td>
  <td> <b>Fish</b> </td>
  <td> <b>Misc</b> </td>
  <td> <b>Snacks</b> </td>
  <td> <b>Groceries</b> </td>
  <td> <b>Cabin</b> </td>
  <td> <b>Campsite</b> </td>
  <td> <b>Rentals</b> </td>
  <td> <b>GST</b> </td>
  <td> <b>PST</b> </td>
  <td> <b>HMT</b> </td>
  <td> <b>Sales</b> </td>
   </tr>";

while($row=mysql_fetch_assoc($result)){
  
  $arr_tot[$row['e_group']] = $row['total'];
  $total = $row['e_group'];

  print "<tr bgcolor='#ffffff'>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>".
  "<td>". sprintf("%.2f",$total) . "</td>";
print "</table>";
}

mysql_free_result($result);






Reality is built on a foundation of dreams.
 
Ok.. thanks for the corrections. I am still having trouble getting the info from table to page..

Here is what the table looks like:

___________________________________________________________
|e_id | e_item | amount |quantity| e_date |e_group|
___________________________________________________________
| 1 |Chocolate Bar | 1.00 | 2 |2006-08-01| 5 |
| 2 |Water | 1.00 | 1 |2006-08-05| 4 |
| 3 |Rental | 20.00 | 3 |2006-08-05| 3 |
| 4 |Ice | 2.50 | 2 |2006-08-05| 4 |
___________________________________________________________

and so on. e_group denotes different tax rates for certain items.

I want to extract and total amounts for each e_group, hence the query:

"SELECT e_group, sum(amount * quantity) as total FROM expense where e_date between '$arr1_date'"." and '$dep1_date' GROUP BY e_group";

(variables for dates are already defined elswhere in the form)

Then I want to display those totals in an html table:

__________________________________________________________
|Group 1|Group2|Group3|Group4|Group5|Group6|Group7|Group8|
__________________________________________________________
| 0.00| 0.00| 60.00| 6.00| 2.00| 0.00| 0.00| 0.00|
__________________________________________________________

 
For your first post, tr close tage is missing
Code:
//...
  "<td>". sprintf("%.2f",$total) . "</td>";
print "[b]</tr>[/b]</table>";
}

I want to extract and total amounts for each e_group, hence the query:
"SELECT e_group, sum(amount * quantity) as total FROM expense where e_date between '$arr1_date'"." and '$dep1_date' GROUP BY e_group";

Hope the following snippet helps
Code:
$thStr = "";
$tdStr = "";
$query = "SELECT e_group, sum(amount * quantity) as total FROM expense where e_date between '$arr1_date'"." and '$dep1_date' GROUP BY e_group";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
  $thStr .= "<th>" . $row[0] . "</th>";
  $tdStr .= "<td>" . $row[1] . "</td>";
}
echo "<table border='1'>";
echo "<tr>" . $thStr . "</tr>";
echo "<tr>" . $tdStr . "</tr>";
echo "</table>";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top