Rewrite the code using classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ramu528
    New Member
    • Mar 2008
    • 18

    Rewrite the code using classes

    Hi friends, I am populating a drop box from a table jos_qualificati ons. can anybody tell me how to populate a drop box using classes. something like
    $db->setQuery( $query );

    this is my code which i have written without using classes :::::::::::::
    [php]
    $result = mysql_query( "SELECT category FROM jos_qualificati ons" )
    or die("SELECT Error: ".mysql_error() );
    // start the select list
    echo '<select name="category" id="category">' ;
    echo '<OPTION value="" selected>Select </OPTION>';
    // read each row field and build option list
    while ($row = mysql_fetch_ass oc($result))
    {
    $category=$row['category'];
    print "<option value='$categor y'>$category</option>";
    }
    // close the select list
    print "</select>";
    ?>
    [/php]

    //plz help me out.
    Last edited by ronverdonk; Mar 20 '08, 09:31 AM. Reason: code within tags!!
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      Hi,

      I have an object - called formObject that inturn contains my Data handling object. In my system there are only certain areas where a select box is created from a table.
      [php]
      function createInputSele ct($pnRetreival Type, $pnReturnType, $pcSelectID, $pnSelection, $plIsReadOnly, $pcExtra, $pcClass)
      {
      switch($pnRetre ivalType)
      {
      case 1: // get country codes
      $lcSelectStr =
      "SELECT
      a.ID, a.countryName as description, a.isDefault
      FROM countrycode a
      ORDER BY a.countryName ASC";
      break;
      case 2: // get denomination list
      $lcSelectStr =
      "SELECT
      a.ID, a.denomination as description, 0 as isDefault
      FROM denomination a
      ORDER BY a.denomination ASC";
      break;
      case 3: // get leader role list
      $lcSelectStr =
      "SELECT
      a.ID, a.role as description, 0 as isDefault
      FROM role a
      ORDER BY a.role ASC";
      }

      // execute the select
      $laResults = $this->loDataObject->queryGetData(f alse, $lcSelectStr);
      $lcItemID = !empty($pcSelec tID)? $pcSelectID : "list" . $pnRetreivalTyp e ;
      switch($pnRetur nType)
      {
      case 1: // write the results into a select input object
      $lvReturn = "<select name=" . "'" . $lcItemID . "'" . "id=" . "'" . $lcItemID . "' " ;
      if(!empty($pcEx tra))
      {
      $lvReturn .= $pcExtra . " " ;
      }

      if(!empty($pcCl ass))
      {
      $lvReturn .= "class='" . $pcClass ."' " ;
      }

      $lvReturn.= ">";

      foreach($laResu lts as $lcDataLine)
      {
      $lvReturn .= "<option value=" . $lcDataLine['ID'] . " ";

      if($pnSelection >= 0)
      {
      if($lcDataLine['ID'] == $pnSelection)
      {
      $lvReturn .= "selected='sele cted'";
      }
      }
      else
      {
      if($lcDataLine['isDefault'])
      {
      $lvReturn .= "selected='sele cted'";
      }
      }
      $lvReturn .= ">" . $lcDataLine['description'] ."</option>";
      }
      $lvReturn .= "</select>";
      break;
      case 2: // return the array from the query
      $lvReturn = $laResults ;
      }

      return $lvReturn ;
      } // end of createInputSele ct
      [/php]

      The parameters here control what happens, A standard call for me would look like:
      [php]
      $lcCountrySelec t=$loform->createInputSel ect(1,1,"cbo_co untry", 78, false, "", "")
      [/php]

      This give the return an ID and a name of 'cbo_country' and defaults to selecting United Kingdom.

      I also have functions for generating select controls from ENUM fields and from static lists of data. If you want either of those let me know.

      I have assumed you know how to instantiate an object and so on but if you need some help there give me a shout.

      Cheers
      nathj

      Comment

      • Ramu528
        New Member
        • Mar 2008
        • 18

        #4
        thanks nathj

        I got it. If any probs i will definitely shout for.

        good luck

        ramu

        Comment

        Working...