【问题标题】:PHP won't echo XML childPHP不会回显XML子
【发布时间】:2011-01-13 06:07:57
【问题描述】:

这是我的代码

    <form method="post">
  <input name="hash" type="text" id="hash" style='width: 30%;'/>
    <input name="Crack!" type="submit" value="Crack!" onfocus="if(this.blur)this.blur()"/>
</form>

    <?php
 if(isset($_POST['Crack!'])){
  $hash = $_POST['hash'];        

     $xml = simplexml_load_file("http://gdataonline.com/qkhash.php?mode=xml&hash=$hash");

  $status = $xml->data->status; 
    if ($status = "Success"){
     $plain = $xml->data->result;
      }elseif ($status = "Hash not found"){
      $plain = "Not Found"; }     

?>
 <table>
 <tr>  
 <td><?php echo "gdataonline.com: "; ?></td>
 <td><?php echo "$plain"; ?></td>
 </tr>
 </table>

<?php
echo "<pre>";
var_dump($xml);
echo "</pre>"; 

 } //if submit

 ?>

出于某种原因,我根本无法让它回显 $plain。好像连读都看不懂。

【问题讨论】:

    标签: php html xml simplexml


    【解决方案1】:

    你从哪里得到“$xml->data”?根据php.net,SimpleXMLElement 对象中没有称为“数据”的成员。请参阅该链接或simplexml_load_file 的文档以获取有关正确使用此功能的大量示例。

    【讨论】:

      【解决方案2】:

      这可能不是你的全部问题,但一个明确的问题是你有两个作业而不是测试:

      if ($status = "Success")

      }elseif ($status = "Hash not found"){

      都将这些值分配给 $status 而不是测试相等性。你想要$status == "Success"$status == "Hash not found"

      在这种情况下,您的第一个测试将始终成功(因为 assignment 的返回值是分配的值,所以 $status = "Success" 将返回 "Success" 它将在 'if' 测试中评估为 true,所以 $ plain 总是 $xml->data->result,即使状态不是真的成功。

      【讨论】:

      • 你说得对,我不知道我是怎么错过的。谢谢!但是我还是有同样的问题,但还是有必要的,谢谢。
      【解决方案3】:

      Rob,如果你想让人们甚至理解你的问题,你必须付出努力,而不是仅仅发布大量不相关的代码并问“为什么 this 不起作用?”

      所以我完成了你的作业,我弄清楚了脚本在做什么并获取了一个example XML document。事实证明,你弄错了层次结构。此外,这无关紧要,但您使用的是assignment operators 而不是comparison operators。换句话说,你的ifs 不测试任何东西,第一个只是将$status 设置为“成功”。

      相关部分应该是这样的:

      $data = simplexml_load_file("http://gdataonline.com/qkhash.php?mode=xml&hash=$hash");
      
      switch ($data->status)
      {
          case 'Success':
              $plain = $data->result;
              break;
      
          case 'Hash not found':
              $plain = "Not Found";
              break;
      }
      

      【讨论】:

      • 非常感谢!像魅力一样工作!
      【解决方案4】:

      这对我有用:

      <form method="post">
        <input name="hash" type="text" id="hash" style='width: 30%;'/>
          <input name="Crack!" type="submit" value="Crack!" onfocus="if(this.blur)this.blur()"/>
      </form>
      
          <?php
       if(isset($_POST['Crack!'])){
        $hash = $_POST['hash'];        
      
      <?php
      
      $xml = simplexml_load_file("http://gdataonline.com/qkhash.php?mode=xml&hash=$hash")
      
      if(!xml)
      {
       echo "hash not found";
       // return false; // not function so cant return false ignore it
      }
      
      $plain = $xml->result;
      
      ?>
      
      
      <table>
       <tr>
       <td><?php echo "gdataonline.com: "; ?></td>
       <td><?php echo "$plain"; ?></td>
       </tr>
       </table>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-27
        • 1970-01-01
        • 1970-01-01
        • 2011-06-21
        • 2020-06-05
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多