Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-03-2011, 01:34 PM   PM User | #1
Adee
Regular Coder

 
Join Date: Jul 2010
Location: Oregon City
Posts: 280
Thanks: 5
Thanked 50 Times in 49 Posts
Adee can only hope to improve
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..
Adee is offline   Reply With Quote
Old 12-03-2011, 07:52 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Adee (12-03-2011)
Old 12-03-2011, 09:04 PM   PM User | #3
Adee
Regular Coder

 
Join Date: Jul 2010
Location: Oregon City
Posts: 280
Thanks: 5
Thanked 50 Times in 49 Posts
Adee can only hope to improve
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.
Adee is offline   Reply With Quote
Old 12-03-2011, 11:13 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I see, use the trim() function instead.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
array, organization, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:28 AM.


Advertisement
Log in to turn off these ads.