concatenate a string to get a foreach & run in a loop

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
lin
Forum Commoner
Posts: 49
Joined: Tue Dec 07, 2010 1:53 pm

concatenate a string to get a foreach & run in a loop

Post by lin »

How to combine / concatenate a *divided* string in order to use this combined / concatenated string
in a loop where i run the following construct:

Code: Select all

$dom = new DOMDocument();
@$dom->loadHTMLFile('<- path to the file-> =60119');
combined with the following.... :
60299
64643
62958
63678
60419
60585
60749
60962

and so on.

Question: How to combine the string (in fact the string is an URL) so that i am able to build the URLs automatically.
I hope that i was able to explain the question so that you understand it. If i have to be more descriptive - just let me know!

Many many thanks for ah hint!

lin
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: concatenate a string to get a foreach & run in a loop

Post by s.dot »

Is it in an array?
See implode()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
lin
Forum Commoner
Posts: 49
Joined: Tue Dec 07, 2010 1:53 pm

Re: concatenate a string to get a foreach & run in a loop

Post by lin »

hello dear S.dot,

Many thanks for the reply. I want to load the numbers into an array: like so:

Code: Select all

$orig_string = "http://www.somesite.com?page=";
$number_array = array ("123", "43567", "9287","3323");
for($i=0; $i<$count($number_array); $i ++) {
$new_url = $orig_url . $number_array[$i];
/* do something with the new url */
}
What is aimed: Hmmm - i want to automize the integration of many urls, that are build in a quite simple way....in order to build in the URLs into a parser-script.

This is a complete URL: Schulen in Rheinland-Pfalz: Einzelanzeige

As i have many URLs to parse i have the divided URL into two pieces:

part one:

Code: Select all

http://schulen.bildung-rp.de/gehezu/startseite/einzelanzeige.html?tx_wfqbe_pi1%5buid%5d=
part two: with the the following endings:
60452
60512
60329
60149
...and so forth ...[note i have many many of those endings of the URL; In order to parse the corresponding sites i subsequently want to load the full URLs into the parser-script you see below.

Again - in other words: I thought that i have to take the first part of the URL, i call it the base-URL

Code: Select all

http://schulen.bildung-rp.de/gehezu/startseite/einzelanzeige.html?tx_wfqbe_pi1%5buid%5d=
...and the second part - the endings 62135, 60422, 60616 [and so forth]- and combine them in a concatenation - with a DOT-Operator. But again: as mentioned above: i runned the code and i got the error:

Code: Select all

martin@suse-linux:~/perl> php parser_rlpa.php
PHP Parse error:  syntax error, unexpected ';' in /home/martin/perl/parser_rlpa.php on line 9

See the code that i runned:

Code: Select all

<?php

$dom = new DOMDocument();
$orig_string = "http://schulen.bildung-rp.de/gehezu/startseite/einzelanzeige.html?tx_wfqbe_pi1%5buid%5d=";

@$dom->loadHTMLFile {

$number_array = array ( "60089", "60152","60242","61730", "43567", "9287","3323");
for($i=0; $i<$count($number_array); $i ++) {

$new_url = $orig_url . $number_array[$i];

/* do something with the new url */
}

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

} 

?>
Hmm _ guess that i have done something wrong here! I will think about this code. There must be an error in line 9. Perhaps i have written something wrong while i integrated the array with the numbers....
S.dot [and all]: Any and all help will be greatly appreciated

Regards
lin

Post Scriptum: Note, see the results of a single URL - without any loop and array:

Code: Select all

pre>Array
(
    [0] => Schulart:
    [1] => BBS
    [2] => Schulnummer:
    [3] => 60119
    [4] => Anschrift:
    [5] => Berufsbildende Schule BoppardAntoniusstr. 2156154 Boppard
    [6] => Telefon:
    [7] => (0 67 42) 80 61-0
    [8] => Telefax:
    [9] => (0 67 42) 80 61-29
    [10] => E-Mail:
    [11] => sekretariat@bbs-boppard.de
    [12] => Internet:
    [13] => http://www.bbs-boppard.de
    [14] => Träger:
    [15] => Kreisverwaltung Rhein-Hunsr�ck-Kreis
    [16] => letzte Ãnderung:
    [17] => 08 Feb 2010 14:33:12 von 60119
)
This nice result is the output of the following code: It is a solution that returns the labels and values in a formatted array ready for input to mysql. Very nice;-)

Code: Select all

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

} 

?>
Question is: how to integrate the above mentioned code:

Code: Select all

$orig_string = "http://www.somesite.com?page=";
$number_array = array ("123", "43567", "9287","3323");
for($i=0; $i<$count($number_array); $i ++) {

$new_url = $orig_url . $number_array[$i];

/* do something with the new url */
}

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: concatenate a string to get a foreach & run in a loop

Post by s.dot »

Try this:

Code: Select all

foreach ($number_array AS $number)
{
    $new_url = $orig_url . $number;
    //do something with $new_url
}

//instead of this
//
//for($i=0; $i<$count($number_array); $i ++) {
//
//$new_url = $orig_url . $number_array[$i];
//
///* do something with the new url */
//}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
lin
Forum Commoner
Posts: 49
Joined: Tue Dec 07, 2010 1:53 pm

Re: concatenate a string to get a foreach & run in a loop

Post by lin »

hello dear s.dot,

many thanks for the mail - great to hear from you. Your advice seems to be convincing. I try it out!
s.dot wrote:Try this:

Code: Select all

foreach ($number_array AS $number)
{
    $new_url = $orig_url . $number;
    //do something with $new_url
}

//instead of this
//
//for($i=0; $i<$count($number_array); $i ++) {
//
//$new_url = $orig_url . $number_array[$i];
//
///* do something with the new url */
//}
Update: i gave it a try - see the full code:

Code: Select all


<?php

$dom = new DOMDocument();
$orig_string = "http://schulen.bildung-rp.de/gehezu/startseite/einzelanzeige.html?tx_wfqbe_pi1%5buid%5d=";

@$dom->loadHTMLFile ($number_array = array ( "60089", "60152","60242","61730", "43567", "9287","3323");
foreach ($number_array AS $number)
{
    $new_url = $orig_url . $number;
    //do something with $new_url
}


/* do something with the new url */


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

} 

?>

see the result:

Code: Select all

PHP Parse error:  syntax error, unexpected ';' in /home/martin/perl/parser_rlpa_new.php on line 6
martin@suse-linux:~/perl> 
hmm - what do we miss - what is it!? I have no glue. i will try to figure it out.

Look forward to hear from you. Many thanks in advance!!

regards
db1
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: concatenate a string to get a foreach & run in a loop

Post by McInfo »

The semicolon at the end of this line is unexpected because the parser expects to see a second right parenthesis. The last right parenthesis here closes array(), but loadHTMLFile() is unclosed.

Code: Select all

@$dom->loadHTMLFile ($number_array = array ( "60089", "60152","60242","61730", "43567", "9287","3323");
lin
Forum Commoner
Posts: 49
Joined: Tue Dec 07, 2010 1:53 pm

Re: concatenate a string to get a foreach & run in a loop

Post by lin »

hello McInfo

many many thanks - great to hear from you
McInfo wrote:The semicolon at the end of this line is unexpected because the parser expects to see a second right parenthesis. The last right parenthesis here closes array(), but loadHTMLFile() is unclosed.

Code: Select all

@$dom->loadHTMLFile ($number_array = array ( "60089", "60152","60242","61730", "43567", "9287","3323");
i try this out later today.
I come back and report all my findings.


many many tanks so far. Greetings
lin

Update: i tried to fix the code - but i had no luck - see here - again some errors.

Code: Select all

<?php

$dom = new DOMDocument();
$orig_string = "http://schulen.bildung-rp.de/gehezu/startseite/einzelanzeige.html?tx_wfqbe_pi1%5buid%5d=";

@$dom->loadHTMLFile ($number_array = array ( "60089", "60152","60242","61730", "43567", "9287","3323"));
//for($i=0; $i<$count($number_array); $i ++) {
//  $new_url = $orig_string . $number_array[$i];


foreach ($number_array AS $number)
{
    $new_url = $orig_string . $number;
    //do something with $new_url
}


/* do something with the new url */


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

} 

?>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: concatenate a string to get a foreach & run in a loop

Post by McInfo »

Sorry; I guided you in the wrong direction by pointing out the syntactical flaw without examining the underlying logical flaw.

Take a few steps back to here:
lin wrote:This nice result is the output of the following code: It is a solution that returns the labels and values in a formatted array ready for input to mysql. Very nice;-)

Code: Select all

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

} 

?>
If that bit of code works and it performs an operation that you intend to call upon multiple times, it makes sense to wrap it in a function. You can name it whatever you want--I'll just name it "doStuff".

Code: Select all

function doStuff () {
    $dom = new DOMDocument();
    $dom->loadHTMLFile('http://.../...?tx_wfqbe_pi1%5buid%5d=60119');
    /*...*/
    foreach ($children as $child) {
        /*...*/
        echo '</pre>';
    }
}
Now, every time you call the doStuff() function, it loads and operates on the HTML file for number 60119. That's not what you really want, so continue reading.

To make the number dynamic, you can assign it to a variable and concatenate the variable with the URI string.

