Jump to content

[SOLVED] Set Variable name use Field name from table


jvrothjr

Recommended Posts

I have a query string that sets the value of Variables below:

 

if ($DB == "Departments") {
$tracking_id = $data22['tracking_id'];
$dept_name = $data22['dept_name'];
}

 

simple and straight forward.

 

Now I would like to set a loop that would set the variable name and value based on the field names in the table queried.

 

if ($DB == "Departments") {
$querystring = "Select * from ".$DB;
$result=mysql_query($querystring);
while(($field = mysql_fetch_field($result))) {
	$field->name = $data22[$field->name];
}
}

 

can I set the variable name using the $field->name if so how?

 

I have tried this code with no luck and no luck searching.

 

This is a small example. I have queries with fifteen plus fields.

 

This would also remove the need to modify this code if a field needs to be added to the table.

 

And yes I have had to add fields to the tables.

  • 3 months later...

This works for me:

 

while($fld = mysql_fetch_field($result))

    {

 

$field_name=($fld->name); //assigns field name to a variable

 

//makes a new variable with the name of the field ie a field called John makes a variable called $john     

        ${"{$field_name}"} = $_POST[$fld->name];

 

}

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.