Trying to store Tabular Data in an object, not arrays

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Norsak
Forum Newbie
Posts: 2
Joined: Sat Sep 01, 2007 4:49 am

Trying to store Tabular Data in an object, not arrays

Post by Norsak »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


When I learned to program, we only had Arrays, no Objects.
Now I am trying to get into the habit of using Objects, rather than always shoe-horning arrays.

Today I am trying to store tabular Data in an object.
[b]
$table = 
1 a b c 
2 x y z
3 e d f
[/b]
In my example I am storing the filename, file size, and creation dates for all the files in a folder, but it could be any data matrix.

Sounds good....until I tried it.   
I can't figure out how to assign an unspecified number of properties in a class, without falling back on arrays.
My first property ends up being an array, and I essentially end up with one array representing to table rows, holding smaller arrays containing the fields.

I feel like I am 'missing something'.   There probably is a very obvious/clever way to do this, but I need someone to point me in the right direction.

Here is what I wrote this morning.
It works.   I have a method called [b]sort[/b] which allows me to sort my 'table' based on any field.
It just feels like a hack.   Please let me know if there is a better way to create an Object which stores a table's worth of data.

Code: Select all

<?php
class table
   {
     var $rows = array();
     var $count = 0;

     function add($row)
        {
         $this->rows[$this->count] = $row;
         $this->count++;
        }

     var $sort_field;
     private function sort_by_field($a,$b)
                  {
                    if ($a[$this->sort_field] == $b[$this->sort_field])
                         return 0;
                    if ($a[$this->sort_field] > $b[$this->sort_field])
                         return 1;
                    else
                         return -1;
                  }

    function sort($field)
        {
          $this->sort_field=$field;
          usort($this->rows,array('table','sort_by_field'));
        }
   }

$myfolder= new table();

$directory=$_SERVER['DOCUMENT_ROOT']."/pdf";

$dp = opendir($directory);

while($file = readdir($dp))
    {
      if($file != '.' && $file != '..')
      {
        $file_object= array();
        $file_object[]=$file;
        $file_object[]=filesize("$directory/$file");
        $file_object[]=filemtime("$directory/$file");
        $myfolder->add($file_object);
      }
    }
closedir($dp);

$myfolder->sort(1);

foreach($myfolder->rows as $row)
    {
     foreach($row as $field)
           echo $field.'....';
     echo '<br>';
    }
?>

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Sounds good....until I tried it.
I can't figure out how to assign an unspecified number of properties in a class, without falling back on arrays.
You don't need to... there are arrays just for that, storing unspecified numbers of similar elements. Though you may benefit from added functionality when wrapping your array into an object.

besides, you should not use static callback when calling usort because your sort_by_field is not a static method (it uses object's data). Pass a callback like this: array($this, 'sort_by_field), or array(&$this, 'sort_by_field') if you're using PHP4
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Trying to store Tabular Data in an object, not arrays

Post by superdezign »

Norsak wrote:When I learned to program, we only had Arrays, no Objects.
Now I am trying to get into the habit of using Objects, rather than always shoe-horning arrays.
Don't confuse objects with arrays. We all still use arrays. Arrays are for lists (or lists of lists, etc), not for holding arbitrary data. Objects are a lot more complex than just existing for holding data.

Objects must have responsibilities, and usually attributes to complement those responsibilities. The sorting would be a responsibility of a "sorting object," and the array of data would be one of it's attributes. It'd sort it within itself, and then you could use it. Usually, an object that handles sorting handles other things, though (unless the sorting is very complex).
Norsak
Forum Newbie
Posts: 2
Joined: Sat Sep 01, 2007 4:49 am

Post by Norsak »

So I'm not really that far off the mark?
I guess I'll keep Arrays front and center.
Don't confuse objects with arrays.
I'll tell you why I'm confusing Objects and Arrays:

The goal is to create an HTML table which the viewer can sort by clicking on any column. The last time I did this:
- I used the MYSQL query to get the data sorted one way
- then PHP to render the data in an HTML table
- then javascript to allow the user to sort the table as they liked.

And in Javascript, objects and Arrays are pretty much the same thing.

This time a wanted to skip the javascript, and use PHP and page-reloads to sort the table....


Thanks for the feed back.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

If you are going to do page reloads and a query on ever reload, then have the sort be in the sql.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If memory serves, there's a thread linked from Useful Posts that discusses a method of on-demand column sorting.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Norsak wrote:The goal is to create an HTML table which the viewer can sort by clicking on any column. The last time I did this:
- I used the MYSQL query to get the data sorted one way
- then PHP to render the data in an HTML table
- then javascript to allow the user to sort the table as they liked.

And in Javascript, objects and Arrays are pretty much the same thing.
Well, JavaScript, Java, and languages that were derived from them are a different beast. Everything is an object. They are purely object-oriented languages, so you can't apply everything you learn from them to other languages... Mostly, only OO concepts and OOD. When it comes to arrays and functions and such, there are huge differences.

MySQL can sort your data for you, so there's no need to use JavaScript when using MySQL would be simpler. You could create a class that built the table, created the links along the head of the table for the names of the columns (and probably 'nicknames' to be displayed to the user) for sorting, and built the MySQL query based on the query string. Putting this all in a class would help you with validating the data in the query string as well by matching it against the valid columns.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I don't know why you don't want to use arrays. Arrays are very much fitted for this purpose. Storing, manipulating, and retreiving data. It seems like more of a hack NOT to use an array.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply