1

我有一张这张表,我需要在表格行中获取第二个 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>
4

4 回答 4

1

您可以使用 DOM 解析器,例如Simple HTML DOM Parser,如下所示:

$ret = $html->find('td', 2);
于 2012-12-05T19:20:05.437 回答
0

只是一个想法,你可以使用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>
于 2012-12-06T00:34:07.563 回答
0

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

于 2012-12-05T19:21:32.713 回答
0

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

$xpath = new DOMXPath($doc);
$values= $xpath->query("/table/tr/td[2]");

foreach ($values as $value)
{
    echo $value->nodeValue;
}
于 2012-12-05T19:22:20.703 回答