Jump to content

Alias field names


RopeADope

Recommended Posts

Hi all.  I've got a little function to generate a simple form depending on the input.  What I'd like to do is alias the field names that get output to make them a little more human readable.  However, I'm not entirely sure how to do that with the output that I get.

 

The function....

function build_form($table_name){
$result=mysql_query("SELECT * FROM $table_name");
$num=mysql_num_rows($result);
$i=1;
echo "<table>";
while ($i < mysql_num_fields($result)) {
	$fields=mysql_fetch_field($result,$i);
	echo "<tr><td>" . $fields->name . "</td><td><input type=\"text\" size=\"30\" name=\"" . $fields->name . "\" /></td></tr>";
	$i++;
	};
echo "</table>";
};

 

Q:  Where would I have to modify the output from mysql_fetch_field?

 

My modified function...

function build_form($table_name){
$result=mysql_query("SELECT * FROM $table_name");
$num=mysql_num_rows($result);
$i=1;
echo "<table>";
while ($i < mysql_num_fields($result)) {
	$fields=mysql_fetch_field($result,$i);
                $field_name=str_replace("_"," ",$fields->name);
	echo "<tr><td>" . $field_name . "</td><td><input type=\"text\" size=\"30\" name=\"" . $field_name . "\" /></td></tr>";
	$i++;
	};
echo "</table>";
};

 

Not sure if that will work, but am I heading in the right direction?  Any help is much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/214004-alias-field-names/
Share on other sites

define your aliases directly in the select... instead of this:

$result=mysql_query("SELECT * FROM $table_name");

 

something like this

$result = mysql_query("SELECT field1withbadname AS field1,
                              field2same AS field2,
                              field3other AS 'This Field',
                              etc...etc
                        FROM $table_name");

Link to comment
https://forums.phpfreaks.com/topic/214004-alias-field-names/#findComment-1113717
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.