![]() |
PHP/MYSQL: form <select> question
How can you fetch data into select form fields and have the data stored in the database as selected?
Code:
<select name="favourite_fruit"> Code:
<select name="favourite_fruit"> |
<select name="favourite_fruit">
<option <? if($row[favorite_fruit] == 'Apples') { echo "selected"; }>Apples</option> <option <? if($row[favorite_fruit] == 'Bananas') { echo "selected"; }>Bananas</option> <option <? if($row[favorite_fruit] == 'Oranges') { echo "selected"; }>Oranges</option> </select> |
Quote:
except you didn't close the php with ?> :winkwink: |
That makes sense. Thanks again k0nr4d. I'll try this out shortly.
Note: I'll Paypal you some funds if I keep having to come to GFY for programming advice/code lol. Thanks again bro! |
I like ternaries :)
Quote:
|
Quote:
|
Gotta make sure the processing code can pull the array as well.
|
Quote:
|
Thanks again boys... the code worked... y'all are better then google... :thumbsup
BTW what do you guys use to code in? Was thinking of using Notepad++ |
Quote:
:party-smi |
RE: dreamweaver
LOL, me as well but was going to look into other editors for programming starting with Notepad++ as it's free to use and to see if they make a difference in reading/writing code. As you can see though I'm still a noob and these tools probably won't benefit me for awhile.
|
I use EmEditor, but Notepad++ is also very good..
|
Quote:
|
Quote:
|
i use dreamweaver, except only the code view. Just force of habit
|
Zend Studio 8.
|
Not to be a dick, but the proper way to do "selected" in an option is selected="selected". Same thing for checkboxes etc. Of course that's if you're doing your HTML to adhere to some standards.
|
"selected" by itself works fine...no less 'proper' than the other way
|
I have found that dreamweaver and notepad++ suffer from the same problem. They add hidden characters that cause scripts to not run properly on some OS's. Character encoding is a serious issue when dealing with *nix OS flavors. With that in mind I either use geany (for quick edits) or NetBeans (7 beta) for projects.
I also noted an odd issue when I was using notepad++ and the explorer plugin... for some reason, on occasion, it would somehow corrupt the security descriptors of my windows partitions. It would lock up and the next thing you know I was having partition issues. I am still not sure what exactly the issue was but I stopped using it and haven't had any more issues with partition corruption. |
Quote:
There's a huge % of pages that don't even declare DOCTYPE though. :Oh crap |
With notepad++ if you go to Settings -> Preferences ->New Document/Default Directory you can fix that stupid BOM issue. Just check UTF-8 without BOM.
|
How would you do the same but this time fetch data from a mysql table then display it in a select form field and have the value stored in the model_in_city table as selected?
Here's what I have so far but every city is being displayed in its own dropdown menu. Code:
<?php |
Code:
<?php |
Thanks sarettah that worked.
I'm going to play around now with the code to see if I can get the selected value to work. |
Quote:
|
Code:
create table model_in_city city_display = their city choice.. models can choose up to 4 cities to be displayed in. In this example i'm trying to get city_display = 1 to work first. Here's what I have now but don't know to to incorporate the selected value into the form: Code:
<?php |
Well, first you probably want your dropdown to reflect the city id when you are pulling in your vars.
Like this: Code:
echo "<option value=" . $city_id . ">$name</option>"; If you have a selected city coming in then what you would do is compare the city_id in the row you are looking at with the city_id coming in and mark it as selected when you find it: Code:
while ($row = $result->fetch_assoc()) |
This is the form field and it will allow for more than one selection:
Code:
<select name="blogs[]" size="55" multiple="multiple" style="font-size:10px;"><? displayBlogs(); ?></select> This is the function that created the list in the form field: Code:
function displayBlogs() { This captures the field: Code:
$blogs = $_POST['blogs']; This forms the statement: Code:
for ($x = 0;$x<count($blogs);$x++) This selects them from the DB: Code:
$grabblog = mysql_query("select * from blogs where ($ids) order by name;",$wp) or die (mysql_error()); Now you can do with them what you want: Code:
while ($rowsgrab = mysql_fetch_assoc($grabblog)) |
Thanks, I'll play with this more in about an hour. Question.. I have 2 columns in both tables that I'm quering the same. Can I use:
$city_id = $row['city.city_id']; $model_in_city_id = $row['model_in_city.city_id']; So mysql knows which one I'm referring to? |
Ok, I had to use the as clause in the query to separate the 2 different column names.
Here's what the code looks like now: Code:
<?php I think I may have to perform another query since the cities are entered in by city id number in the model_in_city table to fetch the name of the city instead to display as selected. Note: I commented out the if statement in the code above. |
What I was saying in icq:
Code:
<?php |
I am missing a close tag on one of the ifs in there, sorry about that.
Code:
<?php |
Ok, like I said in icq.
A test page is at http://www.madspiders.com/selecttest.php The code I have for it (using mysql, not mysqli because I do not think I have mysqli on the server) is: Code:
<?php |
Home now,
as I said in icq, that version was so you could see what was happening all the way through. To do it from one query like you were trying to do would look like this: http://www.madspiders.com/selecttest1.php Code:
<?php . |
Lol, I just can't keep this out of my head dammit.
Okay, when we talked about it, you had said something about using 4 different dropdowns, one for each selected city and that you had the cities marked as 1, 2, 3, 4 in the city_display field. I went in and created a new model_in_city table and named it model_in_city_mod and emulated the way you had that set up. So you could do your 4 separate dropdowns kinda like this: http://www.madspiders.com/selecttest2.php Code:
<?php |
Thanks man that worked!
Now have to add this code to a form with other queries and get it working there. Will try it in about an hour and report back if I have problems. Thanks again sarettah. |
All times are GMT -7. The time now is 05:15 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123