Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Display Column headings php/mysql
Old 08-18-2004, 03:46 PM Display Column headings php/mysql
Novice Talker

Posts: 7
Trades: 0
Hi,

Is there a way to display 'field' names from a table when displaying mysql results?
Sam Gamgee is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-18-2004, 06:04 PM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
yes. I am assuming you have fetched your data with php and the mysql_fetch_array functions. Then you just do a foreach loop;

foreach($retval as $key=>$value) {
echo "$key = $value<br />\n";
}

Where $retval is the variable which recieved the data from mysql_fetch_array.
__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Old 08-18-2004, 10:08 PM
Novice Talker

Posts: 7
Trades: 0
Thanks webwoRRks,

So is $key a set variable to represent column headings?

At the moment I am using this loop to display my results:
PHP Code:
echo "<table width='340' border='1' 
cellpadding='4' cellspacing='0'>\n"
;
while (
$row mysql_fetch_array($resultMYSQL_ASSOC)) 
{
  echo 
"\t<tr>\n";
  foreach (
$row as $col_value
  {
    echo 
"\t\t<td>$col_value</td>\n";
  }
  echo 
"\t</tr>\n";
}
echo 
"</table>\n"
How would I be able to incorporate the $key variable so that the field names would appear just once (at the top of the table)?

Last edited by Sam Gamgee; 08-18-2004 at 10:11 PM..
Sam Gamgee is offline
Reply With Quote
View Public Profile
 
Old 08-19-2004, 06:19 AM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
ah-hah, now we're talkin' (I just wanted to say that). Its nice and simple....

PHP Code:
<?php

echo "<table width='340' border='1' cellpadding='4' cellspacing='0'>\n";
$top true;
while (
$row mysql_fetch_array($resultMYSQL_ASSOC)) 
{
    if(
$top) {
        echo 
"\t<tr>\n";
        foreach(
$row as $key=>$col_value) {
            echo 
"\t\t<td>$key</td>\n";
        }
        echo 
"\t</tr>\n";
        
$top false;
    }

  echo 
"\t<tr>\n"
  foreach (
$row as $col_value
  { 
    echo 
"\t\t<td>$col_value</td>\n"
  } 
  echo 
"\t</tr>\n"

echo 
"</table>\n";

?>
__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Old 08-19-2004, 08:06 AM
Novice Talker

Posts: 7
Trades: 0
That's perfect.

Thanks so much for your help
Sam Gamgee is offline
Reply With Quote
View Public Profile
 
Old 08-19-2004, 09:17 AM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
no problem
__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Reply     « Reply to Display Column headings php/mysql
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17175 seconds with 12 queries