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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.