PHP - repetitive [re]call of an operation - stepwise progression in developing a solution

Dear all,

i have a quite simple but too complex for me: i need explanations!

The following parser-code is a solution that returns the labels and values in a formatted array ready for input to mysql.Now i want to do more - i want to do perform an operation that calls upon multiple times, with various urls. Therefore it makes sense to wrap it in a function. Therefore i name it “multiload”.

some questions arise, while i am

  • loading various urls - and do the
  • repetitive recall of the uid

Please read on below the code.


<?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>';

} 

?>

Again: That code - listet above - works very fine and it performs the 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”.

Question: Can i do this with the following bit of code:

function multiload () {
    /*...*/
    $uid = '60119';
    $dom->loadHTMLFile('http://.../...?tx_wfqbe_pi1%5buid%5d=' . $uid);
    /*...*/

That doesn’t change the way the function operates, though. The function will still always have the same result as before. To fix that, we should make the number a parameter of the function.

function mulitload ($uid) {
    /*...*/
    $dom->loadHTMLFile('http://.../...?tx_wfqbe_pi1%5buid%5d=' . $uid);
    /*...*/
}


Now, different HTML will be loaded every time you call the multiload() function with a different argument.

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

This is still repetitive, so we should put the numbers in an array and subsequently
loop through the array.

Then, different HTML will be loaded every time you call the multiload() function with a different argument.

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

Well - my question is: where to put the code - I guess, that the $numbers loop does not go within the function definition. The loop sure has to go outside the function definition where the function is called multiple times,

**BTW: **I am an advocate of “incremental development” especially when there are multiple steps to be learned.

How has the code to look like if i perform the call of the url with

a. this:


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

or b: different HTML can be loaded every time you call the multiload() function with a different argument.

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

But where to add the code to do that!? How has the code to look like?

I look forward to any and all help!

btw - if i need to explain more - just let me know!

db1

Well,
I’m not going to try to actually validate either your existing or proposed code but I can offer a suggestion where you may be falling short…

You need to define your code flow in a kind of workflow so you understand what is input and what is output. The code you describe doesn’t describe in any way what kind of output you need for the next step in your iterative programming

I would also recommend you learn how to use a RAD (Rapid Application Developent) tool, in your case specifically for PHP (your chosen code language). A good RAD tool can offer things like debugging to understand the consequences and flow of your executed code, organization and management of functions and code blocks you create, recommend code (once you’ve declared objects like functions and code blocks) and if your application is to have a GUI (like a web page) help you design while wiring up your backend code.

HTH,
Tony

Well, I would make the function to work both ways; like this;


function multiload($uid){
if(@is_array($uid)){
foreach($uid as $thing){
if(!@load($thing)){
/** you can do some of these here:
* return false;
*$status=false;
*break;
*/
}
}
elseif($uid != "" && $uid != null){
return @load($uid);  // return status of load function for each item
}
else return false; // return false if $uid is undefined, null or something undesired
}
function load($uid){
/** do things */
return $status;
}