The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 12:47:34 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Question on array returned from DB query  (Read 3006 times)
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« on: August 08, 2007, 10:34:06 AM »

So I have an array returned from a database query, and I want to display it, so I did this:

Code:
echo('<TABLE border="1" summary="Tank Information">
<CAPTION><EM>Tank Information</EM></CAPTION>');
while($row = mssql_fetch_array($result))
{
echo('<TR>');
foreach($row as $a)
{
echo('<TD>'.$a);
}
echo('\n');
}

Now, I want to, for one thing, be able to display the table headers.

For another, I'm sure there's an easier, pre-written way to do what I'm doing, because in ASP.NET I used to just say "populate dataset" or something similar and it would do its thing.

Suggestions?
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: August 08, 2007, 11:45:02 AM »

Hey VS -

There is nothing in PHP by default to do what you are looking for... most folks just gin that up manually.

There is a class here:

http://www.phpclasses.org/browse/package/130.html

that might make things easier for you, but it's really not that difficult to do.

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #2 on: August 08, 2007, 11:51:55 AM »

How can I display the column names at the top of each column?

It's probably a simple array operation but I'm kinda rusty.
Logged

hai
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #3 on: August 08, 2007, 12:21:19 PM »

Sort of a hack but here's my solution:

Code:
echo('<TABLE border="2" summary="Tank Information">
<CAPTION><EM>Tank Information</EM></CAPTION>');

$num=mssql_num_fields($result);
echo('<TR>');
for($i=0;$i<=$num;$i++)
{
echo('<TH>'.@$field=mssql_field_name($result,$i));
}

while($row = mssql_fetch_array($result,MSSQL_ASSOC))
{
//print_r($row);
echo('<TR>');
foreach($row as $a)
{
if($a != '' && $a != 'REF!' && $a != '0' && $a != ' ')
{
echo('<TD>'.$a);
}
else
{
echo('<TD>-');
}
}
//echo('\n');
}
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: August 08, 2007, 01:51:29 PM »

That'd do the job just fine, as you certainly know by now.

I don't have the time just at the moment, but I think that adding this sort of thing to the dbConnection class in the code repository would be a good idea... I'll post here when I do it.

/p

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #5 on: August 09, 2007, 06:34:01 AM »

In the mean time here's a more clean version that I put into a separate file to clean up my code:

Code:
<?php
/**********************************************************************
*This function will display a datagrid-type dataset from an MSSQL query in PHP.                   *
*There are classes you can purchase that do the same thing in a snazzier way,                    *
*but I didn't want to pay for something I could easily do myself.                                         *
*This requires the php_mssql library to be installed on the webserver.                                  *
*It takes as an argument a result set from the mssql_query call and returns nothing               *
*it just prints a datagrid.                  -Drew                    8/9/2007                                   *
***********************************************************************/
function DataGrid($result)
{
if(mssql_num_rows($result)==0)//If the query did not return any rows...
{
echo"No data to display.";
}
else
{
echo('<TABLE border="2">');//Make a table.
$num=mssql_num_fields($result);
echo('<TR>');
for($i=0;$i<=$num;$i++)//Print each field name as a column header.
{
echo('<TH>'.@$field=mssql_field_name($result,$i));
//The @ supresses the error when we run out of field names.
}
while($row mssql_fetch_array($result,MSSQL_ASSOC)) 

echo('<TR>');//Begin the table row.
foreach($row as $a)
{
if($a != '' && $a != 'REF!' && $a != '0' && $a != ' ')
//My data has a bunch of junk in it, you can remove this part if yours is clean.
{
echo('<TD>'.$a);//Echo each piece of data as a <TD> element.
}
else
{
echo('<TD>-');//Easier on the eyes than having all that gibberish in the table.
}
}


}

}
?>

« Last Edit: August 09, 2007, 06:37:12 AM by vsloathe » Logged

hai
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #6 on: August 09, 2007, 06:35:13 AM »

Obviously you can change "mssql" to "mysql" if you're using that flavor of database.
Logged

hai
mrsdf
Rookie
**
Offline Offline

Posts: 20



View Profile
« Reply #7 on: August 14, 2007, 11:36:45 AM »

I've always used mysql_fetch_assoc, and
Code:
foreach ($row as $key=>$a)
..... and $key will have the column name.
Logged

We're sp4mmin', we're sp4mmin', I hope you like sp4mmin' too...
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!