Separating my options

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lazandra
    New Member
    • Sep 2007
    • 25

    Separating my options

    Hi all

    I have a field in my database that is one long list. I want to put this list into a drop down but have each item in that list its own option.

    This is how its printing at the moment:

    Code:
    <select name="v_options"><option value="">-- Any --</option><option value='34'>Allerton, Bolton, Bolton Woods,Bowling,Bradford Moor,Buttershaw,Clayton,Dudley Hill,East Bierley</option></select>
    Code:
    <select name="v_options"><option value="">-- Any --</option>
    <option value='34'>Allerton, Bolton</option><option value='34'>Bolton Woods</option><option value='34'>Bowling</option><option value='34'>Bradford Moor</option><option value='34'>Buttershaw</option><option value='34'>Clayton</option>Dudley Hill<option value='34'>East Bierley</option></select>
    Does anyone know a function that will separate my options?

    Thanks
    xx
  • Amzul
    New Member
    • Oct 2007
    • 130

    #2
    Originally posted by Lazandra
    Hi all

    I have a field in my database that is one long list. I want to put this list into a drop down but have each item in that list its own option.

    This is how its printing at the moment:

    Code:
    <select name="v_options"><option value="">-- Any --</option><option value='34'>Allerton, Bolton, Bolton Woods,Bowling,Bradford Moor,Buttershaw,Clayton,Dudley Hill,East Bierley</option></select>
    Code:
    <select name="v_options"><option value="">-- Any --</option>
    <option value='34'>Allerton, Bolton</option><option value='34'>Bolton Woods</option><option value='34'>Bowling</option><option value='34'>Bradford Moor</option><option value='34'>Buttershaw</option><option value='34'>Clayton</option>Dudley Hill<option value='34'>East Bierley</option></select>
    Does anyone know a function that will separate my options?

    Thanks
    xx
    you can try explode(), and then make a loop to builed your html code from the array
    post a sample of your db filed value
    i dont think i understood in 100% what u want to do :(

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Yeh, like he/she said.

      Say we have a string (coming from your db), each word is seperated by a space.

      [php]
      $_string = "list of places in string seperated by spaces";
      [/php]
      We'd then explode() the string with a space ' ' as a delimiter, to make turn the string into an array
      [php]
      $_string = explode(" ", $_string);
      [/php]
      Then we'd use a foreach() loop to traverse through the array:
      [php]
      foreach( $_string as $_x )
      {
      echo $_x . "<br />";
      }
      [/php]

      Hope this helps!

      Comment

      • Bosstone
        New Member
        • May 2007
        • 3

        #4
        Hi,

        if the field is in a MySQL DB why not use this to populate your dropdown?

        [PHP]<?
        mysql_select_db ($database, $connect);
        $tablename = "event";

        $abfrage = "SELECT datum, event FROM $tablename ORDER BY datum ASC";
        $ergebnis = mysql_query($ab frage);

        echo '<select size="1" name="datum">';

        while($row = mysql_fetch_obj ect($ergebnis))

        {

        $datum = "$row->datum";
        $event = "$row->event";

        echo '<option value="'.$datum .'">'.$datum. ' : ' .$event.'</option><br>'; // Eleganter
        $event = $_POST['event'];
        $datum = $_POST['datum'];
        }
        echo $datum;
        echo ' </select>';
        ?>[/PHP]

        In this case, I have populated the dropdown with "Datum : Event"

        Hope this helps,

        Best regards,

        Stefan

        Comment

        • Lazandra
          New Member
          • Sep 2007
          • 25

          #5
          Hi guys!

          Thats brilliant! I have used the explode () function and it works perfectly.

          Thanks again

          Laz
          xxx

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by Lazandra
            Hi guys!

            Thats brilliant! I have used the explode () function and it works perfectly.

            Thanks again

            Laz
            xxx
            Welcome!

            Post back again whenever :)

            Comment

            Working...