Page 1 of 1
Setting default values
Posted: Tue Jan 29, 2008 1:15 pm
by jeffrydell
I need help in populating default values for HTML forms, hopefully by dynamically retrieving a list of field names from a MySQL table.
Consider an application which takes Customer info, Order info, then Destination info.
First I'd collect a Customer ID, then I'd go to the Customer Form to ensure the address, etc. is accurate.
If this is the first time the Customer form is being presented, I'll query a MySQL table corresponding to the Customer table. If a matching row is found, I'll put the result in a $_SESSION Array, then the HTML has fields which look like this:
Code: Select all
<input name="firstname" value="<?= $_SESSION['Customer']['firstname'] ?>" >
... and so on for the rest of the fields.
Each Form Action takes the $_POST[] data and validates what has been entered, passing the $_POST values to the same array so we can access the info later, or repopulate the fields if an error was detected - if there is an error the user gets 'bounced back' to the form for another try.
All is well and good so long as a record is found prior to the first presentation of the form. IF there is NO record, however, I currently have hard coded a list of field names in my script and I populate those Array elements with blanks (to avoid the nasty Var not Found warnings).
So I'm looking for a dynamic way (to be included in a function) to get all the field names from my Customer table, then if any field does not exist in my array, I can fill the array elements in with a blank "on the fly" - thus avoiding the need to hard code every 'blank' for every field for every table.
Any ideas? Thanks in advance for your help!
Re: Setting default values
Posted: Tue Jan 29, 2008 1:57 pm
by Zoxive
Code: Select all
function Customer_Data($Name){
if(isset($_SESSION['Customer'][$Name]))
return $_SESSION['Customer'][$Name];
else
return NULL; // OR '' OR w/e your default is
}
Re: Setting default values
Posted: Tue Jan 29, 2008 2:12 pm
by jeffrydell
Thanks ... this will give me a value OR a blank for the field submitted (the second part of the process). The big question at this point is how to get the application to "look" at a table, and develop a list of field names from that table (so I have the field name to pass to your function).
The goal is a dynamic function which can be used on any table. The benefit would be not having to change my code when I add a field to the table ... just add the field with an appropriate name to the FORM and I know my application will grab the value from the db or 'stuff' it blank (like you're doing).
Re: Setting default values
Posted: Tue Jan 29, 2008 2:24 pm
by Zoxive
I guess I skimmed threw your question to fast. I think I understand now.
You want to merge two arrays togather, the one with the user supplied data, and the one with emptys, that should be there.
Code: Select all
<?php
$UserArray = array(/* .... */);
// You should be able to populate the BlankArray with a mysql query "Describe `(TableName)`"
// Then loop threw the array results link so.
foreach($ResultArray as $Val){
$BlankArray[$Val['Field']] = NULL;
}
$FullArray = array_merge($BlankArray,$UserArray);
If you do it this way, you wont even the need the function in my other post.
Re: Setting default values
Posted: Tue Jan 29, 2008 3:05 pm
by jeffrydell
No, we're still not there, but that's another useful snippet for something else I've been struggling with. Again, thanks!
Let me try it this way:
IF (a record exists in the MySQL Table) // Note .. the MySQL table is the CORE of everything here
{
mysql_fetch_assoc($res); // Populate the array with the data from the row in the table.
}
else
{
THIS IS THE PART I NEED HELP WITH ... CREATE AN ARRAY THAT HAS ELEMENTS WITH THE SAME keys AS THE field names OF THE TABLE
/* Create the array with that mysterious function (defining ARRAY keys), then Populate the array values with '' */
}
I didn't use the code tags 'cause it isn't code, just a code 'construct' to help make my point.
Thanks again!
Re: Setting default values
Posted: Tue Jan 29, 2008 3:19 pm
by JAM
Re: Setting default values
Posted: Tue Jan 29, 2008 3:39 pm
by Zoxive
jeffrydell wrote:No, we're still not there, but that's another useful snippet for something else I've been struggling with. Again, thanks!
Let me try it this way:
IF (a record exists in the MySQL Table) // Note .. the MySQL table is the CORE of everything here
{
mysql_fetch_assoc($res); // Populate the array with the data from the row in the table.
}
else
{
THIS IS THE PART I NEED HELP WITH ... CREATE AN ARRAY THAT HAS ELEMENTS WITH THE SAME keys AS THE field names OF THE TABLE
/* Create the array with that mysterious function (defining ARRAY keys), then Populate the array values with '' */
}
I didn't use the code tags 'cause it isn't code, just a code 'construct' to help make my point.
Thanks again!
I'm quite confused now, because that's what my code does. It gets the Name of the Fields in the table, then puts them as the key name and puts the value as null. Then the array_merge merges the 2 arrays together, giving you all the Field names of the table as the array keys.
Code: Select all
<?php
$FieldNames = array();
$Query = "Describe `Customers`"; // Example Table
$Result = mysql_query($Query);
while($row = mysql_fetch_assoc($Result)){
// $row['Field'] is the field name, ie: firstname
$FieldNames[$row['Field']] = NULL;
}
print '<pre>';
print_r ($FieldNames);
print '</pre>';
// Should be all the Field Names, with no value set
/* Now you can do what you want, or array_merge them with the other data, and it will replace the real data with the blanks, but if there is no data it will be blank */
Re: Setting default values
Posted: Tue Jan 29, 2008 4:36 pm
by jeffrydell
OK - This time I was guilty of reading too quickly ... I glossed over you comment in line 4.
This works GREAT! I think rather than merging, I'll just do an if/else based on the mysql_num_rows($res) in my original query. I'll either mysql_fetch_assoc() OR do your code in a function that 'stuffs' blanks.
Thanks for being patient with me!!!!
Jeff
Re: Setting default values
Posted: Tue Jan 29, 2008 4:38 pm
by jeffrydell
Yes - I guess that would have done the job too, but I was messing with it before I got here and didn't get the syntax right.
Thanks for the suggestion!