geraldmpiper Posted February 23, 2007 Share Posted February 23, 2007 This is what I'm doing: Each time I create a new table in MySQL, I create new .php files to display the contents of the table, a form to add new records, a form to edit existing records, and a 'commit' file to commit my changes, either add or edit, to the table. I often steal my old code, and then search & replace field names for the new table. I find myself doing this over and over and over again. And then, when I add a new field (or two or three) to a table, I must edit each of those files, and add new '$fieldName = $row['fieldName']' lines in different places. This I also do over and over and over again. What I would like to do: I'd like to create an array of field names, and programmatically create the required variables (same names as the field names) and assign the field values to them. Then, I'm hoping, it would just be a matter of adding the new field names to the array. Am I crazy? Or am I missing something? Is there a good tutorial out there that can walk me through this? I'm spending way too much time on the mechanics of coding correctly, and not enough time on what I want to do with my data. I know there's a better way - but I'm having trouble finding it. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
btherl Posted February 23, 2007 Share Posted February 23, 2007 I don't see why it's not possible. You can use functions like extract() and array_map(), or just foreach loops. What I do is I write functions which do a particular task with the database, which may involve several tables, but which acts as a single unit. I don't make interfaces for each table, as I rarely use any table in more than one or two different ways. I work at a different level of abstraction. Quote Link to comment Share on other sites More sharing options...
geraldmpiper Posted February 23, 2007 Author Share Posted February 23, 2007 Thanks. Can you include some sample code, so that I can understand the syntax? In the meantime, I'll brush up on extract() and array_map(). Quote Link to comment Share on other sites More sharing options...
btherl Posted February 23, 2007 Share Posted February 23, 2007 Hmm.. this ought to work: $res = mysql_query(...); while ($row = mysql_fetch_assoc($res)) { extract($row, EXTR_OVERWRITE); } But that will only work for a single row.. Actually, I don't really understand what you want Since using $fieldName = $row['fieldName'] will only ever work with a single row at one time. I'm not sure where you want the data to go after that.. The method above means you don't need to make any record of which columns are in the table. But there is the question of how those variables get back to the calling function. Usually I return an array like this: $arr = array( 'fieldName' => $fieldVal, 'fieldName2' => $fieldVal2, ); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.