GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP/MYSQL: form <select> question (https://gfy.com/showthread.php?t=1020432)

eMonk 04-29-2011 08:51 AM

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...

k0nr4d 04-29-2011 09:00 AM

<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>

FlexxAeon 04-29-2011 09:03 AM

Quote:

Originally Posted by k0nr4d (Post 18096219)
<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>

:thumbsup

except you didn't close the php with ?> :winkwink:

eMonk 04-29-2011 09:05 AM

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!

nation-x 04-29-2011 09:08 AM

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>

k0nr4d 04-29-2011 09:22 AM

Quote:

Originally Posted by FlexxAeon (Post 18096228)
:thumbsup

except you didn't close the php with ?> :winkwink:

ahhh yeah sorry, stupid gfy doesn't have syntax coloring in the textbox :1orglaugh

SpotOnTechSupport 04-29-2011 09:22 AM

Gotta make sure the processing code can pull the array as well.

FlexxAeon 04-29-2011 09:25 AM

Quote:

Originally Posted by k0nr4d (Post 18096279)
ahhh yeah sorry, stupid gfy doesn't have syntax coloring in the textbox :1orglaugh

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'

eMonk 04-29-2011 09:39 AM

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++

FlexxAeon 04-29-2011 09:47 AM

Quote:

Originally Posted by eMonk (Post 18096316)
...BTW what do you guys use to code in? Was thinking of using Notepad++

dreamweaver...

:party-smi

eMonk 04-29-2011 09:51 AM

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.

HomerSimpson 04-29-2011 09:54 AM

I use EmEditor, but Notepad++ is also very good..

FlexxAeon 04-29-2011 10:14 AM

Quote:

Originally Posted by eMonk (Post 18096347)
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

Aric 04-29-2011 10:16 AM

Quote:

Originally Posted by FlexxAeon (Post 18096403)
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 :thumbsup

k0nr4d 04-29-2011 10:31 AM

i use dreamweaver, except only the code view. Just force of habit

SpotOnTechSupport 04-29-2011 11:36 AM

Zend Studio 8.

Tempest 04-29-2011 12:59 PM

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.

FlexxAeon 04-29-2011 01:13 PM

"selected" by itself works fine...no less 'proper' than the other way

nation-x 04-29-2011 01:35 PM

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.

glowlite 04-29-2011 02:36 PM

Quote:

Originally Posted by Tempest (Post 18096812)
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. :Oh crap

SpotOnTechSupport 04-29-2011 02:44 PM

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.

eMonk 06-14-2011 07:05 PM

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();

?>


sarettah 06-14-2011 07:14 PM

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.

eMonk 06-14-2011 07:47 PM

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.

sarettah 06-14-2011 08:17 PM

Quote:

Originally Posted by eMonk (Post 18216552)
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

eMonk 06-14-2011 08:32 PM

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();
?>


sarettah 06-14-2011 09:17 PM

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)

V_RocKs 06-14-2011 09:56 PM

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))

eMonk 06-14-2011 10:15 PM

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 06-15-2011 01:30 PM

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.

sarettah 06-15-2011 02:17 PM

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();
?>


sarettah 06-15-2011 02:23 PM

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();
?>


sarettah 06-15-2011 02:57 PM

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";
}
?>


sarettah 06-15-2011 04:36 PM

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.

.

sarettah 06-15-2011 05:18 PM

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";
}
?>


eMonk 06-15-2011 06:12 PM

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