SitePoint Sponsor

User Tag List

Results 1 to 18 of 18

Thread: My first class

  1. #1
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My first class

    I am trying to write my first class. For the first function in the class, it grabs all information in that row from the db.

    example:
    PHP Code:
    function grab($id) {
    // sql to grab info @ id

    $r mysql_fetch_array($sql);
    $name $r['name']; 
    My problem is I have a long list of variables like $name, email, etc. How can I return the whole list of variables back to the script when called:
    PHP Code:
    $sig->grab("1");

    echo 
    $name
    I may be going at this the wrong way. Pretty lost here
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  2. #2
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function grab($id) { 
    // sql to grab info @ id 

    $r mysql_fetch_array($sql); 
    return 
    $r;


    thennn

    PHP Code:
    $dbRow $sig->grab("1"); 
    echo 
    $dbRow['name']; 
    echo 
    $dbRow['email']; 
    orrr

    PHP Code:
    $dbRow $sig->grab("1"); 

    foreach(
    $dbRow as $k=>$v){
           $
    $k $v;
    }
    echo 
    $name;
    echo 
    $email

  3. #3
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd just return $r and use it directly. i.e instead of creating new variables $name, $email etc just use $r['name'], $r['email']
    In reality though you probably wouldn't want to class to hand off control of the variables it's responsible for, i.e if you want to use/access 'name' and 'email' then you do it via the class </bad explanation>

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Make sure that when you're programming a class, and values should be pulled in from methods, you check the values to be returned, especially when doing a database query - you will most likely want to return a boolean False when something goes wrong.


    In your class:
    PHP Code:
    function grab($id) {
      
    // sql to grab info @ id
      
    $sql "SQL code here";
      
    $sqlRes mysql_query($sql);
      
    $result mysql_fetch_array($sqlRes);
      if (!
    $result) {
        
    // something went wrong with the query...
        
    return false;
      }
      else {
        
    // everything went fine.
        
    return $result;
      }

    In your script where you want to pull the value:
    PHP Code:
    // grab the sig for given id
    $arrSig $sig->grab('1'); // should this be an int ?

    // reference the variables from the array as this will be 
    // safer than pulling them all in as global vars.
    print 'Name: ' $arrSig['name'] . "<br/>\n";
    print 
    'Email: ' $arrSig['email'] . "<br/>\n"
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  5. #5
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I had just a general statement in there and I wanted to pull, say 20 variables from the same row, would that query get executed 20 times, or just once? Referring to John's code. The point of having all the variables in the function was to not have so much duplicate code, but what was said earlier makes sense
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  6. #6
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I had just a general statement in there and I wanted to pull, say 20 variables from the same row, would that query get executed 20 times, or just once?
    Just the once. Queries (for the most part) operate on rows. Unless you're doing some funky recursive function, most values can be retrieved in the one query.

  7. #7
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I would need to use something like this:
    PHP Code:
        function sigInfo($id) {

    $sql mysql_query("SELECT count(*) as exist FROM info WHERE id = '$id'");
    $exist mysql_num_rows($sql);

    if(
    $exist 1) {
        
    $this->errors[] = "ERROR: There was an error finding a signature at that ID";
        return 
    false;
    } else {
        
    $sql mysql_query("SELECT * FROM info WHERE id = '$id'");
        
    $r mysql_fetch_array($sql);
      if (!
    $r) {
        return 
    false;
      }  else {
        return 
    $r;
      } 
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  8. #8
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It that example, no, you just need the one query:
    PHP Code:
    function sigInfo($id)
    {
      
    $sql mysql_query("SELECT * FROM info WHERE id = '$id'") or die(mysql_error());  
      if(!
    mysql_num_rows($sql))
      {
        
    $this->errors[] = "ERROR: There was an error finding a signature at that ID";
        return 
    false;
      }
      return 
    mysql_fetch_array($sql);


  9. #9
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way, i'd recommend having a read of this:
    http://www.sitepoint.com/forums/show...8&postcount=14

    You might not understand all the concepts he talks about but it's worth having that post to hand before you stray too far over to the dark side of 'a class is a bunch of related functions'

  10. #10
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats a great post. I have one more question. Within the class, how would I use the values grabbed from the db?

    i.e:
    PHP Code:
    // in a file
    $sig = new drawSig;
    $sig->render("1");

    ////////////////////////
    //in the class

        
    function sigInfo($id) {
            
    $sql mysql_query("SELECT * FROM info WHERE id = '$id'");
            if(
    $sql) {
                if(!
    mysql_num_rows($sql)) {
                    
    $this->errors[] = "ERROR: There was an error finding a signature at that ID";
                    return 
    false;
                }
                return 
    mysql_fetch_array($sql);
            } else {
                
    $this->errors[] = "ERROR: There was a problem querying the database.";
            }
        }

    function 
    render($id) {
    // render code using previously gathered variables, $name, $email, & others

    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  11. #11
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // in a file
    $sig = new drawSig;
    $dbInfo $sig->sigInfo("1");
    $sig->render($dbInfo['name'],$dbInfo['emai']);

    //in the class

    function sigInfo($id) {
    // ....
    }

    function 
    render($name$email) {
    echo 
    $name;
    echo 
    $email;

    nice ninja edit...

    PHP Code:
    // in a file
    $sig = new drawSig;
    $dbInfo $sig->sigInfo("1");
    $sig->render($dbInfo);

    ////////////////////////
    //in the class

        
    function sigInfo($id) {
            
    $sql mysql_query("SELECT * FROM info WHERE id = '$id'");
            if(
    $sql) {
                if(!
    mysql_num_rows($sql)) {
                    
    $this->errors[] = "ERROR: There was an error finding a signature at that ID";
                    return 
    false;
                }
                return 
    mysql_fetch_array($sql);
            } else {
                
    $this->errors[] = "ERROR: There was a problem querying the database.";
            }
        }

    function 
    render($dbInformation) {
    echo 
    $dbInformation['email'];
    echo 
    $dbInformation['name'];


  12. #12
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah ok, so it has to be done in the file that calls the class, and not in the class itself. Thanks
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  13. #13
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, no. i was just going off of what you had already.

    if you want to do it in the class, which is the whole point of HAVING classes, you would do something like this...

    PHP Code:
    // in a file
    $sig = new drawSig;
    $sig->sigInfo("1");
    $sig->render();

    //in the class

    class drawSig {
        var 
    $dbInfo;
        function 
    sigInfo($id) {
            
    // ....
            
    $this->dbInfo $r;
            return 
    $r;
        }
        
        function 
    render() {
            echo 
    $this->dbInfo['email'];
            echo 
    $this->dbInfo['name'];
        }


  14. #14
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have one more question I have a function inside the class that grabs information and parses it, splitting each variable with a '~', which is broken up using list() & split(). The only way I can use this would be to return that info in an array correct?

    PHP Code:
    list($char, , $server, , $alignment, , $created, , , $race, , $class, , $level, , $artclass, , $artlvl, , , $lastd, , $deathby, , $deathloc, , , , $guildname$health, , $power, , $attack, , $defense, , , $str, , $agi, , $sta, , $int, , $wis, , , $coldr, , $heatr, , $poisonr, , $diseaser, , $magicr, , $divr, , $mentr) = split("~"$data_main); 
    The skipped areas ($name, , $bla) are unused items from the data.
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  15. #15
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could either return it using the function in an array, or save it to a class variable, then retrieve it later on in your script...

    soo like...

    class foo {
    var $infoArray = array();
    function bar(){
    //makes big array
    $this->infoArray = $localArray;
    }
    }

    thennn
    $foo = new foo();
    $foo->bar();
    $newArray = $foo->infoArray;


    but you are also correct, in that the only way to return a large amount of variables is to use an array

  16. #16
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Those variables are going to be used later in the class. I don't really have use for them elsewhere. How do I save them to a class variable usable in a later function within the class?

    I would need to do this in the list, correct? :
    PHP Code:
    list($info['foo'], $info['foo2'])//... etc 
    s c r i p t s f o r y o u . n e t
    ScriptsForYou

  17. #17
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im not completely sure of how list() works, but you would do something like this...

    PHP Code:
    list($this->info['foo'], $this->info['foo2']); 
    someone correct me if i'm wrong

  18. #18
    SitePoint Guru godsfshrmn's Avatar
    Join Date
    Mar 2001
    Posts
    671
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So when using $this->array['item']

    Can be called in any function within the class as $array['item']?
    s c r i p t s f o r y o u . n e t
    ScriptsForYou


Bookmarks

Posting Permissions

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