how to fill combobox with dynamic php variables ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • pipesportugal
    Second Lieutenant

    • Jul 2007
    • 130

    how to fill combobox with dynamic php variables ?

    Hello dear colleagues from vodahost forum,

    I would like to have a dynamic visualization in a combobox created at BV software, for that purpose I am placing on both "Item text" and "Value" fields of the "edit item" box, the following code:

    <?php echo $option_one; ?>
    <?php echo $option_two; ?>
    <?php echo $option_three; ?>

    I named the combobox combo_color and at the Tab index placed nothing.

    Then I have created in the same webpage an html box that assigns several values to these variables. I used the following code:

    <?php
    $option_one = 'White';
    $option_two = 'Black';
    $option_three = 'Red';
    ?>

    I also tried to insert this same code in the "page html" option in several different places, start, between..., inside..., beginning...,etc and none of my trials worked.

    Can someone give me some ideas of what am I making wrong ?

    Thx,
    pipesportugal
  • navaldesign
    General & Forum Moderator

    • Oct 2005
    • 12080

    #2
    Re: how to fill combobox with dynamic php variables ?

    Your approach is correct. However, make sure that the html box is, in the page code flow, BEFORE the combobox (and the entire form) itself, otherwise the values don't exist yet when the combobox is created. If necessary right click on the html box and select Move to Back.

    Now, if it doesn't work with the code

    $option_one = "White";
    $option_two = "Black";
    $option_three = "Red";

    placed in the Start of Page,

    make sure that you have set your page to publish as php


    Please also note that this approach is rather static. I mean, you have to define the options statically, and the number of options is static also. Usually, the dynamic population of comboboxes is used to populate it with values that are contained in a database table. Now, since these values can be continously changes, both as values and as number, you need a dynamic approach like the followingexample. Let's say that you have a form that uploads products into a cart database. During upload, you have to define which category the product belongs to. So, you have a table called "categories" in your database, and you have there all your categories. Now, when you upload a product, your script must look into this table, retrieve the category IDs and titles, and present them in the combobox as options. Here is what you do:
    <?
    require ("dbconnect.php");
    $query = "SELECT categoryID, category_title FROM categories ORDER BY category_title ASC";
    $result = mysql_query($query, $db);
    while ($row = mysql_fetch_array($result)) {
    $category_title[] = $row['category_title'];
    $categoryID[] = $row['categoryID'];
    }
    ?>
    <select name="categoryID" size="1" id="Combobox1"
    ><?
    $option = "<option value=\"Please select a Category\">Please select a Category</option> \n";
    for ($i = 0; $i < count($category_title); $i++) {
    $option .= "<option ";
    $option .= "value=\"$categoryID[$i]\">$category_title[$i]</option> \n";
    }
    echo $option;
    ?>
    </select>

    As you can see, there is a first part in blue color. This one retrieves the category titles and IDs from the database (i have included here the code that is required to connect to the database, however, i do not have here the dbconnect file code. It is simply the code necessary to connect to the database)

    Then there is a second part in blue, that is responsible for populating the combobox. The parts in black are autocreated by BV.

    if you are using a BV form, then the first part in blue goes into the object (combobox) Before Tag and the second in the Inside Tag
    Navaldesign
    Logger Lite: Low Cost, Customizable, multifeatured Login script
    Instant Download Cart: a Powerfull, Customized, in site, DB driven, e-products Cart
    DBTechnosystems.com Forms, Databases, Shopping Carts, Instant Download Carts, Loggin Systems and more....
    Advanced BlueVoda Form Processor : No coding form processor! Just install and use! Now with built in CAPTCHA!

    Comment

    • pipesportugal
      Second Lieutenant

      • Jul 2007
      • 130

      #3
      Re: how to fill combobox with dynamic php variables ?

      Hi Naval Design,

      I tried very hard but there was no way I could, at the "inside tag", retrieve the information from inside the array loaded at the "before tag".

      There was also an extra (not needed) "}" in the "inside tag" code, but I removed it.

      This is the code I used:
      BEFORE TAG:
      <?php
      include("include/dbconnect.php");
      $pedido = "SELECT cod_tiposusers, des_tiposusers FROM tabela_tiposusers";
      $resultado_busca = mysql_query($pedido,$db);
      ?>

      INSIDE TAG:
      <?php
      $option = "<option value=\"Seleccione actividade\">Seleccione actividade</option> \n";
      while ($row = mysql_fetch_array($resultado_busca))
      {
      $opcao = $row["des_tiposusers"];
      $option .= "<option ";
      $option .= "value=\"$opcao\">$opcao</option> \n";
      }
      echo $option;
      ?>

      This was the only way I could make it work, many, many hours after I started.
      This a "User type" table with 2 fields only, "User number" and "User description" and I am interested in displaying in the combobox the description only.

      I imagine that You made copy/paste of the code You've put at this thread before, but I haven't been able to make it work. There was no way, I couldn retrieve in the "INSIDE TAG" the array that I loaded at the "BEFORE TAG".

      This routine does work in Your program, right ?

      Thank You so much for all the help,

      pipesportugal

      Comment

      • navaldesign
        General & Forum Moderator

        • Oct 2005
        • 12080

        #4
        Re: how to fill combobox with dynamic php variables ?

        Yes, i took the code from one of my scripts, and is perfectly working. Make it:

        <?
        require ("dbconnect.php");
        $query = "SELECT des_tiposusers FROM tabela_tiposusers";
        $result = mysql_query($query, $db);
        while ($row = mysql_fetch_array($result)) {
        $des_tiposusers[] = $row['des_tiposusers'];
        }
        ?>

        and


        ><?
        $option = "<option value=\"Seleccione actividade\">Seleccione actividade</option> \n";
        for ($i = 0; $i < count($des_tiposusers); $i++) {
        $option .= "<option ";
        $option .= "value=\"$des_tiposusers[$i]\">$des_tiposusers[$i]</option> \n";
        }
        echo $option;
        ?



        For before Tag and Inside Tag of the Combobox

        be careful to the >< at the start and the missing > at the end of the second code.

        I prefer storing the values in an array and not echoing them directly from the database as this allowes me to make particular validations / checks on the values, if necessary, before feeding them into the combobox.
        Navaldesign
        Logger Lite: Low Cost, Customizable, multifeatured Login script
        Instant Download Cart: a Powerfull, Customized, in site, DB driven, e-products Cart
        DBTechnosystems.com Forms, Databases, Shopping Carts, Instant Download Carts, Loggin Systems and more....
        Advanced BlueVoda Form Processor : No coding form processor! Just install and use! Now with built in CAPTCHA!

        Comment

        • pipesportugal
          Second Lieutenant

          • Jul 2007
          • 130

          #5
          Re: how to fill combobox with dynamic php variables ?

          Originally posted by navaldesign View Post
          be careful to the >< at the start and the missing > at the end of the second code.

          I prefer storing the values in an array and not echoing them directly from the database as this allowes me to make particular validations / checks on the values, if necessary, before feeding them into the combobox.
          Hi Naval Design,

          Those particular details of the >< and the other one of the "close php tag" without the >, have passed completed aside from my eyes.

          In fact it works as it should with the great great advantage of being able to test the values of the arrays.

          This was Precious help,

          Thank You again and till next time....

          pipesportugal

          Comment

          • Jackaal
            Private

            • Jun 2008
            • 4

            #6
            Re: how to fill combobox with dynamic php variables ?

            I am a novice at this sort of thing but i have learnt a great deal from this post. I have adapted the code you offered and it seems to work by adding as many entries into the combo box as there are in the database however, they appear blank. Any help would be much appreciated.

            Example

            <?
            require ("actdb.php");
            $query = "SELECT name FROM combatant_table WHERE damage!=0 AND ally='T'";
            $result = mysql_query($query);
            while ($row = mysql_fetch_array($result)) {
            $name[] = $row['name'];
            }

            ?>
            <select name="name" size="1" id="Combobox1">
            <?
            $option = "<option value=\"Please select a Category\">Please select a Category</option> \n";
            for ($i = 0; $i < count($name); $i++) {
            $option .= "<option ";
            $option .= "value=\"$name[$i]\">$name[$i]</option> \n"; }
            echo $option;
            ?>
            </select>

            Comment

            • Jackaal
              Private

              • Jun 2008
              • 4

              #7
              Re: how to fill combobox with dynamic php variables ?

              I have also adapted using the two variables for the sake of the exercise and get:

              Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/.lazar/jackaal/aocmaps.threedd.com/aocparse/test.php on line 4

              Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/.lazar/jackaal/aocmaps.threedd.com/aocparse/test.php on line 5

              <?
              require ("actdb.php");
              $query = "SELECT DISTINCT encid, name FROM combatant_table WHERE damage>0 AND ally='T'";
              $result = mysql_query($query, $db);
              while ($row = mysql_fetch_array($result)) {
              $name[] = $row['name'];
              $encid[] = $row['encid'];
              }
              ?>
              <select name="name" size="1" id="Combobox1">
              <?
              $option = "<option value=\"Please select a Category\">Please select a Category</option>";
              for ($i = 0; $i < count($name); $i++) {
              $option .= "<option ";
              $option .= "value=\"$encid[$i]\">$name[$i]</option> \n";
              }
              echo $option;
              ?>
              </select>

              Comment

              • navaldesign
                General & Forum Moderator

                • Oct 2005
                • 12080

                #8
                Re: how to fill combobox with dynamic php variables ?

                The options appear blank because you have no "option" echoed! See part in red i added in your post
                Navaldesign
                Logger Lite: Low Cost, Customizable, multifeatured Login script
                Instant Download Cart: a Powerfull, Customized, in site, DB driven, e-products Cart
                DBTechnosystems.com Forms, Databases, Shopping Carts, Instant Download Carts, Loggin Systems and more....
                Advanced BlueVoda Form Processor : No coding form processor! Just install and use! Now with built in CAPTCHA!

                Comment

                • Jackaal
                  Private

                  • Jun 2008
                  • 4

                  #9
                  Re: how to fill combobox with dynamic php variables ?

                  Ah sensational. To be honest that particular line didn't quite click with me until I read it with your correction. Many thanks for your help. It would seem I have a long way to go!

                  Comment

                  • Jackaal
                    Private

                    • Jun 2008
                    • 4

                    #10
                    Re: how to fill combobox with dynamic php variables ?

                    I do feel a little guilty asking these questions but after 2 days I'm considering that I may not be up for this. Anyway... I shall ask.

                    After progressive reading I've delved into building this over two php files. The end result should be to see the results of a table pertaining to a particular selection. It all goes well up until it is required to display the form and it appears blank.

                    Here are the files:

                    Code:
                    <?
                    require ("actdb.php");
                    $query = "SELECT DISTINCT name FROM combatant_table WHERE damage!=0 AND ally='T'";
                    $result = mysql_query($query);
                    while ($row = mysql_fetch_array($result)) {
                    $name[] = $row['name'];
                    }
                    
                    ?>
                    <form action="encounter_table2.php" name="name" method="post">
                    <select name="name" size="1" id="name">
                    <?
                    $option = "<option value=\"Please select a Category\">Please select a Category</option> \n";
                    for ($i = 0; $i < count($name); $i++) {
                    $option .= "<option ";
                    $option .= "value=\"$name[$i]\">$name[$i]</option> \n"; }
                    echo $option;
                    ?>
                    </select>
                    <input type="submit" value="submit" name="submit">
                    </form>
                    Which should all output to:

                    Code:
                    <?php
                    include 'actdb.php';
                    $toon_name=$_GET["name"];
                    $query  = "SELECT * FROM encounter_table WHERE name=" . $toon_name;
                    $result = mysql_query($query);
                    $name[] = $row['name'];
                    
                    echo "<table border='1'>";
                    echo "<tr><th>Title</th><th>Start</th><th>End</th><th>Duration</th><th>Damage</th><th>ExtDPS</th><tr>";
                    
                        $escapedurl = htmlentities("combatant_table.php?encid=$row[0]", ENT_QUOTES);
                        
                        echo "<tr>" .
                        "<td><a href='$escapedurl'>$name[1]</a></td>" .
                        "<td>$name[2]</td>" .
                        "<td>$name[3]</td>" .
                        "<td>$name[4]</td>" .
                        "<td>$name[5]</td>" .
                        "<td>$name[6]</td>" .
                        "</tr>";
                    
                    echo "</table>";
                    ?>
                    I think I'm at the point where my lack of experience has me totally stumped. Any help will be much appreciated.

                    Comment

                    • navaldesign
                      General & Forum Moderator

                      • Oct 2005
                      • 12080

                      #11
                      Re: how to fill combobox with dynamic php variables ?

                      Your second listing is wrong:

                      <?php
                      include 'actdb.php';
                      $toon_name=$_GET["name"];
                      $query = "SELECT * FROM encounter_table WHERE name=" . $toon_name;
                      $result = mysql_query($query);
                      $name[] = $row['name'];

                      What's the part in red for (which, in any case, is wrong ) ?

                      I mean, you use SELECT * which means that you are selecting ALL columns from your table. At this point, you should have something like
                      $row = mysql_fetch_array($result);
                      $name = $row['name'];
                      $field2 = $row[field2'];
                      etc................................
                      }

                      So you put ALL the table data in arrays. Then you should echo the results in the table like

                      echo "<table border='1'>";
                      echo "<tr><th>Title</th><th>Start</th><th>End</th><th>Duration</th><th>Damage</th><th>ExtDPS</th><tr>";

                      $escapedurl = htmlentities("combatant_table.php?encid=$row[0]", ENT_QUOTES); // I don't know what you want to do here, neither where did you get $row[0] from

                      echo "<tr>" .
                      "<td><a href='$escapedurl'>$name</a></td>" .
                      "<td>$field2</td>" .
                      "<td>$field3td>" .
                      etc................
                      "</tr>";

                      echo "</table>";
                      Navaldesign
                      Logger Lite: Low Cost, Customizable, multifeatured Login script
                      Instant Download Cart: a Powerfull, Customized, in site, DB driven, e-products Cart
                      DBTechnosystems.com Forms, Databases, Shopping Carts, Instant Download Carts, Loggin Systems and more....
                      Advanced BlueVoda Form Processor : No coding form processor! Just install and use! Now with built in CAPTCHA!

                      Comment

                      Working...
                      X