How to combine a string to get a foreach & run alltogether in a loop

Hello dear community, good day

first of all - merry Christmas!

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


$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… and subsequently have a loop that processes (or loads) all the loaded urls … in order to do something with them…

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 a hint! Any and all help will greatly be appreciated.

greetings - and great season-holidays…
db1:)

g day

again me: probably i can do it with this solution, using **the dot.Operator! **



$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 */
}

this is a good solution. i guess that it is good to go!
it gives me a good starting point!

greetings

Hello again - good day,

Update: one last question: I integrate the loop solution with the array into my basic-script …


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

} 

**…like so: **


<?php

$dom = new DOMDocument();
$orig_string = "http://www.somesite.com?page=";

@$dom->loadHTMLFile {

$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 */
}

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

} 

[/quote]

can the integration be done like that!? In order to get a running script with the integration of the array that holds the urls…

I hope that i was able to make clear the question! If not - just post. I can explain more…

Apart from wishing you success in programming in PHP, could you next time when you have PHP quesstion, please state so in the title? Like “PHP: How to combine a string to get a foreach & run alltogether in a loop”. That would people who are more fluent in other languages spare some time in not opening your thread.

Do you mean, loop inside a loop like in this case foreach loop.
If so, you can integrate it with out any problem. You should test it 1st either it is giving you the desired results or not.

@hcvv: Agree, title/subject should be more precise.

Hello you both! good evening hcvv and mmarif4u! great to see you!!

@both, hcvv and mmarif4u!: agreed - i will write more precisely in the future!

Hmmm - i am not sure about your question. I will write down what is aimed and what i have done! Perhaps this clears up a bit the code.

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.

What happened: i runned the code that is shown below (see the code at the end of this posting) - and got the following error:

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

Again **What is aimed!: ** well i am creating a little parser:

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: **

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
60239
60436
60569
62135
60422
60616
…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 [b]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 **

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:

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:


<?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…

Any and all help will be greatly appreciated

Regards
db1:)

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


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;-)


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

} 

?>

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

That is because you are not using the brackets in a right manner.

$dom->loadHTMLFile {

Should be:

$dom->loadHTMLFile ();

You can put any file name you want to extract.

$dom->loadHTMLFile ("downloadfile.html");

Good luck!

Hello mmarif4u - many thanks for the reply.

but i have to include the full code / or expression in order to be able to load virtually many urls into the $dom:

$dom->loadHTMLFile ("downloadfile.html");

…in other words: this must be included:

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

or should i do write it like this: Another way of concatenation - with a Dot-operator.


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

all i want is to load the various urls into this line here:

$dom->loadHTMLFile ("downloadfile.html");

hmm - i will try out some solutions… and try to figure out which solution fits…

many many greetings!!
db1