
![]() |
vsloathe
So I have an array returned from a database query, and I want to display it, so I did this:
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(' '); } 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.NETI used to just say "populate dataset" or something similar and it would do its thing.Suggestions? perkiset
Hey VS -
There is nothing in PHPby 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.htmlthat might make things easier for you, but it's really not that difficult to do. /p vsloathe
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. vsloathe
Sort of a hack but here's my solution:
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(' '); } perkiset
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 vsloathe
In the mean time here's a more clean version that I put into a separate file to clean up my 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. } } } } } ?> vsloathe
Obviously you can change "mssql" to "mysql" if you're using that flavor of database.
mrsdf
I've always used mysql_fetch_assoc, and
foreach ($row as $key=>$a) ..... and $key will have the column name.
|

Thread Categories

![]() |
![]() |
Best of The Cache Home |
![]() |
![]() |
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads