[英]Need to display input and output in one page of php?
我從數據庫中獲取表中的行數。 我只需要在dat表旁邊顯示每一行的輸出。 輸出數據也從數據庫中獲取。
示例:如果我單擊第一行,則意味着我想要該表旁邊該行的詳細信息。
<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>";
}
請幫我..
有兩種解決方案,一種是在每個帶有$ 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
屬性。
您可能會發現這樣的東西-在您的第一張桌子中
<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;
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.