473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Quick PHP / MYSQL drop down question

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
Mar 24 '08 #1
9 1780
ronverdonk
4,258 Expert 4TB
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
Mar 24 '08 #2
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.
Mar 24 '08 #3
ronverdonk
4,258 Expert 4TB
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
Mar 24 '08 #4
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 ?
Mar 24 '08 #5
ronverdonk
4,258 Expert 4TB
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
Mar 24 '08 #6
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
Mar 24 '08 #7
ronverdonk
4,258 Expert 4TB
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
Mar 24 '08 #8
[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)
Mar 24 '08 #9
ronverdonk
4,258 Expert 4TB
There are 2 ways: see post no 6

Ronald
Mar 24 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

51
by: w_curtis | last post by:
I'm an Access user, and I'm trying to learn MySQL and then PHP so I can make some web databases. But it just isn't clicking. I've followed some tutorials, and picked up a book, but just getting...
0
by: tusaar | last post by:
Hi all I am in big need for a drop down menu created with php, mysql and ajax. Exactly, I need three drop down menu (Category, Subcategory and Item). The data of each drop down will come from...
2
by: Boujii | last post by:
Greetings, I have been attempting to make a drop down menu of countries. From this menu I wish to create a variable in order to INPUT into mysql database. I have no trouble making the drop down menu,...
2
by: dm06tw | last post by:
Hi there, I've got a question about an odd annoyance that's been happening on our new php form. The form originally had javascript controlled dynamic dropdown boxes which would force the user to...
7
by: dongletran06 | last post by:
Hi, Please help me find out what wrong with my codes in inputting from my form to mysql database using drop down menu. Below is the codes I used. Only the drop down is not working but the "input...
5
by: mramsay | last post by:
HI, I'm having alot of problems trying to figure out how to create drop down links from a hyperlink on my homepage. I have a hyperlink called Programs (this is for a community centre) When...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
10
by: darkenroyce | last post by:
Hi Guys I am creating a sports database and one of the features involves create drop down dialogue boxes that retrieve data from MySQL tables and provides them as <option> within...
20
by: brendanmcdonagh | last post by:
Hi all, This is my first attempt at anything to do with php/mysql so any help will be greatly appreciated. I have been set a challenge to come up with a web app to enable staff to log on to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.