【问题标题】:Get values from second TD from every TR with PHP使用 PHP 从每个 TR 中获取第二个 TD 的值
【发布时间】:2012-11-23 17:21:49
【问题描述】:

我有一张这张表,我需要获取表行中第二个 TD 的值。我可以用 PHP 做到这一点,如何做到这一点?

这是我的桌子:

<table class = "jshop cart">
  <tr class = "jshop_prod_cart odd">
    <td class = "jshop_img_description_center">
      <a href = "/shopping/index.php/product/view/210/3690">
        <img src = "http://localhost/shopping/components/com_jshopping/files/img_products/noimage.gif" alt = "test" class = "jshop_img" />
      </a>
    </td>
    <td style="text-align:left">
        FIRST VALUE I NEED                      
            </td>    
    <td>
        1 ден.            </td>
    <td>
      <input type = "text" name = "quantity[0]" value = "1" class = "inputbox" style = "width: 25px" />
    </td>
    <td>
        1 ден.            </td>
    <td>
      <a href = "/shopping/index.php/cart/delete?number_id=0" onclick="return confirm('Навистина сакате да го отстраните?')"><img src = "http://localhost/shopping/components/com_jshopping/images/remove.png" alt = "Отстрани" title = "Отстрани" /></a>
    </td>
  </tr>

  <tr class = "jshop_prod_cart even">
    <td class = "jshop_img_description_center">
      <a href = "/shopping/index.php/product/view/169/2876">
        <img src = "http://localhost/shopping/components/com_jshopping/files/img_products/thumb_3e28bb073e2ac2ca90345955ead6fc58.JPG" alt = "" class = "jshop_img" />
      </a>
    </td>
    <td style="text-align:left">
                 SECOND VALUE I NEED       

            </td>    
    <td>
        140 ден.            </td>
    <td>
      <input type = "text" name = "quantity[1]" value = "1" class = "inputbox" style = "width: 25px" />
    </td>
    <td>
        140 ден.            </td>
    <td>
      <a href = "/shopping/index.php/cart/delete?number_id=1" onclick="return confirm('Навистина сакате да го отстраните?')"><img src = "http://localhost/shopping/components/com_jshopping/images/remove.png" alt = "Отстрани" title = "Отстрани" /></a>
    </td>
  </tr>
    </table>

【问题讨论】:

  • PHP 无法做到这一点。因为数据在客户端,所以需要用到JavaScript。
  • 简短的回答是不,你不能。 PHP是服务器端的。它生成 HTML。您不能使用 PHP 在客户端读取网页内容。您需要使用客户端(即 javascript)技术来读取 DOM。除非您的意思是要解析 HTML 服务器端?那就另当别论了。
  • @Diodeus 不一定。 Curl 可用于下载页面,然后使用 XPath 提取所需的数据。
  • 是的 - 但这就像用右手抓挠左耳,将手臂放在头顶上方。

标签: php html dom html-table


【解决方案1】:

您可以像这样使用 DOM 解析器,例如 Simple HTML DOM Parser

$ret = $html->find('td', 2);

【讨论】:

    【解决方案2】:

    PHP 不会用于这种情况,因为它是一种服务器端语言。 PHP 在服务器而不是客户端上运行。除非您愿意通过 Javascript 将整个页面的数据发送到 PHP,否则我认为没有理由使用 PHP 来选择这些值。我认为您应该使用 jQuery 来提取这些值。 jQuery 有很多方法可以做这样的事情。做一个基本的谷歌搜索。

    【讨论】:

      【解决方案3】:

      如果您在 PHP 处理期间有表,则可以在其上使用 XPath。像这样的东西:(考虑这个伪代码,没有测试就输入了)

      $xpath = new DOMXPath($doc);
      $values= $xpath->query("/table/tr/td[2]");
      
      foreach ($values as $value)
      {
          echo $value->nodeValue;
      }
      

      【讨论】:

        【解决方案4】:

        只是一个想法,你可以使用jquery。为元素分配一个id,你可以有一个隐藏的输入。

        在 html 中,类似这样:

        <input type="hidden" id="test" value="<?=row['value']?>">
        

        在 jquery 中,你可以获取元素值,如下所示:

        <script>
           $(document).ready(function(){
             $('#button').click(function() {
               $('#sub_cont').fadeIn('fast');
               $("#content #sub_cont").load("postpage.php?id="+ $("#test").val());
             });
          });
        </script>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-30
          • 1970-01-01
          相关资源
          最近更新 更多