Re: Validation problem with array.
| From: | Brent Baisley | Date: | Thu, 21 Oct 2004 14:20:08 +0000 |
| Subject: | Re: Validation problem with array. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-199992@lists.php.net to get a copy of this message | ||
Since your data on the client is linked, you should keep it linked on submission by nesting an array with in the array.
Your client HTML should like something like this:
<input type="text" name="skills[0][skill]" value="" size="20">
<select name="skills[0][skys]" size="1">
<option ...>
</select>
<select name="skills[0][Lused]" size="1">
<option ...>
</select>
...
<input type="text" name="skills[1][skill]" value="" size="20">
<select name="skills[1][skys]" size="1">
<option ...>
</select>
<select name="skills[1][Lused]" size="1">
<option ...>
</select>
All the way up to skills[10][...], or however many you want. If everything is filled out, you will then get a 10x3 array, just like a spreadsheet.
Since empty fields won't get submitted, you will be missing parts of your array. But that's easy to check for.
So for submission your are creating grid contain in one array variable skills:
row skill skys Lused
1 C++ 3 10/1/2000
2 0 10/21/2004
3 ...
Now it's easy to process the grid and check for missing data.
$skills = $_POST['skills'];
//Process each row of the array $skills
//Each row may have up to three elements: skill, skys, Lused
foreach($skills as $skill) {
//Check if a skill was entered by check for array element named 'skill'
if(isset($skill['skill'])) {
$query = 'INSERT INTO dbName (SkilCerts, NumYear, Lused)
VALUES ("'.$skill['skill'].'", '.$skill['skys'].', '.$skill['Lused'].')';
}
}
On Oct 21, 2004, at 6:55 AM, Stuart Felenstein wrote:
Graham, I'm going to make those changes. Not sure if that was creating some crazy behaviour. See inline: --- Graham Cossey <graham@cossey.plus.com> wrote: ................I don't know if I care if it's been entered. My main concern would be to make sure that if a $skill was entered, then a matching $skys and $slus should be: skills[0]-baker skys[0]-7 slus[0]-2002 but wouldn't want skill[0]-baker skys[1]-7 slus[2]-2002 In other words I need to figure out how to check that each "line" is complete. But~ right now, it is matching all the values/inputs correctly. If someone just chose a slus, seems the database won't take it.{if ($skill != '') // Has this skill beenentered?Not sure why I would want too:{ // You may also want to check for sky & sluentriesIt outputs the insert and values string. And it seems to print the iteration: INSERT INTO LurkProfiles_Skicerts (SkicertID, ProfileID, SkilCerts, NumYear, Lused) VALUES (null, 39, 'aaaaa', 2,1)INSERT INTO LurkProfiles_Skicerts (SkicertID, ProfileID, SkilCerts, NumYear, Lused) VALUES (null, 39, 'bbbbb', 3,2)INSERT INTO LurkProfiles_Skicerts (SkicertID, ProfileID, SkilCerts, NumYear, Lused) VALUES (null, 39, 'ccccc', 4,3)INSERT INTO LurkProfiles_Skicerts (SkicertID, ProfileID, SkilCerts, NumYear, Lused) VALUES (null, 39, 'ddddd', 5,4)INSERT INTO LurkProfiles_Skicerts (SkicertID, ProfileID, SkilCerts, NumYear, Lused) VALUES (null, 39, 'eeeee', 6,5)your insertions were successful//$result = mysql_query($query); $res6 = run_query($query); echo $query;^^^^^^^^^^^ What does this output?This probably makes sense to add , perhaps that is why if I don't use all the elements for input I get a syntax error. Stuart -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Brent Baisley Systems Architect Landover Associates, Inc. Search & Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577}// end if ($skill != '')