开发者

Repetitive use of a function: how to perform this - with an array

First of all-many season-greetings and a happy new year to all of you! Have a great time!!

The following code is a solution that returns the labels and values in a formatted array ready for input to mysql. Very nice;-)

<?php

$dom = new DOMDocument();
@$dom->loadHTMLFile('http://schulen.bildung-rp.de/gehezu/startseite/einzelanzeige.html?tx_wfqbe_pi1%5buid%5d=60119');
$divElement = $dom->getElementById('wfqbeResults');

$innerHTML= '';
$children = $divElement->childNodes;
foreach ($children as $child) {
$innerHTML = $child->ownerDocument->saveXML( $child );

$doc = new DOMDocument();
$doc->loadHTML($innerHTML);
//$divElementNew = $dom->getElementsByTagName('td');
$divElementNew = $dom->getElementsByTagname('td');

    /*** the array to return ***/
    $out = array();
    foreach ($divElementNew as $item)
    {
        /*** add node value to the out array ***/
        $out[] = $item->nodeValue;
    }

echo '<pre>';
print_r($out);
echo '</pre>';

} 

?>

That bit of code works very fine and it performs an operation that i intend to call upon multiple times. Therefore it makes sense to wrap it in a function. We can name it whatever we want- Let us just name it "multiload". I tried to do this with the following code - but this does not run... I am still not sure where to put the uid - inside or outside the function...

    <?php

    function multiload ($uid) {
    /*...*/
    //  $uid = '60119';

    $dom = new DOMDocument();

             $dom->loadHTMLFile('basic-url ' . $uid);


         }

    multiload ('60089');
    multiload ('60152');
    multiload ('60242');
    /*...*/

    $divElement = $dom->getElementById('wfqbeResults');

    $innerHTML= '';
    $children = $divElement->childNodes;
    foreach ($children as $child) {
    $innerHTML = $child->ownerDocument->saveXML( $child );

    $doc = new DOMDocument();
    $doc->loadHTML($innerHTML);
    //$divElementNew = $dom->getElementsByTagName('td');
    $divElementNew = $dom->getElementsByTagname('td');

        /*** the array to return ***/
        $out = array();
        foreach ($divElementNew as $item)
        {
            /*** add node value to the out array ***/
            $out[] = $item->nodeValue;
        }

    echo '<pre>';

print_r($out);
echo '</pre>';

}开发者_如何学JAVA?>

Where to put the following lines

multicall('60089');
multicall('60152');
multicall('60242');
/*...*/

This is still repetitive, so we can put the numbers in an array - can ´t we! Then we can loop through the array.

$numbers = array ('60089', '60152', '60242' /*...*/);
foreach ($numbers as $number) {
    doStuff($number);
}

But the question is - how to and where to put the loop!?

Can anybody give me a starting point...

BTW - if i have to be more descriptive i am trying to explain more - just let me know... it is no problem to explain more

greetings Zero

UPDATE: thaks to the great help i now have made a big step

<?php

    function multiload ($uid) {
    /*...*/
    //  $uid = '60119';

    $dom = new DOMDocument();

             $dom->loadHTMLFile('basic-url ' . $uid);


         }

    multiload ('60089');
    multiload ('60152');
    multiload ('60242');
    /*...*/

    $divElement = $dom->getElementById('wfqbeResults');

    $innerHTML= '';
    $children = $divElement->childNodes;
    foreach ($children as $child) {
    $innerHTML = $child->ownerDocument->saveXML( $child );

    $doc = new DOMDocument();
    $doc->loadHTML($innerHTML);
    //$divElementNew = $dom->getElementsByTagName('td');
    $divElementNew = $dom->getElementsByTagname('td');

        /*** the array to return ***/
        $out = array();
        foreach ($divElementNew as $item)
        {
            /*** add node value to the out array ***/
            $out[] = $item->nodeValue;
        }

    echo '<pre>';

print_r($out);
echo '</pre>';

}

$numbers = array ('60089', '60152', '60242' /*...*/);
foreach ($numbers as $number) {
    multiload($number);
}

?>

i add the code a the end.... Now i have learned alot. By the way: I am very happy to be here!!! Greetings zero Now i try it out...


Call the function from outside the function definition. For example, you could place it directly after the closing } of the function:

<?php

function multiload ($uid) {
    // function code
}

$numbers = array ('60089', '60152', '60242' /*...*/);
foreach ($numbers as $number) {
    multiload($number);
}

?>

Calling the function from within itself is valid, but is definitely not what you desire. Read up on recursion if you're interested in finding out what happens when you call a function from within itself. It can be a very useful technique, but not here. But before reading up on recursion, I suggest you read some tutorials on writing functions such as this one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