#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    North Carolina
    Posts
    187
    Rep Power
    22

    Automatically creating class variables


    I am working on an online CV creation program. There are several tables, e.g. Personal details, educational details. Each of these tables contains many fields.

    I want to write a class that will automatically extract the variables from the tables and give each variable a name that corresponds to the name of the column which it was extracted from.
    e.g. if a value of 'Conor' was extracted from the column memberName then a new variable $memberName would have the value of 'Conor'. I use the extract of this purpose.

    The problem is that I have no way of referring to the variables obtained by the extract() function outside of the class. I don't want to have to declare each variable (There are about 5 tables and many variables for each table). Is there any way of automatically making the variables obtained by the extract() function class variables?

    BTW, this is my first time using OO in PHP!

    If you don't know what I am talking about I have included a snippet of what I have so far :

    ///////////////////START OF SAMPLE

    Class SampleClass {

    function SampleClass($table)
    {

    $this->table = $table;
    $this->theQuery();

    }

    function theQuery()
    {

    $query = "SELECT * FROM $this->table";

    $db = mysql_connect('theHost', 'userName', 'passWord') or die("couldn't connect to database");
    $connect = mysql_select_db('dbName',$db) or die("couldn't select db");
    $result = mysql_query($query,$db) or die("Query unsuccessful");

    while ($row = mysql_fetch_assoc($result)) {
    extract($row);
    }

    }

    } // end class


    $firstGroupOfVariables = new SampleClass('personalDetailsTable');
    echo $firstName;
    echo $surName;

    $secondGroupOfVariables = new SampleClass('educationalDetailsTable');
    echo $employerName;
    echo $dutiesInclude;

    //////////////////////END OF SAMPLE
  2. #2
  3. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,196
    Rep Power
    2268
    You're better off either returning the value of $result from your function, and parsing it the way you need to w/in the rest of your code (outside the class), or returning an array w/ the values in it, which you can then parse or extract outside the class.

    PHP Code:
    <?
    // return result
    function theQuery()
    {
        
    $query "SELECT * FROM $this->table";
        
    $db mysql_connect('theHost''userName''passWord') or die("couldn't connect to database");
        
    $connect mysql_select_db('dbName',$db) or die("couldn't select db");
        
    $result mysql_query($query,$db) or die("Query unsuccessful");
        return 
    $result;

    // use it
    $result        theQuery();
    $numres        count($result);
    for(
    $i 0$i $numres$i++)
    {
        
    $row     mysql_fetch_assoc($result);
        
    extract $row;
    }
    // return array of values
    function theQuery()
    {
        
    $output_array    = array();
        
    $query "SELECT * FROM $this->table";
        
    $db mysql_connect('theHost''userName''passWord') or die("couldn't connect to database");
        
    $connect mysql_select_db('dbName',$db) or die("couldn't select db");
        
    $result mysql_query($query,$db) or die("Query unsuccessful");
        while (
    $row mysql_fetch_assoc($result)) 
        {
            
    $push_array    = array();
            foreach(
    $row as $key => $value)
            {
                
    $push_array[$key] = $value;
                
    array_push($output_array$push_array);
            }
        }     
        return 
    $output_array;
    }
    // use it
    $result_array    theQuery();
    print_r($result_array);
    ?>
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn
    The Most Important Article You Can Read as a Developer

IMN logo majestic logo threadwatch logo seochat tools logo