需要在php的一个页面中显示输入和输出?

时间:2015-03-10 09:31:55

标签: javascript php jquery

我从数据库中获取的表中有多少行。我只需要在dat表旁边显示每行的输出。输出数据也来自数据库。

示例:如果我单击第一行意味着我想要有关该表旁边该行的详细信息。 Screenshot of what i need

      <script type="text/javascript">
      $(function() {
      $('#showmenu').click(function() {
      $(".menu").slideToggle();
      });
      });
      </script>

PHP代码:第一个表(显示数据)

      $query = "select * from orders_list";
                        $result = mysql_query($query);

                        $num_rows = mysql_num_rows($result);
                        if($num_rows >= 1)
                            {
                            echo "<div id='showmenu' class='scroll'>";  
                        echo "<table id='table_struct' cellspacing='0' cellpadding='1' border='1' width='400'>
                             <tr class='tr_class' bgcolor='#0066CC'>
                             <td align='center' style='font-weight:bold'> Select </td>
                             <td align='center' style='font-weight:bold'> order_id </td>
                             <td align='center' style='font-weight:bold'> customer_name </td>
                             <td align='center' style='font-weight:bold'> no_of_pack </td>
                             <td align='center' style='font-weight:bold'> price </td>
                             <td align='center' style='font-weight:bold'> Weight </td>
                             <td align='center' style='font-weight:bold'> payment mode </td>
                        </tr>";

                            while($row = mysql_fetch_array($result))
                            {

                                    $order_id = $row['order_id'];
                                    echo "<tr>
                                    <td align='center'><input type='checkbox' class='case' name='case' value='1'></td>
                                    <td align='center'>".$row['order_id']."</td>
                                    <td align='center'>".$row['customer_name']."</td>
                                    <td align='center'>".$row['number_of_pack']."</td>
                                    <td align='center'>".$row['price']."</td>
                                    <td align='center'>".$row['weight']."</td>
                                    <td align='center'>".$row['payment']."</td>";
                            echo "</tr>";
                                    }
                                    echo "</table>";
                                    echo "</div>";
                                    }

输出的代码显示在同一页面中:

                 $_SESSION['order_id'] = $order_id;
                        echo $_SESSION['order_id'];
                        $query = "select * from orders_details where order_id=$order_id";
                        $result = mysql_query($query);

                        $num_rows = mysql_num_rows($result);
                        if($num_rows >= 1)
                            {
                            echo "<div class='menu' class='scroll'>";   
                        echo "<table id='table_struct' cellspacing='0' cellpadding='1' border='1' width='400'>
                             <tr class='tr_class' bgcolor='#0066CC'>
                             <td align='center' style='font-weight:bold'> Product </td>
                             <td align='center' style='font-weight:bold'> Quantity </td>
                             <td align='center' style='font-weight:bold'> Sku </td>
                             <td align='center' style='font-weight:bold'> Price </td>
                             <td align='center' style='font-weight:bold'> Total </td>

                        </tr>";

                            while($row = mysql_fetch_array($result))
                            {
                                    echo "<tr>
                                    <td align='center'><input type='checkbox' class='case' name='case' value='1'></td>
                                    <td align='center'>".$row['product']."</td>
                                    <td align='center'>".$row['quantity']."</td>
                                    <td align='center'>".$row['sku']."</td>
                                    <td align='center'>".$row['price']."</td>
                                    <td align='center'>".$row['total']."</td>";

                            echo "</tr>";
                                    }
                                    echo "</table>";
                                    echo "</div>";
                                    }

请帮帮我..

2 个答案:

答案 0 :(得分:1)

有两种解决方案,一种是使用$ order_id创建每行旁边的行,在内部包含第二个脚本。您还可以使用style="display:none"隐藏该行,然后在javascript中隐藏或显示下一行(您可以使用colspan="7"将整个表格包含在一个td中)

$('#table_struct tr.input').click(function() {
   $(this).next().toggle();
});

第二个选项是将第二个脚本放在不同的文件中,并使用所选行的order_id运行ajax请求。您可以在对话框中显示第二个表。或者如果你希望它在行的旁边,那么你可以使用javascript:

设置它的位置
$('#table_struct tr').click(function() {
    var $this = $(this);
    var offset = $this.offset();
    var height = $this.height();
    var order_id = $this.data('order_id');
    $('.menu').remove();
    $.get('your_second_script.php?order_id=' + order_id, function(table) {
        $('#showmenu').append(table);
        $('.menu').css({
           position: 'absolute',
           left: offset.left,
           top: offset.top+height
        });
    });
});

tr需要data-order_id属性。

答案 1 :(得分:0)

你可能会发现这样的事情 -         在你的第一个表中

    <tr class='tr_class' bgcolor='#0066CC' data-id="<?=$row['order_id']?>">

你的jQuery -

    $('#tr_class').click(function(){  

            var rowId =  $(this).data('id');

            $.ajax({             
            url: "targetScript.php",  
            data:{rowId: rowId},      
            type: 'POST',
            success: function(data){  
            $('#table_second').html("");
            $('#table_second').html(data);
            }           
            }); 
    }); 

targetScript.php -

<?php   
if(isset($_POST['rowId'])){
        $order_id = $_POST['rowId'];
        $query = "select * from orders_details where order_id=$order_id";
        $result = mysql_query($query);
        $num_rows = mysql_num_rows($result);

        $html = "";
        if($num_rows >= 1){
            $html.="<tr class='tr_class' bgcolor='#0066CC'>
                             <td align='center' style='font-weight:bold'> Product </td>
                             <td align='center' style='font-weight:bold'> Quantity </td>
                             <td align='center' style='font-weight:bold'> Sku </td>
                             <td align='center' style='font-weight:bold'> Price </td>
                             <td align='center' style='font-weight:bold'> Total </td>

                        </tr>";

            while($row = mysql_fetch_array($result)){
            $html.="<tr>
            <td align='center'><input type='checkbox' class='case' name='case' value='1'></td>
            <td align='center'>".$row['product']."</td>
            <td align='center'>".$row['quantity']."</td>
            <td align='center'>".$row['sku']."</td>
            <td align='center'>".$row['price']."</td>
            <td align='center'>".$row['total']."</td>"."</tr>";
            }           
        }print $html;
}
?>