Disscussion about filling combo box from database in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • punitshrivastava
    New Member
    • Jul 2007
    • 22

    Disscussion about filling combo box from database in php

    Hi to all,
    I am punit.i m working in php.As i m fresher in php .i am using combo box for selection of data & making Report .please tell me that how i fill the combo box dynamically.so please suggest me the code for filling combo dynamically in php
    using database as sql.
    Thanks
    Punit
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Originally posted by punitshrivastav a
    Hi to all,
    I am punit.i m working in php.As i m fresher in php .i am using combo box for selection of data & making Report .please tell me that how i fill the combo box dynamically.so please suggest me the code for filling combo dynamically in php
    using database as sql.
    Thanks
    Punit
    I do something like this, oce I have got the data in an associative array. For help on that please read the article on data abstraction layers.

    [CODE=php]
    // NOTE: $laResults is the associative array returned via data query
    echo "<select name='list1' id='list1'>;
    foreach($laResu lts as $lcDataLine)
    {
    echo "<option value=" ."'" . $lcDataLine['ID'] . "'" ;
    if($lcDataLine['isDefault'])
    // used to indicate a default selection, this is an item in my database
    {
    echo "selected='sele cted'";
    }
    echo ">" . $lcDataLine['description'] ."</option>";
    }
    echo "</select>";
    [/CODE]

    I hope this helps. It works for me, wven producing a country codes drop down list of all the countries in the world in no time at all.

    Have a go and if you get stuck give me a shout.
    nathj
    Last edited by nathj; Jul 24 '07, 02:27 PM. Reason: bad formatting

    Comment

    • punitshrivastava
      New Member
      • Jul 2007
      • 22

      #3
      Hi Nathj,
      I read your reply send it for filling combobox.
      Thanks to you .But i have one problem as i am working in on two form .In one form i create one textbox & combobox incombobox i fill it manually by database feild & in textbox i use ot give entry . i have to do that i take the input from textbox & select the data from database according to combox feild for this i code for first form is :
      <form name="search" method="post" action="select. php">
      Seach for: <input type="text" name="find" /> in
      <select name="field" id="field" >
      <option value="1" >FirstName</option>
      <option value="2" >LastName</option>
      <option value="3" >Age</option>
      </select>
      <input type="hidden" name="searching " value="yes" />
      <input type="submit" name="search" value="select" />
      </form>
      & for searching form i code it as:

      <?php
      if ($searching =="yes")
      {
      echo "<h2>Result s</h2><p>";

      //If they did not enter a search term we give them an error
      if ($find == "")
      {
      echo "<p>You forgot to enter a search term";
      exit;
      }

      $host="localhos t"; // Host name
      $username="root "; // Mysql username
      $password="root "; // Mysql password
      $db_name="proje ctdb"; // Database name
      $tbl_name="pers on"; // Table name

      // Connect to server and select database.
      mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
      mysql_select_db ("$db_name") or die("cannot select DB");
      // We preform a bit of filtering
      $find = $_POST['find'];
      $find = trim ($find);

      $ddl = $_POST['field'];

      // Retrieve data from database
      if($ddl = 'FirstName')
      {
      $sql="SELECT * FROM $tbl_name where FirstName LIKE '%$find%'";
      $result=mysql_q uery($sql);
      }
      elseif($ddl = 'LastName')
      {
      $sql="SELECT * FROM $tbl_name where LastName LIKE '%$find%'";
      $result=mysql_q uery($sql);
      }
      else
      {
      $sql="SELECT * FROM $tbl_name where Age LIKE '%$find%'";
      $result=mysql_q uery($sql);
      }
      // Start looping rows in mysql database.
      while($rows=mys ql_fetch_array( $result))
      {
      ?>
      <table width="400" border="1" cellspacing="0" cellpadding="3" >
      <tr>
      <td width="30%"><? echo $rows['FirstName']; ?></td>
      <td width="30%"><? echo $rows['LastName']; ?></td>
      <td width="30%"><? echo $rows['Age']; ?></td>
      </tr>
      </table>
      <?
      // close while loop
      }
      //This counts the number or results - and if there wasn't any it gives them a little message explaining that
      $anymatches=mys ql_num_rows($re sult);
      if ($anymatches == 0)
      {
      echo "Sorry, but we can not find an entry to match your query<br><br>";
      }
      //And we remind them what they searched for
      echo "<b>Searche d For:</b> " .$find;
      }
      ?>

      it is not executing both else loop.as we cut the else loop it run fine but as use else loop it gives error & also for switch statement.
      Please go through this code and suggest me .As i am new for this php so please suggest me.
      Thanks
      punit
      Originally posted by nathj
      I do something like this, oce I have got the data in an associative array. For help on that please read the article on data abstraction layers.

      [CODE=php]
      // NOTE: $laResults is the associative array returned via data query
      echo "<select name='list1' id='list1'>;
      foreach($laResu lts as $lcDataLine)
      {
      echo "<option value=" ."'" . $lcDataLine['ID'] . "'" ;
      if($lcDataLine['isDefault'])
      // used to indicate a default selection, this is an item in my database
      {
      echo "selected='sele cted'";
      }
      echo ">" . $lcDataLine['description'] ."</option>";
      }
      echo "</select>";
      [/CODE]

      I hope this helps. It works for me, wven producing a country codes drop down list of all the countries in the world in no time at all.

      Have a go and if you get stuck give me a shout.
      nathj
      Last edited by punitshrivastava; Jul 27 '07, 07:28 AM. Reason: editing the dicription

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Originally posted by punitshrivastav a
        [CODE=php]
        <?php
        ....
        // Retrieve data from database
        if($ddl = 'FirstName')
        {
        $sql="SELECT * FROM $tbl_name where FirstName LIKE '%$find%'";
        $result=mysql_q uery($sql);
        }
        elseif($ddl = 'LastName')
        {
        $sql="SELECT * FROM $tbl_name where LastName LIKE '%$find%'";
        $result=mysql_q uery($sql);
        }
        else
        {
        $sql="SELECT * FROM $tbl_name where Age LIKE '%$find%'";
        $result=mysql_q uery($sql);
        }
        ....
        ?>
        [/CODE]
        Hi ,

        I would try replacing the code snippet I quoted above with:

        [CODE=php]
        switch ($ddl)
        {
        case "1": // first name
        $sql="SELECT * FROM $tbl_name where FirstName LIKE '$find'";
        break;
        case "2": // last name
        $sql="SELECT * FROM $tbl_name where LastName LIKE '$find'";
        break ;
        case "3":// age
        $sql="SELECT * FROM $tbl_name where Age LIKE '$find'";
        break;
        }

        if !empty($sql)
        {
        $result=mysql_q uery($sql);
        }
        // if there is a result etc - you can carry on with the rest of your code.
        }
        [/CODE]

        This is waht I would do. Now I'm not saying this will work first time but it's a slicker structure that you should be able to play around with a bit easier.

        If you are not getting the desired results try putting 'echo' lines in the code to indeicate where you are. That way you will get output on the screen indicating where you are at.

        Cheers
        nathj

        Comment

        • punitshrivastava
          New Member
          • Jul 2007
          • 22

          #5
          Thanks Nathj it works
          Punit
          Originally posted by nathj
          Hi ,

          I would try replacing the code snippet I quoted above with:

          [CODE=php]
          switch ($ddl)
          {
          case "1": // first name
          $sql="SELECT * FROM $tbl_name where FirstName LIKE '$find'";
          break;
          case "2": // last name
          $sql="SELECT * FROM $tbl_name where LastName LIKE '$find'";
          break ;
          case "3":// age
          $sql="SELECT * FROM $tbl_name where Age LIKE '$find'";
          break;
          }

          if !empty($sql)
          {
          $result=mysql_q uery($sql);
          }
          // if there is a result etc - you can carry on with the rest of your code.
          }
          [/CODE]

          This is waht I would do. Now I'm not saying this will work first time but it's a slicker structure that you should be able to play around with a bit easier.

          If you are not getting the desired results try putting 'echo' lines in the code to indeicate where you are. That way you will get output on the screen indicating where you are at.

          Cheers
          nathj

          Comment

          • nathj
            Recognized Expert Contributor
            • May 2007
            • 937

            #6
            Originally posted by punitshrivastav a
            Thanks Nathj it works
            Punit
            That's great! All the best with the rest of your project, and remember if you have any more questions just post back here.

            nathj

            Comment

            Working...