Re: Dynamically generating PHP functions?
| From: | Cynic | Date: | Fri, 15 Sep 2000 10:52:28 +0000 |
| Subject: | Re: Dynamically generating PHP functions? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-16943@lists.php.net to get a copy of this message | ||
What about something like this:
function insertdata()
{
$fna = func_num_args() ;
$fgas = func_get_args() ;
$query = 'INSERT INTO ' . $fgas[ 0 ] . ' ' ;
for ( $i = 1 ; $i < $fna - 1 ; $i++ )
{
$query .= 'SET ' . ${$fgas[ $i ]} . ' = ' . $fgas[ $i ] . '
, ' ;
} ;
$query .= 'SET ' . ${$fgas[ $i ]} . ' = ' . $fgas[ $i ] ;
mysql_query( $query ) ;
} ;
Sure you could do it in quite a number different ways,
for example you could have pass it a connection pointer,
table name, and an array of the data to insert in the form
array( 'fieldname' => 'value' ) and first get the fieldnames
from the SQL server, and check if all the cells of the array
have valid indexes.
One more thing: the code above is completely off the top of
my head, and untested.
At 03:01 PM 9/15/00 +1030, Joseph H Blythe wrote the following:
--------------------------------------------------------------
>Hey,
>
>I have written a script that dynamically generates a form from a given
>database and table that would be suitable, as a generic data input
>device for any mysql table.
>
>My problem is that when I have tried to write the script for the forms
>action i.e.:
>
>...
>mysql("database","INSERT INTO table Values (
>'$field1',
>'$field2',
>'$field3',
>'$field4'
>)");
>
>
>I cannot figure out how to generate the above function dynamically so
>that that it has the correct number of fields for the given database
>table, does anyone know how one would go about writing php functions
>dynamically then executing them?
>
>I suppose it is much the same as the way php dynamically generates html,
>but I need it to do it on itself, if this makes any sense.
>
>Any help would be much appreciated.
>
>Joseph
>
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
>For additional commands, e-mail: php-general-help@lists.php.net
>To contact the list administrators, e-mail: php-list-admin@lists.php.net
------end of quote------
____________________________________________________________
Cynic:
A member of a group of ancient Greek philosophers who taught
that virtue constitutes happiness and that self control is
the essential part of virtue.
cynic@mail.cz
ICQ: 2808 2505