![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
![]() How can you fetch data into select form fields and have the data stored in the database as selected?
Code:
<select name="favourite_fruit"> <option>Apples</option> <option>Bananas</option> <option>Oranges</option> </select> Code:
<select name="favourite_fruit"> <option selected><?= $fruit ?></option> <option>Bananas</option> <option>Oranges</option> </select> |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
Confirmed User
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,229
|
<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>
__________________
Mechanical Bunny Media Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 | |
Confirmed User
Industry Role:
Join Date: May 2003
Posts: 3,765
|
Quote:
![]() except you didn't close the php with ?> ![]()
__________________
flexx [dot] aeon [at] gmail |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
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! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 | |
Confirmed User
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
|
I like ternaries
![]() Quote:
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 |
Confirmed User
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,229
|
ahhh yeah sorry, stupid gfy doesn't have syntax coloring in the textbox
![]()
__________________
Mechanical Bunny Media Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 |
Confirmed User
Industry Role:
Join Date: Mar 2011
Location: Mansfield, Ohio
Posts: 119
|
Gotta make sure the processing code can pull the array as well.
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 |
Confirmed User
Industry Role:
Join Date: May 2003
Posts: 3,765
|
yeah i'm pretty reliant on coloring as well....but i been breathing/sleeping/eating/shitting PHP the last couple of weeks so i'm in 'the zone'
__________________
flexx [dot] aeon [at] gmail |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
Thanks again boys... the code worked... y'all are better then google...
![]() BTW what do you guys use to code in? Was thinking of using Notepad++ |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 | |
Confirmed User
Industry Role:
Join Date: May 2003
Posts: 3,765
|
Quote:
![]()
__________________
flexx [dot] aeon [at] gmail |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
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.
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#12 |
Too lazy to set a custom title
Industry Role:
Join Date: Sep 2005
Location: Springfield
Posts: 13,826
|
I use EmEditor, but Notepad++ is also very good..
__________________
Make a bank with Chaturbate - the best selling webcam program ![]() ![]() ![]() Ads that can't be block with AdBlockers !!! /// Best paying popup program (Bitcoin payouts) !!! PHP, MySql, Smarty, CodeIgniter, Laravel, WordPress, NATS... fixing stuff, server migrations & optimizations... My ICQ: 27429884 | Email: ![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#13 | |
Confirmed User
Industry Role:
Join Date: May 2003
Posts: 3,765
|
Quote:
__________________
flexx [dot] aeon [at] gmail |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#14 | |
Confirmed User
Industry Role:
Join Date: Sep 2002
Posts: 1,209
|
Quote:
![]()
__________________
Awesome cloud hosting by DigitalOcean |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#15 |
Confirmed User
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,229
|
i use dreamweaver, except only the code view. Just force of habit
__________________
Mechanical Bunny Media Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#16 |
Confirmed User
Industry Role:
Join Date: Mar 2011
Location: Mansfield, Ohio
Posts: 119
|
Zend Studio 8.
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#17 |
Too lazy to set a custom title
Industry Role:
Join Date: May 2004
Location: West Coast, Canada.
Posts: 10,217
|
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.
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#18 |
Confirmed User
Industry Role:
Join Date: May 2003
Posts: 3,765
|
"selected" by itself works fine...no less 'proper' than the other way
__________________
flexx [dot] aeon [at] gmail |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#19 |
Confirmed User
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
|
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. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#20 | |
Confirmed User
Industry Role:
Join Date: Dec 2005
Location: Arizona/Indiana
Posts: 366
|
Quote:
There's a huge % of pages that don't even declare DOCTYPE though. ![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#21 |
Confirmed User
Industry Role:
Join Date: Mar 2011
Location: Mansfield, Ohio
Posts: 119
|
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.
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#22 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
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 include("../includes/connect.php"); $query = "SELECT * FROM city"; if ($result = $db->query($query)) { while ($row = $result->fetch_assoc()) { $name = $row['city_name']; echo "<select name=\"select\">"; echo "<option>$name</option>"; echo "</select>"; } $result->free(); } $db->close(); ?> |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#23 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,115
|
Code:
<?php include("../includes/connect.php"); $query = "SELECT * FROM city"; if ($result = $db->query($query)) { echo "<select name=\"select\">"; while ($row = $result->fetch_assoc()) { $name = $row['city_name']; echo "<option>$name</option>"; } echo "</select>"; $result->free(); } $db->close(); ?>
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#24 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
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. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#25 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,115
|
What does your model_in_city table look like? (if you don't mind me asking of course) ;p
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#26 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
Code:
create table model_in_city ( model_id int unsigned not null, city_id int unsigned not null, city_display tinyint(2) unsigned, primary key (model_id, city_display) ); 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 include("../includes/connect.php"); $id = 17; $query = "SELECT city.*, model_in_city.* FROM city, model_in_city WHERE model_in_city.model_id = $id AND model_in_city.city_display = 1"; if ($result = $db->query($query)) { echo "<select name=\"select\">"; while ($row = $result->fetch_assoc()) { $name = $row['city_name']; $city_id = $row['city_id']; echo "<option>$name</option>"; } echo "</select>"; echo "$city_id"; // just making sure the correct value is being fetched. $result->free(); } $db->close(); ?> |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#27 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,115
|
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()) { $name = $row['city_name']; $city_id = $row['city_id']; echo "<option value=" . $city_id if($row['city_id']==$idin) { echo " selected "; } echo ">$name</option>"; }
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#28 |
Damn Right I Kiss Ass!
Industry Role:
Join Date: Dec 2003
Location: Cowtown, USA
Posts: 32,409
|
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() { global $getquery,$optionrows,$blogs; //$count = mysql_num_rows($getquery); while ($optionrows = mysql_fetch_assoc($getquery)) { $select = ''; for ($x=0;$x<count($blogs);$x++) { if ($blogs[$x] == $optionrows["id"]) { $select = ' selected '; } } echo "<option value=\"".$optionrows["id"]."\"$select>".$optionrows["name"]."</option>"; } } This captures the field: Code:
$blogs = $_POST['blogs']; This forms the statement: Code:
for ($x = 0;$x<count($blogs);$x++) { if ($x<count($blogs)-1) { $ids .= "id = $blogs[$x] or "; } else { $ids .= "id = $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)) |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#29 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
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? |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#30 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
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 include("../includes/connect.php"); $id = 17; $query = "SELECT city.city_id AS city_id_1, city.city_name, model_in_city.model_id, model_in_city.city_id AS city_id_2, model_in_city.city_display FROM city, model_in_city WHERE model_in_city.model_id = $id AND model_in_city.city_display = 1"; if ($result = $db->query($query)) { echo "<select name=\"select\">"; while ($row = $result->fetch_assoc()) { $name = $row['city_name']; $city_id_1 = $row['city_id_1']; $city_id_2 = $row['city_id_2']; echo "<option value=\"$city_id_1\">$name</option>"; /* if($row['city_id'] == $city_id) { echo "<option selected>$name</option>"; } */ } echo "</select>"; echo "$city_id_2"; // just checking to make sure this value is correct. $result->free(); } $db->close(); ?> 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. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#31 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,115
|
What I was saying in icq:
Code:
<?php include("../includes/connect.php"); $query="select city_id, city_name from city order by city_name"; if ($city_result = $db->query($query)) { $id = 17; $query = "model_id, city_id from model_in_city WHERE model_id = $id AND city_display = 1"; if ($result = $db->query($query)) { echo "<select name=select multiple>"; while ($cityrow = $city_result->fetch_assoc()) { echo "<option value=" . $cityrow['city_id'] mysql_data_seek($result,0); while($model_city=mysql_fetch_array($result)) { if($model_city['city_id']==$cityrow['city_id']) { echo " selected "; } } echo ">" . $cityrow['city_name'] . "</option>"; } echo "</select>"; $result->free(); } $db->close(); ?>
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#32 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,115
|
I am missing a close tag on one of the ifs in there, sorry about that.
Code:
<?php include("../includes/connect.php"); $query="select city_id, city_name from city order by city_name"; if ($city_result = $db->query($query)) { $id = 17; $query = "model_id, city_id from model_in_city WHERE model_id = $id AND city_display = 1"; if ($result = $db->query($query)) { echo "<select name=select multiple>"; while ($cityrow = $city_result->fetch_assoc()) { echo "<option value=" . $cityrow['city_id'] mysql_data_seek($result,0); while($model_city=mysql_fetch_array($result)) { if($model_city['city_id']==$cityrow['city_id']) { echo " selected "; } } echo ">" . $cityrow['city_name'] . "</option>"; } echo "</select>"; $result->free(); } $cityresult->free(); } $db->close(); ?>
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#33 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,115
|
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 //error_reporting(0); $dbhost="XXXXXXX"; $dbuser="XXXXXXX"; $dbpass="XXXXXXX"; $db = mysql_connect($dbhost, $dbuser, $dbpass); $dbname="select_test"; $selected=mysql_select_db($dbname); $query="select city_id, city_name from city order by city_name"; $city_result=mysql_query($query,$db); if ($city_result) { $id = 1; $query = "select model_id, city_id from model_in_city WHERE model_id = $id AND city_display = 1"; $modelresult=mysql_query($query, $db); if ($modelresult) { echo "<select name=select multiple size=8>"; while ($cityrow = mysql_fetch_array($city_result)) { echo "<option value=" . $cityrow['city_id']; mysql_data_seek($modelresult,0); while($model_city=mysql_fetch_array($modelresult)) { if($model_city['city_id']==$cityrow['city_id']) { echo " selected "; } } echo ">" . $cityrow['city_name'] . "</option>"; } echo "</select>"; } else { echo "no model result mysql error=" . mysql_error() . "<br>\n"; } } else { echo "no city result mysql error=" . mysql_error() . "<br>\n"; } ?>
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#34 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,115
|
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 //error_reporting(0); $dbhost = "XXXXX"; $dbuser = "XXXXX"; $dbpass = "XXXXX"; $db = mysql_connect($dbhost, $dbuser, $dbpass); $dbname="select_test"; $selected=mysql_select_db($dbname); // for testing $id = 1; $query ="select a.city_id, a.city_name, b.city_display from city a "; $query .="left outer join model_in_city b on a.city_id=b.city_id and b.city_display>0 and b.model_id=" . $id . " "; $query .="order by a.city_name"; $city_result=mysql_query($query,$db); if ($city_result) { echo "<select name=select multiple size=8>"; while ($cityrow = mysql_fetch_array($city_result)) { echo "<option value=" . $cityrow['city_id']; if($cityrow['city_display']==1) { echo " selected "; } echo ">" . $cityrow['city_name'] . "</option>"; } echo "</select>"; } else { echo "no city result mysql error=" . mysql_error() . "<br>\n"; } ?> .
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#35 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,115
|
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 //error_reporting(0); $dbhost = "XXXXX"; $dbuser = "XXXXX"; $dbpass = "XXXXX"; $db = mysql_connect($dbhost, $dbuser, $dbpass); $dbname="select_test"; $selected=mysql_select_db($dbname); $sel=array(); $id = 1; $query="select a.city_id, a.city_name, b.city_display from city a "; $query .="left outer join model_in_city_mod b on a.city_id=b.city_id and b.city_display>0 and b.model_id=" . $id . " "; $query .="order by a.city_name"; $city_result=mysql_query($query,$db); if ($city_result) { for($i=1;$i<=4;$i++) { $sel[$i]="<select name=select[" . $i . "] size=8>\n"; } while ($cityrow = mysql_fetch_array($city_result)) { for($i=1;$i<=4;$i++) { $sel[$i].="<option value=" . $cityrow['city_id']; if($cityrow['city_display']==$i) { $sel[$i] .=" selected "; } $sel[$i].=">" . $cityrow['city_name'] . "</option>\n"; } } for($i=1;$i<=4;$i++) { $sel[$i].="</select>"; } } else { echo "no city result mysql error=" . mysql_error() . "<br>\n"; } for($i=1;$i<=4;$i++) { echo $sel[$i] . "<br>\n<br>\n"; } ?>
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#36 |
Confirmed User
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
|
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. |
![]() |
![]() ![]() ![]() ![]() ![]() |