nested loops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
barry_normal
Forum Newbie
Posts: 21
Joined: Sun Jul 02, 2006 5:02 am

nested loops

Post 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>";
                }
        }
    }
}
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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?
barry_normal
Forum Newbie
Posts: 21
Joined: Sun Jul 02, 2006 5:02 am

Post 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]
barry_normal
Forum Newbie
Posts: 21
Joined: Sun Jul 02, 2006 5:02 am

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
barry_normal
Forum Newbie
Posts: 21
Joined: Sun Jul 02, 2006 5:02 am

Post by barry_normal »

Thanks ole, I should have added 'that runs under os x'.

all the best
B
Post Reply