www.webdeveloper.com
Results 1 to 5 of 5

Thread: Another OOp question

  1. #1
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654

    Another OOp question

    If I create a new instance of the Class below from within another Class by using $category = new Category;
    From the class which is instantiating the Category Class, how could I pass a name to $category (The instance of the class) and get back the id number ($catId)?

    PHP Code:
    <?php
    class Category{

        public 
    $catName;
        public 
    $catId;
        public 
    $catDate;
        private 
    $data = array();

        public function 
    __construct(){
            
    // ADODB connection
            
    require_once( 'includes/adodb5/adodb.inc.php' );
            
    $conn NewADOConnection('mysql');
            
    $conn->Connect("mysql1.100ws.com""gramac4_wiki""xxxxxx""gramac4_wiki");
        }
        
        
    // auto-include any Class files
        
    function __autoload($className){
            require_once 
    $className '.php';
        }

        public function 
    __set($name$value){
            
    $this->data[$name] = $value;
        }
        public function 
    __get($name){
            return 
    $this->data[$name];
        }

        
    // displays html form for input of a new category record
        
    function displayForm(){
            echo 
    '
                <form action="category.php" method="post">
                    <label for="category">Category</label>
                    <input type="text" name="categoryname" /><br />
                    <input name="submit" value="Submit" type="submit" /><input name="reset" value="Reset" type="reset" />
                </form>'
    ;
        }

        
    // given an ID number, sets all objects properties to the corresponding database record's field properies
        
    function load($catId){
            
    $ADODB_FETCH_MODE ADODB_FETCH_ASSOC;
            
    $sql "SELECT * FROM hd_category WHERE c_id = '$catId'";
            
    $result = &$conn->Execute('select CustomerID,OrderDate from Orders'); 
            if (!
    $result)
                print 
    $conn->ErrorMsg(); 
            else 
                while (!
    $result->EOF) { 
                    
    $this->catId $result->fields['c_id'];
                    
    $this->catName $result->fields['c_name'];
                    
    $this->catDate $result->fields['c_date'];
                        
    $result->MoveNext(); 
                }
        }

        
    // saves all objects properties to hd_category table
        
    function save(){
            
    $sql "INSERT INTO hd_category (c_name,c_time) VALUES ('$this->catName', " time() .")";
            if (
    $conn->Execute($sql) === false) { 
                print 
    '<p>error inserting: ' $conn->ErrorMsg();
                echo 
    '<br />' $sql '</p>';
            } 
        }

        
    // given an ID number, sets that databse records properties to the objects properties
        
    function update($catId){
            
    $sql "UPDATE hd_category SET c_name = '$this->catName' WHERE c_id = '$this->catId'";
            if (
    $conn->Execute($sql) === false) { 
                print 
    '<p>error updating: ' $conn->ErrorMsg();
                echo 
    '<br />' $sql '</p>';
            } 
        }

    }

    ?>

  2. #2
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    huh? i don't follow what you're trying to get. can't you just do $category->catId ?
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  3. #3
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    But the category class would have to be passed something to check the database against.
    lets say we have 2 categories in the category table
    c_id:1
    c_name:CONNECT FAIL

    c_id:2
    c_name:CONNECT FAIL

    If, from the class I am instantiating the category class (in this case it's called Incident), I want to find out which c_id relates to the c_name that has been entered by a user, calling $category->catId won't work as the class won't know which ID to return. The class will have to be passed the c_name so that itcan check the database and return the c_id.
    I'm just wondering what the most efficient way of doing this is. I could easily make a single method for this which is passed the name as an argument and then returns $this->catId as this is a small class but the larger it gets I can't cover every eventuality.

    Maybe a method which takes the value and also the mysql column name to check against?

    Feck it I'll give it a bash and see what happens but if anyone is able to decode my ramblings, any input would be appreciated (Just like yours was Chazzy! ). It's hard to put this one into words without writing a novel about it.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,145
    Not sure I follow you, but I think maybe what you are asking would be addressed by one of these simplistic examples:
    PHP Code:
    <?php
    class Inner
    {
       public function 
    hello($name)
       {
          echo 
    "Hello, $name!";
       }
    }

    class 
    Outer
    {
       public 
    $inner;
       public function 
    __construct()
       {
          
    $this->inner = new Inner();
       }
    }

    $outer = new Outer();
    $outer->inner->hello('NogDog');
    PHP Code:
    <?php
    class Inner
    {
       protected 
    $name;
       public function 
    __construct($name)
       {
          
    $this->name $name;
       }
       public function 
    hello()
       {
          echo 
    "Hello, " $this->name "!";
       }
    }

    class 
    Outer
    {
       public 
    $inner;
       public function 
    __construct(Inner $inner)
       {
          
    $this->inner $inner;
       }
    }

    $outer = new Outer(new Inner('NogDog'));
    $outer->inner->hello();
    Either one will end up outputting "Hello, NogDog!"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    What I did was created a load($column,$value) method which I can pass the whatever I like, as in id or name or whatever, but also pass the column name from the database. This then does a select * from table where $column = $value. It's a bit messy but it works.
    Thanks for everyones help on this.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles