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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.