PHP Walker Class

walker class. This class will load results by executing given SQL query and then create multidimensional array that will have entries in right position. Let’s say that you have this data in your database.

 

+----+--------+---------------------------------+---------------------+
| id | parent | message                         | date                |
+----+--------+---------------------------------+---------------------+
|  1 |      0 | First message.                  | 2009-09-02 00:07:07 |
|  2 |      0 | Second message.                 | 2009-09-02 00:07:08 |
|  3 |      1 | Reply to first message          | 2009-09-02 00:07:30 |
|  4 |      2 | Reply to second message         | 2009-09-02 00:07:31 |
|  5 |      3 | Reply to reply to first message | 2009-09-02 00:07:45 |
+----+--------+---------------------------------+---------------------+

What our script will do is something like this.

array
  1 =>
    object(stdClass)[3]
      public 'self' =>
        object(stdClass)[2]
          public 'id' => string '1' (length=1)
          public 'parent' => string '0' (length=1)
          public 'message' => string 'First message.' (length=14)
          public 'date' => string '2009-09-02 00:07:07' (length=19)
      public 'childs' =>
        array
          3 =>
            object(stdClass)[7]
              public 'self' =>
                object(stdClass)[6]
                  public 'id' => string '3' (length=1)
                  public 'parent' => string '1' (length=1)
                  public 'message' => string 'Reply to first message' (length=22)
                  public 'date' => string '2009-09-02 00:07:30' (length=19)
              public 'childs' =>
                array
                  5 =>
                    object(stdClass)[11]
                      public 'self' =>
                        object(stdClass)[10]
                          public 'id' => string '5' (length=1)
                          public 'parent' => string '3' (length=1)
                          public 'message' => string 'Reply to reply to first message' (length=31)
                          public 'date' => string '2009-09-02 00:07:45' (length=19)
                      public 'childs' =>
                        array
                          empty
  2 =>
    object(stdClass)[5]
      public 'self' =>
        object(stdClass)[4]
          public 'id' => string '2' (length=1)
          public 'parent' => string '0' (length=1)
          public 'message' => string 'Second message.' (length=15)
          public 'date' => string '2009-09-02 00:07:08' (length=19)
      public 'childs' =>
        array
          4 =>
            object(stdClass)[9]
              public 'self' =>
                object(stdClass)[8]
                  public 'id' => string '4' (length=1)
                  public 'parent' => string '2' (length=1)
                  public 'message' => string 'Reply to second message' (length=23)
                  public 'date' => string '2009-09-02 00:07:31' (length=19)
              public 'childs' =>
                array
                  empty

As you can see, all entries are nested and on the right place.

Define Class And Variables

First we will define class and variables. Class name will be walker and she will have 6 variables.

  • $_results will have results resource retrieved from mysql_query
  • $_con will have MySQL connection resource
  • $_refMap will be our reference map. It’s just simple array where key is current entry ID and value is parent ID of current entry
  • $_traced will have contain traced entries like our example above
  • $_parent will be the name of column that has parent ID value
  • $_id will be the name of column that has ID value

 

To use this class just extend it and create some formatting methods. In my next tutorial we will create multilevel comments using this class so stay tuned. You can download source here.

http://www.php4every1.com/tutorials/php-walker-class/

Leave a comment