将 [$_POST] var 插入 DOM 搜索字符串?

发布于 2024-10-03 19:52:05 字数 2681 浏览 5 评论 0原文

我不确定我是否正确命名了该标题,但基本上我有一个来自 $_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆で 2024-10-10 19:52:05

编辑#1:看起来您在添加的示例中引用的是 DOM,而不是 SimpleXML。不过,以下是使用 XML 结构对 SimpleXML 对象执行此操作的方法...

$band = $_POST['band'];

if (property_exists($address, $band)) {
    $lastvisits = $address->$band->lastvisit;
}
else {
    /* handle the fact that the name of the element which the user passed is invalid */
}

注意:只有 1 个 lastvisit 元素。如果还有更多,您需要将这些 lastvisit 元素包装到 XML 文件中的单个“lastvisits”父元素中。有效的 XML 等等。

编辑 #2:您的 xml 结构存在一些问题。 “披头士”太独特了,不能作为一个元素的名称。您可能想像这样更改它:

<addresses>
   <address>
      <ip>127.0.0.1</ip>
      <visits>
         <visit>
            <band>Beatles</band>
            <lastvisit>12/08/2006</lastvisit>
         </visit>
         <visit>
            <band>New Kids On the Block</band>
            <lastvisit>1/14/2008</lastvisit>
         </visit>
      </visits>
   </address>
   <address>
      <ip>24.135.9.2</ip>
      <visits>
...
      </visits>
   </address>
</addresses>

然后您需要搜索具有要搜索的 IP 值的 ip 节点,然后在该节点下搜索 band code> 节点的值是“Beatles”。

编辑#3、4:使用 DOMXPath 类添加 DOM 方法,以在上述 XML 结构上运行搜索,如果找不到带则添加新记录...

$band = $_POST['band'];
$client_IP = $_SERVER["REMOTE_ADDR"];
$doc = new DOMDocument();
$doc->load("votingxml/addresses.xml");
$xpath = new DOMXPath($doc);
$ip_query = $xpath->query('//addresses/address/ip[. = "' . $client_IP . '"]');
if ($ip_query->length === 1) {
    // client's IP found, now grab and work with the client's address record
    $address = $ip_query->item(0)->parentNode;
    // search for the visit record within $address for band element value = $band
    $band_query = $xpath->query('visits/visit/band[. = "'.$band.'"]', $address);
    if ($band_query->length === 1) {
        // $band found, now get visit parent record for more details
        $visit = $band_query->item(0)->parentNode;
        // with that, now do something with the record's lastvisit value
        echo $visit->getElementsByTagName("lastvisit")->item(0)->nodeValue;
        }
    else {
        // $band not found.  Add new visit record to the XML file
        // point to 'visits' group element
        $visits = $address->getElementsByTagName("visits")->item(0);
        // create a new 'visit' element, complete with 'band' and 'lastvisit' child elements
        $visit = $doc->createElement("visit");
        $band = $doc->createElement("band", $band);
        $lastvisit = $doc->createElement("lastvisit", date("n/d/Y"));
        $visit->appendChild($band);
        $visit->appendChild($lastvisit);
        // now add new 'visit' element to 'visits' element and save
        $visits->appendChild($visit);
        $doc->save("votingxml/addresses.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...

$band = $_POST['band'];

if (property_exists($address, $band)) {
    $lastvisits = $address->$band->lastvisit;
}
else {
    /* handle the fact that the name of the element which the user passed is invalid */
}

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:

<addresses>
   <address>
      <ip>127.0.0.1</ip>
      <visits>
         <visit>
            <band>Beatles</band>
            <lastvisit>12/08/2006</lastvisit>
         </visit>
         <visit>
            <band>New Kids On the Block</band>
            <lastvisit>1/14/2008</lastvisit>
         </visit>
      </visits>
   </address>
   <address>
      <ip>24.135.9.2</ip>
      <visits>
...
      </visits>
   </address>
</addresses>

Then you'll want to search for the ip node with value of the IP you want to search for, then under that for a band 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...

$band = $_POST['band'];
$client_IP = $_SERVER["REMOTE_ADDR"];
$doc = new DOMDocument();
$doc->load("votingxml/addresses.xml");
$xpath = new DOMXPath($doc);
$ip_query = $xpath->query('//addresses/address/ip[. = "' . $client_IP . '"]');
if ($ip_query->length === 1) {
    // client's IP found, now grab and work with the client's address record
    $address = $ip_query->item(0)->parentNode;
    // search for the visit record within $address for band element value = $band
    $band_query = $xpath->query('visits/visit/band[. = "'.$band.'"]', $address);
    if ($band_query->length === 1) {
        // $band found, now get visit parent record for more details
        $visit = $band_query->item(0)->parentNode;
        // with that, now do something with the record's lastvisit value
        echo $visit->getElementsByTagName("lastvisit")->item(0)->nodeValue;
        }
    else {
        // $band not found.  Add new visit record to the XML file
        // point to 'visits' group element
        $visits = $address->getElementsByTagName("visits")->item(0);
        // create a new 'visit' element, complete with 'band' and 'lastvisit' child elements
        $visit = $doc->createElement("visit");
        $band = $doc->createElement("band", $band);
        $lastvisit = $doc->createElement("lastvisit", date("n/d/Y"));
        $visit->appendChild($band);
        $visit->appendChild($lastvisit);
        // now add new 'visit' element to 'visits' element and save
        $visits->appendChild($visit);
        $doc->save("votingxml/addresses.xml");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文