Code: Select all

function doStuff () {
    /*...*/
    $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, make the number a parameter of the function.

Code: Select all

function doStuff ($uid) {
    /*...*/
    $dom->loadHTMLFile('http://.../...?tx_wfqbe_pi1%5buid%5d=' . $uid);
    /*...*/
}
Now, different HTML will be loaded every time you call the doStuff() function with a different argument.

Code: Select all

doStuff('60089');
doStuff('60152');
doStuff('60242');
/*...*/
This is still repetitive, so put the numbers in an array, as you did. Then, remembering s.dot's suggestion, loop through the array.

Code: Select all

$numbers = array ('60089', '60152', '60242' /*...*/);
foreach ($numbers as $number) {
    doStuff($number);
}
lin
Forum Commoner
Posts: 49
Joined: Tue Dec 07, 2010 1:53 pm

Re: concatenate a string to get a foreach & run in a loop

Post by lin »

Hello McInfo,

[right now i am in a big hurry - i will answer later this day. I am just about to leave the house. i will be back home in bout 12 hours. Then i can answer in a longer message]

many many thanks for the message - and your great step-by-step-tutorial. This is a indepth tutorial wich gives me some tips on how i go about creating this.
After a very very quick preview i came up with this - but i am not 100 % sure that i have put it together in the right way... i will have a closer look at the code later this day.

Anyway - you helped my very very much. i find your ideas and explanations very very good.

i hope that i can put the code togehter in a propper fashion... [this one runned with some errors.. ] i think that ihave to take care for the brackets ....

Code: Select all

<?php

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

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

$dom = new DOMDocument();

         $dom->loadHTMLFile('http://schulen.bildung-rp.de/gehezu/startseite/einzelanzeige.html?tx_wfqbe_pi1%5buid%5d=' . $uid);

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

}

?>

i come back later this day...

untill soon!!
regards
lin
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: concatenate a string to get a foreach & run in a loop

Post by McInfo »

I think you missed the point of the function. The $numbers loop does not go within the function definition. The loop goes outside the function definition where the function is called multiple times.

Code: Select all

function doStuff () {
    /* Inside, define the function. */
}
doStuff(); /* <-- Outside, call the function. */
lin
Forum Commoner
Posts: 49
Joined: Tue Dec 07, 2010 1:53 pm

Re: concatenate a string to get a foreach & run in a loop

Post by lin »

Hello dear McInfo many thanks for the infos.

First of all: This is a very very supportive thread!
McInfo wrote:I think you missed the point of the function. The $numbers loop does not go within the function definition. The loop goes outside the function definition where the function is called multiple times.

Code: Select all

function doStuff () {
    /* Inside, define the function. */
}
doStuff(); /* <-- Outside, call the function. */
According your idea of a " stepwise progression " i did some trials - and followed the first steps - did it with the following...

Code: Select all


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

}

?>
the code works not really -


Well - i try to do it first with the following expression - and subsequently with the array...
first - i want to put this one here into the code ...

Code: Select all

multiload ('60089');
multiload ('60152');
multiload ('60242');
and lastly i want to replace it with the array: i guess that i have to put the array at the end of the code - before to this part here...

Code: Select all

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

}
McInfo - i love to hear from you again... Can you give me one more advice and tip? That would be great!
Many many thanks to you for the great help!

lin :)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: concatenate a string to get a foreach & run in a loop

Post by McInfo »

You appear to be trying to load multiple documents and then perform one operation on all of them at once. Programming does not work that way. You must load one document, perform the operation on it; load the next document, perform the operation on that document; and continue that way for all the documents.

If you look back to my post where I introduced the doStuff() function, you should notice that the function (the "operation") encompasses everything from the line where $dom is initialized to the line with the right curly bracket after "</pre>" is echoed.
lin
Forum Commoner
Posts: 49
Joined: Tue Dec 07, 2010 1:53 pm

Re: concatenate a string to get a foreach & run in a loop

Post by lin »

Hello
McInfo wrote:You appear to be trying to load multiple documents and then perform one operation on all of them at once. Programming does not work that way. You must load one document, perform the operation on it; load the next document, perform the operation on that document; and continue that way for all the documents.

If you look back to my post where I introduced the doStuff() function, you should notice that the function (the "operation") encompasses everything from the line where $dom is initialized to the line with the right curly bracket after "</pre>" is echoed.
Many thanks for the input! I am trying to figure out what you said! And i try to add the code in the right way.
Many thanks for your help so far!: So, what I seeing that you were providing help in dividing the steps in multiple little steps.

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

And now i try to figure out what i have to do! I will come back and report all my findings...

greetings
lin
Post Reply