Jump to content

repetitive use of a function - how to perform with an array in a loop


dilbertone

Recommended Posts

Hello dear community,

 

 

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('http://schulen.bildung-rp.de/gehezu/startseite/einzelanzeige.html?tx_wfqbe_pi1%5buid%5d=' . $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>';

}


?>

 

 

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

 

Hello BlueSkyIs,

 

many thanks for your answer!!

 

That function is so simple, i would just skip writing the function and put the function code within the loop.

 

Hmm - how do i apply it!`

 

Hmmm - as i am new to php i ask myself what the point of the function is.  Does the $numbers loop  go within the function definition.

 

i guess no - Hmm - guess the loop goes outside the function definition where the function is called multiple times.

 

function  () {
    /* Inside, define the function. */
}
multiload(); /* <-- Outside, call the function. */ 

 

Love to hear from you

 

best regards

db1

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.