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.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 04-29-2011, 08:51 AM   #1
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
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">
<option>Apples</option>
<option>Bananas</option>
<option>Oranges</option>
</select>
I was thinking something like:

Code:
<select name="favourite_fruit">
<option selected><?= $fruit ?></option>
<option>Bananas</option>
<option>Oranges</option>
</select>
But don't think the unselected fruits would show up in the select menu...
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:00 AM   #2
k0nr4d
Confirmed User
 
k0nr4d's Avatar
 
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>
k0nr4d is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:03 AM   #3
FlexxAeon
Confirmed User
 
FlexxAeon's Avatar
 
Industry Role:
Join Date: May 2003
Posts: 3,765
Quote:
Originally Posted by k0nr4d View Post
<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>


except you didn't close the php with ?>
__________________
flexx [dot] aeon [at] gmail

Last edited by FlexxAeon; 04-29-2011 at 09:05 AM..
FlexxAeon is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:05 AM   #4
eMonk
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!
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:08 AM   #5
nation-x
Confirmed User
 
nation-x's Avatar
 
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
I like ternaries
Quote:
<select name="favourite_fruit">
<option<?php echo ($row[favorite_fruit] == 'Apples')?' selected':''; ?>>Apples</option>
<option<?php echo ($row[favorite_fruit] == 'Bananas')?' selected':''; ?>>Bananas</option>
<option<?php echo ($row[favorite_fruit] == 'Oranges')?' selected':''; ?>>Oranges</option>
</select>
nation-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:22 AM   #6
k0nr4d
Confirmed User
 
k0nr4d's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,229
Quote:
Originally Posted by FlexxAeon View Post


except you didn't close the php with ?>
ahhh yeah sorry, stupid gfy doesn't have syntax coloring in the textbox
k0nr4d is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:22 AM   #7
SpotOnTechSupport
Confirmed User
 
SpotOnTechSupport's Avatar
 
Industry Role:
Join Date: Mar 2011
Location: Mansfield, Ohio
Posts: 119
Gotta make sure the processing code can pull the array as well.
__________________
'cuz you're too busy dealing with other stuff to mess around with bad hosting!
Prices start at $2.99 for shared hosting, $80 for managed dedicated!
SpotOnTechSupport is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:25 AM   #8
FlexxAeon
Confirmed User
 
FlexxAeon's Avatar
 
Industry Role:
Join Date: May 2003
Posts: 3,765
Quote:
Originally Posted by k0nr4d View Post
ahhh yeah sorry, stupid gfy doesn't have syntax coloring in the textbox
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
FlexxAeon is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:39 AM   #9
eMonk
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++

Last edited by eMonk; 04-29-2011 at 09:41 AM..
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:47 AM   #10
FlexxAeon
Confirmed User
 
FlexxAeon's Avatar
 
Industry Role:
Join Date: May 2003
Posts: 3,765
Quote:
Originally Posted by eMonk View Post
...BTW what do you guys use to code in? Was thinking of using Notepad++
dreamweaver...

__________________
flexx [dot] aeon [at] gmail
FlexxAeon is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:51 AM   #11
eMonk
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.
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 09:54 AM   #12
HomerSimpson
Too lazy to set a custom title
 
HomerSimpson's Avatar
 
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:
HomerSimpson is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 10:14 AM   #13
FlexxAeon
Confirmed User
 
FlexxAeon's Avatar
 
Industry Role:
Join Date: May 2003
Posts: 3,765
Quote:
Originally Posted by eMonk View Post
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.
they'll help even as a noob. especially with things like coloring (which will help when you forget or overlook proper syntax) and function libraries
__________________
flexx [dot] aeon [at] gmail
FlexxAeon is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 10:16 AM   #14
Aric
Confirmed User
 
Aric's Avatar
 
Industry Role:
Join Date: Sep 2002
Posts: 1,209
Quote:
Originally Posted by FlexxAeon View Post
they'll help even as a noob. especially with things like coloring (which will help when you forget or overlook proper syntax) and function libraries
As well as bracket matching
__________________
Awesome cloud hosting by DigitalOcean
Aric is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 10:31 AM   #15
k0nr4d
Confirmed User
 
