php使用DOMDocument更改div但不更新页面

时间:2013-08-04 21:09:12

标签: php dom html

我不确定我做错了什么,但我正在为我想要的东西获得正确的nodeValue。它只是在PHP脚本完成时不更新。这是代码:

    $dom = new DOMDocument();
    //suppress HTML5 and other errors
    libxml_use_internal_errors(true);
    $dom->loadHTMLFile($pageURL);
    libxml_use_internal_errors(false);
    $xpath = new DOMXPath($dom);
    $divContent = $xpath->query("//*[@id='resultStats']/p")->item(0);
    $newText = new DOMText("100 results");
    var_dump($divContent->nodeValue); //returns old test value "400 results" which is correct
    $divContent->removeChild($divContent->firstChild);
    $divContent->appendChild($newText);
    var_dump($divContent->localName); //"p" because i got it from <p> in resultStats
    var_dump($divContent->textContent); //"100 results"
    var_dump($divContent->nodeValue); //"100 results"

更多围绕它的HTML

    <div class="container">
    <div class="row">
    <div class="resultStats span3 offset1" id="resultStats">
      <p>400 results found.</p>
    </div>
    </div>
    <div class="row">
     <div class="span12">
         <div class="row">
              <div class="span6 offset1">
                  <?php
                         if (isset($_POST['q'])) {
                        //code from above that is executing every time from tests
                         }
                  ?>
               </div>
               <div class="span5">
                  span5
                  </div>
                  </div>
                  </div>
                  </div>

我不确定我做错了什么。如果我做dom-&gt;保存它会重写所有内容(甚至PHP代码),所以我认为这不是一个好主意。

1 个答案:

答案 0 :(得分:0)

我不明白你为什么要使用DOMDocument。你能不能这样做:

<div class="container">
<div class="row">
<div class="resultStats span3 offset1" id="resultStats">

<?php

  // get new result count somehow in $resultCount

  echo '<p>'.$resultCount.' results found</p>';
?>
</div>
</div>