Page 1 of 1

nested loops

Posted: Sun Jul 02, 2006 1:22 pm
by barry_normal
Hello, can anyone tell me what's wrong with this code?

Basically I'm using an array of search terms ($terms) to search an array of strings ($w).

I get no output at all at the moment.

Any help would be great, I'm losing patience.

Cheers
B

Code: Select all

//ADDS RESULTS TO PAGE
function outputNews($w){
    $terms = getTerms();
        for($j = 0; $j < sizeof($w); $j++){
            $temp = $w[$j]->get_content();
                for($k = 0; $k < sizeof($terms); $k++){
                    if(strstr($temp, $terms[$k])){
                echo "<BR>";
                    echo $temp;
                        echo "<BR>";
                }
        }
    }
}

Posted: Sun Jul 02, 2006 1:45 pm
by jayshields
Well it's hard to help without seeing the functions, etc.

Have you tried echo'ing something at the start of each loop to see if it's actually dropping into them all?

Posted: Sun Jul 02, 2006 2:24 pm
by barry_normal
Hi Jay, thanks for the reply.

Well, here's the whole lot.

It works fine till I start trying to search for multiple terms in the outputnews or matchWords function.

Just don't know why. It's likely some kind of simple syntax thing as I'm new to php.

Again, any help would be much appreciated.

Cheers
B

Code: Select all

<?php

function loadFiles($fn){
$allPages = array();
for($x = 0; $x < count($fn); $x++){
$doc = domxml_new_doc("1.0");
$doc = domxml_open_file($fn[$x]);
$word = array();
$word = getTitles($doc);
$J = sizeof($word);
echo "this big: ".$J;
$allPages = array_merge($allPages, $word);
}
doHead("Twit");
outputNews($allPages);
doFoot();
}

function getTerms(){
$setTerms = ('Israel', 'bomb', 'monkey', 'drown');
return $setTerms;
}

function outputNews($w){
foreach($w as $tits){
if  (matchWords($tits)){
echo "<BR>";
echo $tits->get_content();
echo "<BR>";
        }
    }
}

function matchWords($t){
$searchTerms = getTerms();
foreach($searchTerms as $tx){
if  (strstr($t->get_content(), $tx)){
return 1;
        }
        return 0;
    }
}

function getTitles($dn){
$title_array = $dn->get_elements_by_tagname("title");
return $title_array;
}

function doHead($name){
echo "<HTML><HEAD><TITLE>";
echo $name;
echo "</TITLE></HEAD><BODY>";
}

function doFoot(){
echo "</BODY></HTML>";
}


$f = array('http://rss.news.yahoo.com/rss/world', 'http://rss.news.yahoo.com/rss/topstories');
loadFiles($f);
?>
[/i]

Posted: Sun Jul 02, 2006 2:52 pm
by barry_normal
Hello Jay.

It was the failure to add the word array before the array creation in 'getTerms()'

I drive myself crazy. Do you know of a good, free php IDE which might pick something like that up?

All the best
B

Posted: Sun Jul 02, 2006 3:53 pm
by Ollie Saunders
PHP Designer 2006 is good although if you are writing code for development you should stick

Code: Select all

ini_set('display_errors','on');
error_reporting(E_ALL);
at the top so PHP tells you about the errors (in a lot more detail than an ide could).

Remember to remove it or comment it out when you have everthing working as its a security risk and errors look really unprofessional anyway.

Posted: Sun Jul 02, 2006 4:09 pm
by barry_normal
Thanks ole, I should have added 'that runs under os x'.

all the best
B