PHP question, OOP select class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbdg
    Super MURCer
    • Sep 1999
    • 1030

    #1

    PHP question, OOP select class

    Messing with classes in PHP 4, very new to OOP, anyway I have created the following select class but for some reason I have to manually add the query to the constructor other wise it doesn't work.

    PHP Code:
    // Select Field (Drop Down)
    class form_select_menu {
        // Properties
        var $name; // Name
        var $cssclass; // Css Style
        var $query; // sql query
        var $opvalue; // db field
        var $opname; // db field
       // Constructor
       function form_select_menu() {
          $this->query = "SELECT ID, Name FROM tblartists ORDER BY Name ASC LIMIT 10";
          $this->rQuery = mysql_query($this->query) or die(mysql_error());
          $this->row_rQuery = mysql_fetch_assoc($this->rQuery);
        }
       // Output
       function display() {
          echo "<select name=\"" . $this->name . "\" class=\"" . $this->cssclass . "\">\n";
           do {
               echo "<option value=\"" . $this->row_rQuery[$this->opvalue] . "\">" . $this->row_rQuery[$this->opname] . "</option>\n";
            } while ($this->row_rQuery = mysql_fetch_assoc($this->rQuery));
            echo "</select>\n";
        }
    } 
    
    $this->query = "SELECT ID, Name FROM tblartists ORDER BY Name ASC LIMIT 10"; is the line that is causing problems as I don't see why I should have to pass it (sort of defeats the point), if I don't include it I get query empty.

  • TnT
    Super MURCer
    • Sep 1999
    • 1644

    #2
    Maybe I'm dense, but I'm not understanding your problem. You don't know why you need to have the $this->query line in the constructor?
    Gigabyte GA-K8N Ultra 9, Opteron 170 Denmark 2x2Ghz, 2 GB Corsair XMS, Gigabyte 6600, Gentoo Linux
    Motion Computing M1400 -- Tablet PC, Ubuntu Linux

    "if I said you had a beautiful body would you take your pants off and dance around a bit?" --Zapp Brannigan

    Comment

    • dbdg
      Super MURCer
      • Sep 1999
      • 1030

      #3
      No, I know I need $this->$query in the constructor but later in the page when I define a new class I do set it (shown below). The thing is that in the class defintion I shouldn't have to re add the sql, won't work for different queries.

      $form_select_menu = new form_select_menu;
      $form_select_menu->query = "SELECT ID, Name FROM tblartists ORDER BY Name ASC LIMIT 10";
      $form_select_menu->display();

      If I use the following, without the constructor it works fine.

      PHP Code:
      class form_select {
          // Properties
          var $name; // Name
          var $cssclass; // Css Style
          var $query; // sql query
          var $opvalue; // db field
          var $opname; // db field
      
         function display() {
            $this->rQuery = mysql_query($this->query) or die(mysql_error());
            $this->row_rQuery = mysql_fetch_assoc($this->rQuery);
            echo "<select name=\"" . $this->name . "\" class=\"" . $this->cssclass . "\">\n";
             do {
                 echo "<option value=\"" . $this->row_rQuery[$this->opvalue] . "\">" . $this->row_rQuery[$this->opname] . "</option>\n";
              } while ($this->row_rQuery = mysql_fetch_assoc($this->rQuery));
              echo "</select>\n";
          }
      } 
      

      Comment

      Working...