k0nr4d's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,229
i use dreamweaver, except only the code view. Just force of habit
k0nr4d is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 11:36 AM   #16
SpotOnTechSupport
Confirmed User
 
SpotOnTechSupport's Avatar
 
Industry Role:
Join Date: Mar 2011
Location: Mansfield, Ohio
Posts: 119
Zend Studio 8.
__________________
'cuz you're too busy dealing with other stuff to mess around with bad hosting!
Prices start at $2.99 for shared hosting, $80 for managed dedicated!
SpotOnTechSupport is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 12:59 PM   #17
Tempest
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.
Tempest is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 01:13 PM   #18
FlexxAeon
Confirmed User
 
FlexxAeon's Avatar
 
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
FlexxAeon is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 01:35 PM   #19
nation-x
Confirmed User
 
nation-x's Avatar
 
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.
nation-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 02:36 PM   #20
glowlite
Confirmed User
 
Industry Role:
Join Date: Dec 2005
Location: Arizona/Indiana
Posts: 366
Quote:
Originally Posted by Tempest View Post
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.
True, selected="selected" is always correct and a good coding habit, just using "selected" is sometimes correct.

There's a huge % of pages that don't even declare DOCTYPE though.
glowlite is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-29-2011, 02:44 PM   #21
SpotOnTechSupport
Confirmed User
 
SpotOnTechSupport's Avatar
 
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.
__________________
'cuz you're too busy dealing with other stuff to mess around with bad hosting!
Prices start at $2.99 for shared hosting, $80 for managed dedicated!
SpotOnTechSupport is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-14-2011, 07:05 PM   #22
eMonk
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();

?>
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-14-2011, 07:14 PM   #23
sarettah
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();
?>
That fixes your dropdown issue.
__________________
All cookies cleared!

Last edited by sarettah; 06-14-2011 at 07:15 PM..
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-14-2011, 07:47 PM   #24
eMonk
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.
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-14-2011, 08:17 PM   #25
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,115
Quote:
Originally Posted by eMonk View Post
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.
What does your model_in_city table look like? (if you don't mind me asking of course) ;p
__________________
All cookies cleared!
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-14-2011, 08:32 PM   #26
eMonk
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_id = the id of the city. They are displayed in this table by number.. eg 44 = Toronto, etc
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();
?>
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-14-2011, 09:17 PM   #27
sarettah
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>";
Now, I am not sure what you are trying to do but you said " incorporate the selected value into the form:". Which selected value? The city that was already selected somewhere? You are losing me there, maybe just too late in the night for me..lol.

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>";
  }
Something like that. (edited in: that assumes that you already pulled the city_id coming in into $idin)
__________________
All cookies cleared!

Last edited by sarettah; 06-14-2011 at 09:19 PM..
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-14-2011, 09:56 PM   #28
V_RocKs
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))
V_RocKs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-14-2011, 10:15 PM   #29
eMonk
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?
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-15-2011, 01:30 PM   #30
eMonk
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();
?>
Now, how can I echo the selected value if the option value = $city_id_2

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.
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-15-2011, 02:17 PM   #31
sarettah
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!
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-15-2011, 02:23 PM   #32
sarettah
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!

Last edited by sarettah; 06-15-2011 at 02:25 PM..
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-15-2011, 02:57 PM   #33
sarettah
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!

Last edited by sarettah; 06-15-2011 at 03:01 PM..
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-15-2011, 04:36 PM   #34
sarettah
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";
}
?>
You said on yours that it was not marking the options as selected. When you get back, post up the actual code you have so we can take a look.

.
__________________
All cookies cleared!

Last edited by sarettah; 06-15-2011 at 04:41 PM..
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-15-2011, 05:18 PM   #35
sarettah
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!
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 06-15-2011, 06:12 PM   #36
eMonk
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.
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.