Connecting Tech Pros Worldwide Forums | Help | Site Map

Quick PHP / MYSQL drop down question

Newbie
 
Join Date: Mar 2008
Posts: 5
#1: Mar 24 '08
Hi,

I've created a drop down menu that gets it's values (ID and SCHOOL) from a MYSQL database. I've created a variable for the ID value, but can't figure out how to do this for the SCHOOL value.

The code ...
[php]<?php
$sql = "SELECT ID, SCHOOL FROM SCHOOL ORDER BY ID";
$users = mysql_query($sql,$conn);
while($row = mysql_fetch_array($users))
{
echo ("<option value=\"$row[ID]\" " . ($schooladd == $row["ID"] ? " selected" : "") . ">$row[SCHOOL]</option>"); }
?>[/php]
I'm sure it's something simple, but I've been looking at it for so long I just can't see it - any ideas ?

Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 24 '08

re: Quick PHP / MYSQL drop down question


Welcome to The Scripts!

When you mean by 'add a variab'le' to add an option to the select list, then the following will suffice. I cannot fill in the variable you use to check the checked attribute, so it is named $variable.[php]<?php
$sql = "SELECT ID, SCHOOL FROM SCHOOL ORDER BY ID";
$users = mysql_query($sql,$conn);
while($row = mysql_fetch_array($users))
{
echo ("<option value=\"$row[ID]\" " . ($schooladd == $row["ID"] ? " selected" : "") . ">$row[SCHOOL]</option>");
echo ("<option value=\"$row[SCHOOL]\" " . ($value == $row["SCHOOL"] ? " selected" : "") . ">$row[SCHOOL]</option>");
}
?>[/php]If you mean something else, then please explain a bit further.

Ronald
Newbie
 
Join Date: Mar 2008
Posts: 5
#3: Mar 24 '08

re: Quick PHP / MYSQL drop down question


Whoops - sorry Mods, will remember in future !

@ Ronald: Thanks for the welcome, I'll explain a bit further ...

The variable $schooladd contains the row ID on the value list selection and I need a variable to contain the actual text displayed based on the value list selection (called $schoolname for example)

Unfortunately your solution creates a duplicate in the drop down list.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Mar 24 '08

re: Quick PHP / MYSQL drop down question


Does not make it any clearer. Let me see if I understand correctly. You want the value in $row['SCHOOL'], which is displayed in your drop down list text, to be stored from the POST array into a variable after the form has been submitted. Correct?

Ronald
Newbie
 
Join Date: Mar 2008
Posts: 5
#5: Mar 24 '08

re: Quick PHP / MYSQL drop down question


Quote:

Originally Posted by ronverdonk

Does not make it any clearer. Let me see if I understand correctly. You want the value in $row['SCHOOL'], which is displayed in your drop down list text, to be stored from the POST array into a variable after the form has been submitted. Correct?

Ronald

Yup, that sounds about right !

This is where the school ID appears a bit further down the script:

Expand|Select|Wrap|Line Numbers
  1. <?php echo "School id = $school "; ?>
I've realised in my stupidity that I've forgot to mention that a piece of Javascript 'auto-submits' the form (and populates the variable) when the drop down menu is used. So all I need is another variable (for the school name) added somewhere in the piece of code in a similar way to the $school variable shown in my first post .. if possible ?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#6: Mar 24 '08

re: Quick PHP / MYSQL drop down question


There are 2 ways of doing this: the easy and the hard way.
The easy way is to concat, with a separator such as underscore ()_), the $row[SCHOOL] value to the $orw[ID] value in the option. Like[php]echo ("<option value=\"$row[ID]_$row[SCHOOL]\" " . ($schooladd == $row["ID"] ? " selected" : "") . ">$row[SCHOOL]</option>");[/php]In the POST you can easily take that out, using[php]$str=explode('_', $_POST[select_name]);
// $str[0] = the ID
// $str[1] = the school name[/php]
The hard way is to use JavaScript to get the text from the selected drop down button and insert it into the value of a hidden field in the form before submitting it.

Ronald
Newbie
 
Join Date: Mar 2008
Posts: 5
#7: Mar 24 '08

re: Quick PHP / MYSQL drop down question


Quote:

Originally Posted by ronverdonk

There are 2 ways of doing this: the easy and the hard way.
The easy way is to concat, with a separator such as underscore ()_), the $row[SCHOOL] value to the $orw[ID] value in the option. Like[php]echo ("<option value=\"$row[ID]_$row[SCHOOL]\" " . ($schooladd == $row["ID"] ? " selected" : "") . ">$row[SCHOOL]</option>");[/php]In the POST you can easily take that out, using[php]$str=explode('_', $_POST[select_name]);
// $str[0] = the ID
// $str[1] = the school name[/php]
The hard way is to use JavaScript to get the text from the selected drop down button and insert it into the value of a hidden field in the form before submitting it.

Ronald

Aaah ok, I think I'll try the easier option then !

I was actually hoping that I could add a $schoolname == $row["SCHOOL"] somewhere in the code similar to where it says $schooladd == $row["ID"] - I assume this isn't possible for some reason ?

No worries though, at least it CAN be done - thanks for all your help mate.

Nath
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#8: Mar 24 '08

re: Quick PHP / MYSQL drop down question


Quote:

Originally Posted by stealthbristol

Aaah ok, I think I'll try the easier option then !
I was actually hoping that I could add a $schoolname == $row["SCHOOL"] somewhere in the code similar to where it says $schooladd == $row["ID"] - I assume this isn't possible for some reason ?
No worries though, at least it CAN be done - thanks for all your help mate.

Nath

[php]$schooladd == $row["ID"] [/php] is NOT an assignment, it is a comparison. And you use it with a ternary function, i.e.[php]$schooladd == $row["ID"] ? " selected" : "")[/php]meaning: if $schooladd equals $row['ID'] then set attribute 'selected 'in option statement, otherwise set '' in option statement. Nothing to do with assignment to a variable!

Ronald
Newbie
 
Join Date: Mar 2008
Posts: 5
#9: Mar 24 '08

re: Quick PHP / MYSQL drop down question


Quote:

Originally Posted by ronverdonk

[php]$schooladd == $row["ID"] [/php] is NOT an assignment, it is a comparison. And you use it with a ternary function, i.e.[php]$schooladd == $row["ID"] ? " selected" : "")[/php]meaning: if $schooladd equals $row['ID'] then set attribute 'selected 'in option statement, otherwise set '' in option statement. Nothing to do with assignment to a variable!

Ronald

That would explain your confusion earlier !

Sorry, but I'm still finding my feet with PHP / MYSQL and we all gotta start somewhere right ?

So, in conclusion, apart from what you suggested earlier I cannot assign a variable with the 'text' part of a drop down menu selection ?

(Last question then I'll leave you alone)
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#10: Mar 24 '08

re: Quick PHP / MYSQL drop down question


There are 2 ways: see post no 6

Ronald
Reply