General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 3rd, 2003, 09:45 AM
PHP041 PHP041 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Turkey
Posts: 10 PHP041 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to PHP041
Question Select Boxes

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

Reply With Quote
  #2  
Old October 3rd, 2003, 11:34 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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.

Reply With Quote
  #3  
Old October 6th, 2003, 05:41 AM
PHP041 PHP041 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Turkey
Posts: 10 PHP041 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to PHP041
Confused

I am a little bit confused. Do yu know any scripts or example on this issue.

regards

Erdal

Reply With Quote
  #4  
Old October 6th, 2003, 06:59 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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:
if(sizeof($_POST["personnel"] == 3){
    
$p="'" join("','",$_POST["personnel"]) . "'";
}
else{
    print 
"Please choose three options from the personnel select box.";
    exit;



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.

Reply With Quote
  #5  
Old October 6th, 2003, 07:41 AM
PHP041 PHP041 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Turkey
Posts: 10 PHP041 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to PHP041
Unhappy

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

Reply With Quote
  #6  
Old October 6th, 2003, 08:09 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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:
<?php

//If $_POST vars have been sent, process them, printing the checked values.
if($_POST){

    foreach(
$_POST["personnel"] as $p){
        print 
$p "<br>";
    }

}
//Else print a form composed of six checkboxes with the same name.
else{

?>

<form method="POST" action="check.php">

<input type="checkbox" name="personnel[]" value="One"> One
<input type="checkbox" name="personnel[]" value="Two"> Two
<input type="checkbox" name="personnel[]" value="Three"> Three
<input type="checkbox" name="personnel[]" value="Four"> Four
<input type="checkbox" name="personnel[]" value="Five"> Five
<input type="checkbox" name="personnel[]" value="Six"> Six

<input type="submit" value="Submit">

</form>

<?php

}

?>


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.

Reply With Quote
  #7  
Old October 6th, 2003, 08:11 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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.

Reply With Quote
  #8  
Old October 9th, 2003, 06:45 AM
PHP041 PHP041 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Turkey
Posts: 10 PHP041 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to PHP041
Lightbulb

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

Reply With Quote
  #9  
Old October 9th, 2003, 07:00 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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:
<?php


if(sizeof($_POST["personnel"]) <=5){
    
$_POST["personnel"]=array_pad($_POST["personnel"],5,"NULL");
    
$people=join(",",$_POST["personnel"];
}
else{
    print 
"You may check no more than five personnel boxes.";
    exit;
}

?>


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.

Reply With Quote
  #10  
Old October 9th, 2003, 07:07 AM
PHP041 PHP041 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Turkey
Posts: 10 PHP041 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to PHP041
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.

Reply With Quote
  #11  
Old October 9th, 2003, 09:52 AM
PHP041 PHP041 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Turkey
Posts: 10 PHP041 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to PHP041
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Select Boxes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |