SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,032
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Array function for Harry F's DB Class

    Hi all,

    Harry F's DB Class from The PHP Anthology really inspired me and I've been moving proceedural code and functions into their own classes for a while. While I've been successful in my tasks, I'm always looking to improve on what I've done and this function is either a usefull addition or,.. hopefully I'll learn a better way to do the job.

    I'm a bit of an array junkie and found myself building an object that required either two DB recordsets or, two multidimensional arrays. As I've already confessed my addiction to arrays, I thought I would add a function to the db class that will create arrays of recordsets. It looks like the following:
    PHP Code:
    /**
     * Sets the results to a multidimensional array
     * @return array
     * @access public
     */
     
    function db_to_array()
     {
         
    $res_array = array();
     
         for (
    $count=0$row mysql_fetch_array($this->query); $count++)
             
    $res_array[$count] = $row;
     
         return 
    $res_array;
     }
     
     
    //USAGE
     
    $db          =&new MySQL($host,$dbUser,$dbPass,$dbName);
     
    $sql         "SELECT * FROM db_table";
     
    $result      $db->query($sql);
     
    $resultarray $result->db_to_array(); 
    $resultarray is a multidimensional array representing the db query and can be sorted, reversed, flipped for whatever use you have. In my latest effort I needed to grab the first record, the last record, of my first array then flip the array and loop it with a second array to calculate how data in one was affecting the other. Doing it this way made sense and worked quite easily.

    It's very similar to a procedural function that I found in "PHP and MySQL Web Development" (Welling & Thomson).

    The function works just as expected but,.. that doesn't mean it's right. Is there a better or more efficient way to work with recordsets inside objects?

    Thanks,
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  2. #2
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would use a ResultIterator over the result returned by your querying method.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,032
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Hey Super Phil thanks,

    There's always another way to do things and now that you supplied the keyword ResultIterator I've found some really good discussions about using both methods including one thread I did a double take on because it was almost exactly what I had posted at the start of this one.

    I'm going to have to read up on this a bit before I can determine which is best for my needs. According to what I've read unless you are dealing with huge arrays (1Meg +) the array method is more efficient than ResultIterator. I have to read more and play around with it first before I can even be certain of that though.

    I suppose what my chief question is, if it is a better practice to use my db results object or an array inside the class that I am building or whether that's just a preference of who's coding?

    The code for both are very similar and look something like this:
    * Of course the code inside the Calc class will be quite different for either.

    PHP Code:
    $result1    $db->query($sql1);
     
    $calcarray  $result1->db_to_array(); //db_to_array function
     
     
    $result2    $db->query($sql2);
     
    $eventarray $result2->db_to_array(); //db_to_array function
     
     //Instantiating the object using arrays
     
    $CalculationObject =&new Calc($calcarray$datevariable$eventarray);
     
     
    //Instantiating the object using db object
     
    $CalculationObject =&new Calc($result1$datevariable$result2); 
    Maybe either method will work but I found it easier to work with the array in my class when it came to re-ordering the results asc/desc and getting my first or last elements but that's just me.

    Thanks for the insight, I searched arround for a while and didn't find much until I looked up ResultIterator.

    Cheers,
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Efficiency is basically the same, unless your counting the cost of instiating the iterator - both of them loop over the array. In fact, if you have to do anything with the data I would take the iterator over your function.

    Say I wanted to run an if statement on every name - allow me to use this nonsensicle example:
    PHP Code:
    if($row['name'] == 'Phil' || (substr($row['name'], 01) === 'B'))
    {
        if(isset(!
    $cool_user) && rand(0,1))
        {
            
    $cool_user $row['name'];
        }
        else
        {
            echo 
    $row['name'] . ' is a loser';
        }

    If I loop over the array db_to_array, thats the 'second' loop over the data I've done. I've looped through the rows once to get to the array, and the array a second time. This, as you can see, gets expensive. Using the iterator, you can only go over everything once.

    While you could argue you could use array_walk, this can get messy and is unescessary.

    If you loop over data isntead of sending it directly to a template engine or something, I'm pretty sure the iterator will be more efficient.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,032
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Super Phil
    If I loop over the array db_to_array, thats the 'second' loop over the data I've done. I've looped through the rows once to get to the array, and the array a second time. This, as you can see, gets expensive. Using the iterator, you can only go over everything once.
    It's late and my eyes aren't focussing super fast but....
    You're absolutely right and the code looks a whole lot easier to maintain at first glance. That's an excellent point and I thought my arrays were so cool

    I've got some code to sift through.

    Thanks for the info, this is really helpful.
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development


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
  •