将 [$_POST] var 插入 DOM 搜索字符串?
我不确定我是否正确命名了该标题,但基本上我有一个来自 $_POST (乐队名称)的变量,我想将它插入到像这样的行中代替节点名称 - 其中
$lastvisits = $address->$band->getElementsByTagName("lastvisit");
$band
是
$band = $_POST['band']
因为 $address 内部的节点实际上可以是任何名称,并且可能不存在。 我确信一定有一个简单的方法,但我不确定格式。
如果存在或不存在, $lastvisits = $address->$band>getElementsByTagName("lastvisit") 会带来错误 - 致命错误:在 a 上调用成员函数 getElementsByTagName()非对象...
XML-
<?xml version="1.0"?>
<addresses>
<address>
<ip>127.0.0.1</ip>
<Beatles>
<lastvisit>12/08/2006</lastvisit>
</Beatles>
</address>
<address>
<ip>125.0.0.1</ip>
</address>
</addresses>
这是完整的代码:
$doc = new DOMDocument();
$doc->load("votingxml/addresses.xml");
$addresses = $doc->getElementsByTagName("address");
$band = strval($_POST['band']);
$pVoted = false;
$pFound = false;
//Loop through the addresses nodes and see if the person has voted before for each( $addresses as $address )
{
$ips = $address->getElementsByTagName("ip");
$ip = $ips->item(0)->nodeValue;
if ($ip == $domain){
$pFound = true;
if ($address->$band == 'NULL'){
$bandfound= false;
$newBandElement = $doc->createElement($_POST['band']);
$newLastVisitElement = $doc->createElement('lastvisit');
$dayvalue = $doc->createTextNode($today);
$dayvalue = $newLastVisitElement->appendChild($dayvalue);
$newBandElement->appendChild($newLastVisitElement);
$address->appendChild($newBandElement);
$doc->save("votingxml/addresses.xml");
$pVoted = false;
}
else{
$bandfound =true;
$lastvisits = $address->$band->getElementsByTagName("lastvisit");
$lastvisit = $lastvisits->item(0)->nodeValue;
if ($lastvisit == $today){
echo "alreadyvoted";
$pVoted = true;
}else{
$lastvisits->item(0)->nodeValue = $today;
$doc->save("votingxml/addresses.xml");
$pVoted = false;
}
}
}
else if ($ip != $domain)
{
$pFound = false;
}
}
任何帮助将不胜感激。
I'm not sure if I named that title correctly, but basically I have a variable from a $_POST (a name of a band) and I want to insert it in place of a node's name in a line like this-
$lastvisits = $address->$band->getElementsByTagName("lastvisit");
where $band
is
$band = $_POST['band']
because the node inside of $address could be any name really, and might not exist.
I'm sure there must be a simple way, but I'm not sure of the formatting.
If it exists or it doesn't, $lastvisits = $address->$band>getElementsByTagName("lastvisit") brings an error- Fatal error: Call to a member function getElementsByTagName() on a non-object in...
The XML-
<?xml version="1.0"?>
<addresses>
<address>
<ip>127.0.0.1</ip>
<Beatles>
<lastvisit>12/08/2006</lastvisit>
</Beatles>
</address>
<address>
<ip>125.0.0.1</ip>
</address>
</addresses>
This is the full code:
$doc = new DOMDocument();
$doc->load("votingxml/addresses.xml");
$addresses = $doc->getElementsByTagName("address");
$band = strval($_POST['band']);
$pVoted = false;
$pFound = false;
//Loop through the addresses nodes and see if the person has voted before for each( $addresses as $address )
{
$ips = $address->getElementsByTagName("ip");
$ip = $ips->item(0)->nodeValue;
if ($ip == $domain){
$pFound = true;
if ($address->$band == 'NULL'){
$bandfound= false;
$newBandElement = $doc->createElement($_POST['band']);
$newLastVisitElement = $doc->createElement('lastvisit');
$dayvalue = $doc->createTextNode($today);
$dayvalue = $newLastVisitElement->appendChild($dayvalue);
$newBandElement->appendChild($newLastVisitElement);
$address->appendChild($newBandElement);
$doc->save("votingxml/addresses.xml");
$pVoted = false;
}
else{
$bandfound =true;
$lastvisits = $address->$band->getElementsByTagName("lastvisit");
$lastvisit = $lastvisits->item(0)->nodeValue;
if ($lastvisit == $today){
echo "alreadyvoted";
$pVoted = true;
}else{
$lastvisits->item(0)->nodeValue = $today;
$doc->save("votingxml/addresses.xml");
$pVoted = false;
}
}
}
else if ($ip != $domain)
{
$pFound = false;
}
}
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑#1:看起来您在添加的示例中引用的是 DOM,而不是 SimpleXML。不过,以下是使用 XML 结构对 SimpleXML 对象执行此操作的方法...
注意:只有 1 个 lastvisit 元素。如果还有更多,您需要将这些 lastvisit 元素包装到 XML 文件中的单个“lastvisits”父元素中。有效的 XML 等等。
编辑 #2:您的 xml 结构存在一些问题。 “披头士”太独特了,不能作为一个元素的名称。您可能想像这样更改它:
然后您需要搜索具有要搜索的 IP 值的
ip
节点,然后在该节点下搜索band
code> 节点的值是“Beatles”。编辑#3、4:使用 DOMXPath 类添加 DOM 方法,以在上述 XML 结构上运行搜索,如果找不到带则添加新记录...
Edit #1: Looks like you're referring to DOM in your added example, not SimpleXML. Still, here's how you do it with SimpleXML objects using your XML structure...
Note: there's only 1 lastvisit element. If there are to be more, you'll need to wrap those lastvisit elements into a single "lastvisits" parent element within the XML file. Valid XML and all that.
Edit #2: there are some problems with your xml structure. "Beatles" is way too unique to be the name of an element. You may want to change it up like so:
Then you'll want to search for the
ip
node with value of the IP you want to search for, then under that for aband
node for the value "Beatles".Edit #3, 4: Adding method for DOM using DOMXPath class to run searches on the above XML structure, and add new record if band not found...