Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts

    Question Array organization..

    Hi, I'll try to make this as simple as possible.

    There's a site: http://rs2006.net/services/forums/forums.ws

    What I want to do, is loop through each forum category ('news & announcements', 'report a bug/fault') and store the id of the forum and the time it was last posted in in an array..

    I eventually plan to make this into an application that checks this main forums.ws page for categories that were recently posted in, and if the recent posted in time is different than what's stored in the database, it'll go to that category and retrieve a list of the most recently posted in topics etc..

    This is what I have. Keep in mind it works exactly how I want, but I have a feeling there is a simpler way of doing it.

    PHP Code:
    <?php

    include('../config/database-config.php');
    include(
    '../includes/DOM.php');

    $data file_get_html('http://rs2006.net/services/forums/forums.ws');

    $cats = array();

    foreach(
    $data->find('tr[class=border item]') as $category)
    {
        foreach(
    $category->find('span[class=bigtitle]a') as $url)
        {
            if(
    preg_match('/(board)/'$url->href))
            {
                
    $m explode("?"$url->href);
                
                if(@!
    $cats[$m[1]]) 
                { 
                    foreach(
    $category->find('td[class=righttd updated]') as $time)
                    {
                        
    $cats[$m[1]] = preg_replace('/\s\s\s+/'''$time->innertext);
                    }
                    
                }
            }
            
            
        }
        
            
        
        
    }
    ?>
    print_r($cats); returns:

    PHP Code:
    Array
    (
        [
    1,2] => 03-Dec-2011 8:28:00
        
    [2,3] => 03-Dec-2011 7:00:59
        
    [3,4] => 03-Dec-2011 8:23:18
        
    [43,44] => 03-Dec-2011 6:55:50
        
    [4,5] => 03-Dec-2011 7:50:01
        
    [5,6] => 03-Dec-2011 7:38:58
        
    [14,15] => 03-Dec-2011 6:51:31
        
    [15,16] => 03-Dec-2011 6:38:23
        
    [16,17] => 03-Dec-2011 6:35:01
        
    [17,18] => 03-Dec-2011 6:33:09
        
    [7,8] => 03-Dec-2011 8:16:27
        
    [8,9] => 03-Dec-2011 7:03:15
        
    [9,10] => 03-Dec-2011 7:47:54
        
    [10,11] => 03-Dec-2011 6:55:38
        
    [11,12] => 02-Dec-2011 18:14:55
        
    [12,13] => 03-Dec-2011 8:04:08
        
    [13,14] => 03-Dec-2011 6:23:48
        
    [18,19] => 03-Dec-2011 8:28:48
        
    [19,20] => 03-Dec-2011 7:52:05
        
    [20,21] => 03-Dec-2011 4:16:09
        
    [21,22] => 03-Dec-2011 5:39:45
        
    [44,45] => 03-Dec-2011 6:29:03
        
    [22,23] => 03-Dec-2011 7:06:55
        
    [6,7] => 03-Dec-2011 8:22:20

    Last edited by Adee; 12-03-2011 at 01:45 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This looks fairly simple to me.
    I don't see a purpose for the preg replace though. The data doesn't appear that it will have any issues without a replacement. I would however convert it to a timestamp using strtotime instead. That will make sorting it easier, as well as allowing a different format. That said, this step can occur at any time you desire, so you can wait until you are checking if you want to.

    As for a simpler way, not really. You can use straight pattern matching to do what you want, but matching is relatively slow to do. You can use a full dom, but that wastes memory more (what you have appears to be a dom/xpath hybrid with the custom functions you are using). Using the dom with xpath would result in something almost the same as what you have here.

    All and all, what I'd suggest is actually contacting the owner of that page, to see if they have an xml or RSS feed you can tap into. Typically, you wouldn't check all the time, but once every half hour or so is sufficient. If that is available, that will make life a whole lot easier as you don't need to do anything with PHP, except maybe bringing it in and formatting it with an xslt.

  • Users who have thanked Fou-Lu for this post:

    Adee (12-03-2011)

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    Quote Originally Posted by Fou-Lu View Post
    This looks fairly simple to me.
    I don't see a purpose for the preg replace though. The data doesn't appear that it will have any issues without a replacement. I would however convert it to a timestamp using strtotime instead. That will make sorting it easier, as well as allowing a different format. That said, this step can occur at any time you desire, so you can wait until you are checking if you want to.

    As for a simpler way, not really. You can use straight pattern matching to do what you want, but matching is relatively slow to do. You can use a full dom, but that wastes memory more (what you have appears to be a dom/xpath hybrid with the custom functions you are using). Using the dom with xpath would result in something almost the same as what you have here.

    All and all, what I'd suggest is actually contacting the owner of that page, to see if they have an xml or RSS feed you can tap into. Typically, you wouldn't check all the time, but once every half hour or so is sufficient. If that is available, that will make life a whole lot easier as you don't need to do anything with PHP, except maybe bringing it in and formatting it with an xslt.
    Thanks. Before using preg_replace on the date/time it would have a bunch of spaces on either side.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I see, use the trim() function instead.


  •  

    Tags for this Thread

    Posting Permissions

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