从表的选定行中获取所有值

时间:2012-10-12 17:29:40

标签: php jquery ajax

如何将表2中的某些值插入表1.如果单击“添加”按钮,我想获取所选行的所有值。我应该从我选择的当前行获取rowindex并通过$prod获得rowindex值吗?

<table id="table1">
    <tr>
        <th>Product ID</th>
        <th>Product Name</th>
        <th>Price</th>
        <th>Stock</th>
        <th>Action</th>
    </tr>
    <?php foreach($prod->getAll() as $name):?>
    <tr>
        <td><?php echo $name['prod_id'];?></td>
        <td><?php echo $name['name'];?></td>
        <td><?php echo $name['price'];?></td>
        <td><?php echo $name['stock'];?></td>
        <td><button class="addbtn">Add</button></td>
    </tr>
    <?php endforeach;?>
</table>

<table id="table2">
<tr>
    <th>No</th>
    <th>Product Name</th>
    <th>Price</th>
    <th>Qty</th>
    <th>Total</th>
</tr>
<tr>
    <td>1</td>
    <td>Some Product Name From Table 1</td>
    <td>Some Product Price From Table 1</td>
    <td>Qty is generated from table 1</td>
    <td>Qty * Price</td>
</tr>
</table>


 <?php

 class Product extends DB_Connect
 {

    public function __construct()
    {
     parent::__construct();
    }

function getAll(){
    try
    {
        $sql = "SELECT * FROM product Order By prod_id asc";
        $result = $this->db->query($sql);
        $results = $result->fetchAll(PDO::FETCH_ASSOC);
        return $results;
    }
    catch ( Exception $e )
    {
        die ( $e->getMessage() );
    }
}
}

?>

1 个答案:

答案 0 :(得分:1)

如果你正在使用jQuery,你可以使用closest()函数:

$('.addbtn').click(function(){
    var row = $(this).closest('tr'),
        cells = row.find('td'),
        prodId = cells.eq(0).html(),
        name = cells.eq(1).html(),
        price = cells.eq(2).html(),
        stock = cells.eq(3).html();
});

小提琴:http://jsfiddle.net/Afk8a/1/