|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
hello all
I need a smal help with select boxes. I have report entry form and personel list under this list. This personel list is coming from the table personel by sql query and with a select box near that. When form is filled and more than one personall selected under the form, I can not insert the personel to the report table. Report table contains colums personel1, personel2,personel3, etc. Thanks you very much |
|
#2
|
||||
|
||||
|
First, you might consider modifying your table structure. When you find yourself adding numbered fields (personnel1, 2, 3, etc.) in a given table, there's a good chance your data's not normalized. That's not always the case, but you might want to take another look at your data and see if there'd be a benefit in breaking your personnel out into a new table.
To address your question, consider appending square brackets ( [ ] ) to your select box name. This will make it come across as an array. Then, in your PHP code, you'll treat that variable as an array and get its selected values by looping through the array and using the values to write your query. |
|
#3
|
|||
|
|||
|
Confused
I am a little bit confused. Do yu know any scripts or example on this issue.
regards Erdal |
|
#4
|
||||
|
||||
|
Here's a quick example. In your HTML code for the form, you'd have something like this:
Code:
<select name="personnel[]" size="5" multiple>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
<option value="six">Six</option>
</select>
This code creates a select box five rows tall that allows you to select multiple options by shift- or control-clicking. Note that its name has square brackets at the end, signifying that it's to be treated as an array. Now for the PHP code to process its values: PHP Code:
What I'm doing here is to test the $_POST["personnel"] array to make sure three items were selected. If not, I'm printing an error. I could also optionally just pop extra elements off the array or pad the array out to three elements. If the size is three, I'm creating a fragment of the SQL query by creating a string of quoted values based on the options selected and separated by commas. You can adapt this to suit your needs; hopefully it clarifies my original suggestion. |
|
#5
|
|||
|
|||
|
I am so soryy that I am confused you because it is check boxes instead of select boxes. Perosonal names are coming from table to the screen. I apologize again for misunderstanding
![]() |
|
#6
|
||||
|
||||
|
Ah, well, the code should be fundamentally the same. I just put together this little test page, using checkboxes instead of a select box:
PHP Code:
Copy this into a file named check.php and run it. When you go to the page, it'll print a form. Check any number of boxes and submit the form. It should simply print out the values of the boxes you checked. With any form control, in order to make it an array, you just stick square brackets at the end of the name. In the case of checkboxes, just name all checkboxes the same thing and all the checked values will be pushed onto the same array. |
|
#7
|
||||
|
||||
|
I think I still might be misunderstanding your question. If so, please post your table definition and the code you've got so far, and I'll see if that makes things more clear.
|
|
#8
|
|||
|
|||
|
Dear dhouston,
here is my table defination, CREATE TABLE `reports` ( `reportID` tinyint(3) unsigned NOT NULL auto_increment, `userID` tinyint(11) NOT NULL default '0', `eventID` tinyint(11) NOT NULL default '0', `userName` varchar(255) NOT NULL default '', `hospitalName` varchar(255) NOT NULL default '0', `departmentName` varchar(255) NOT NULL default '0', `instrumentName` varchar(255) NOT NULL default '', `brandName` varchar(255) NOT NULL default '', `model` varchar(255) NOT NULL default '', `serial` varchar(255) NOT NULL default '', `startTime` int(50) NOT NULL default '0', `finishTime` int(50) NOT NULL default '0', `totalWorkHours` int(50) NOT NULL default '0', `travelTime` int(50) NOT NULL default '0', `servicePurpose` varchar(255) NOT NULL default '', `jobDescription` text NOT NULL, `jobResult` varchar(255) NOT NULL default '', `date` date default '0000-00-00', `dateofEntry` date default NULL, `personel1` int(55) NOT NULL default '0', `personel2` int(55) NOT NULL default '0', `personel3` int(55) NOT NULL default '0', `personel4` int(55) NOT NULL default '0', `personel5` int(55) NOT NULL default '0', PRIMARY KEY (`reportID`) ) ; I also have a users table where I get my personelID so What I want to do is I want to take userID from that users table and put into reports table into personel1, personel2... etc. I can get the users name as follow(city is another table) $query2 = mysql_query("select users.userLogin,users.userID,city.cityName,city.ci tyID from users,city where users.cityID=city.cityID and city.cityName='$cityName' "); while($ans2 = mysql_fetch_array($query2)) { $userid = $ans2["userID"]; $userLogin = $ans2["userLogin"]; echo "<br><input type=\"checkbox\" name=\"personnel[]\" value=$userid> " . $userLogin .""; } rest of the code.. From this screen I can get the users list select them and send to second page and print the userID on the screen by the script that you sent to me. But I can not insert them into personel columns under reports table. And also I do not want the secretary select more than 5 personel. I know a little bit complicated ![]() Thanks for your help |
|
#9
|
||||
|
||||
|
I think you're just about there. You've got the checkboxes printing out now, and all that remains is to use the submitted values to build a query. Take my checkbox for example. Instead of printing the values as I do, you'd do something like this:
PHP Code:
What this does is to check the size of the array. If it's less than or equal to five, it pads the array to a size of five with "NULL" values. Then it sets $people to a comma-separated list of the values. You can use this later to build the personnel portion of your query. If the personnel array has more than five elements, an error is printed and the script exits. |
|
#10
|
|||
|
|||
|
I am sorry but I am little bit confuse after I set the commas on array, should I go and run insert command to enter the valiables to the report table.
|
|
#11
|
|||
|
|||
|
Dear dhouston,
Thank you for your help, I think I have solved my problem. here is the code <?php include "config.php"; if(sizeof($_POST["personnel"]) <=5) { $_POST["personnel"]=array_pad($_POST["personnel"],5,"NULL"); $people=join(",",$_POST["personnel"]); echo "<br><br>" . $_POST["personnel"][0]; echo $_POST["personnel"][1]; echo $_POST["personnel"][2]; echo $_POST["personnel"][3]; echo $_POST["personnel"][4]; } else{ print "You may check no more than five personnel boxes."; exit; } ?> from here I can go and run a sql command and enter these variables to table.. Best Regards |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Select Boxes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|