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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Suggestions on displaying query results onto page?

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
I am using the 'odbc_' funtions to work with a database.

The results I get from a query are moved into a variable called $results and contain the following:

TYPE HOURS RATE TOTAL
personal 8 5.25 42
regular 24 5.25 126
sick 8 5.25 42

Basically, this is data pulled from a paycheck details table.

What I want to do is output results of the query onto the page in a column format with some space in between each column to display exactly how I gave the above example. Additionally, I want to be able to loop through the results and add the values to get individual column totals for all rows returned.

Are the results that I move into $results an array or do I need to somehow put them in an array and then echo out the results of the array? I ran into the issue when trying to return the row count and getting back -1 but found a post with a function to handle that. It just seems like I need to be using a combination of array funtions and loops to get the data out.

I can provide further information if needed. Thanks in advance for any suggestions.
 
Code:
echo "<table>";
echo "<tr><td>Type</td><td>Hours</td><td>Rate</td><td>Total</td></tr>";

$sql = "Select * from $table";
$result = mysql_query ($sql) or die ("db error: ".mysql_error());
$hourstotal=0;
$totaltotal = 0;
while ($row=mysql_fetch_assoc($result))
{
  extract ($row);
  echo "<tr>";
  echo "<td>$type</td>";
  echo "<td>$hours</td>";
  echo "<td>$rate</td>";
  echo "<td>$total</td>";
  echo "</tr>";
  $hourstotal = $hourstotal + $hours;
  $totaltotal = $totaltotal + $total;
{
echo "<tr><td>&nbsp;</td><td>$hourstotal</td><td>&nbsp;</td><td>$totaltotal</td></tr>";
echo "<\table>";
 
jpadie-

The issue with using the code snippet you suggested is that I am not pulling from a MySQL database and have to use odbc_ functions. There is not a odbc_fetch_assoc function that I can use... I am not very good with the arrays so I am not sure what odbc_ functions I can use to get the same results.

I still looking at it, thanks for the suggestion.
 
are you using the DB abstraction layer? if so the:

Code:
$results = $db->query($sql, $params);
while ($results->fetchInto($row))
{
  //do things.
}

if you are just using plain odbc (i am NOT SURE about this)

Code:
$result = odbc_exec($dbconn, "Select * from table");
while ($row=odbc_fetch_array($result))
{
//do things

}
 
jpadie-

The plain odbc is correct. I finally got it to work using that last syntax...

Thanks again for the suggestions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top