重复使用函数:如何使用数组执行此操作
首先向大家致以节日的问候并祝大家新年快乐!玩得开心!
以下代码是返回标签和值的解决方案 格式化数组准备好输入到 mysql。非常好;-)
<?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>';
}
?>
这段代码工作得很好,它执行了我打算多次调用的操作。因此,将其包装在函数中是有意义的。我们可以将其命名为任何我们想要的名称 - 让我们将其命名为“multiload”。我尝试使用以下代码执行此操作 - 但这不会运行...我仍然不确定将 uid 放在哪里 - 在函数内部还是外部...
<?php
function multiload ($uid) {
/*...*/
// $uid = '60119';
$dom = new DOMDocument();
$dom->loadHTMLFile('basic-url ' . $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>';
}?>
在哪里放置以下行
multicall('60089');
multicall('60152');
multicall('60242');
/*...*/
这仍然是重复的,所以我们可以将数字放入数组中 - 我们不能吗! 然后我们就可以循环遍历数组了。
$numbers = array ('60089', '60152', '60242' /*...*/);
foreach ($numbers as $number) {
doStuff($number);
}
但问题是 - 如何以及在哪里放置循环!?
任何人都可以给我一个起点...
顺便说一句 - 如果我必须更具描述性,我会尝试解释更多 - 请让我知道... 是没有问题的
解释更多问候语 零
更新:感谢大家的大力帮助,我现在已经迈出了一大步,
<?php
function multiload ($uid) {
/*...*/
// $uid = '60119';
$dom = new DOMDocument();
$dom->loadHTMLFile('basic-url ' . $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>';
}
$numbers = array ('60089', '60152', '60242' /*...*/);
foreach ($numbers as $number) {
multiload($number);
}
?>
我在末尾添加了代码......现在我学到了很多。顺便说一句:我很高兴来到这里! 问候零 现在我尝试一下...
First of all-many season-greetings and a happy new year to all of you! Have a great time!!
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('basic-url ' . $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
Zero
UPDATE: thaks to the great help i now have made a big step
<?php
function multiload ($uid) {
/*...*/
// $uid = '60119';
$dom = new DOMDocument();
$dom->loadHTMLFile('basic-url ' . $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>';
}
$numbers = array ('60089', '60152', '60242' /*...*/);
foreach ($numbers as $number) {
multiload($number);
}
?>
i add the code a the end.... Now i have learned alot. By the way: I am very happy to be here!!!
Greetings zero
Now i try it out...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从函数定义外部调用该函数。例如,您可以将其直接放在函数的结束
}
之后:从函数内部调用函数是有效的,但绝对不是您想要的。如果您有兴趣了解从内部调用函数时会发生什么,请阅读递归本身。它可能是一种非常有用的技术,但不是在这里。但在阅读递归之前,我建议您阅读一些有关编写函数的教程,例如 this一个。
Call the function from outside the function definition. For example, you could place it directly after the closing
}
of the function:Calling the function from within itself is valid, but is definitely not what you desire. Read up on recursion if you're interested in finding out what happens when you call a function from within itself. It can be a very useful technique, but not here. But before reading up on recursion, I suggest you read some tutorials on writing functions such as this one.