0

I have number of rows in a table fetched from database. I need to display output of each row beside dat table only. output data also fetching from database.

Example: If i click first row means i want detail information about that row next to that table. Screenshot of what i need

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

PHP Code: First Table(Displaying Data)

      $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>";
                                    }

code for output to display in same page:

                 $_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>";
                                    }

Pls help me out..

6
  • 3
    We need the code, not the screenshot. Commented Mar 10, 2015 at 9:34
  • All you looking for is AJAX. Commented Mar 10, 2015 at 9:35
  • Not a big issue. but you need to post code, which you tried and not worked for you? Commented Mar 10, 2015 at 9:35
  • ya i attached the code Commented Mar 10, 2015 at 9:40
  • WARNING: You're using a deprecated database API. Consider using PDO or MySQLi. The mysql_* driver will soon be dropped and is considered obsolete in modern applications. Commented Mar 10, 2015 at 9:46

2 Answers 2

1

There are two solutions one is to create row next to each row with $order_id and inside you include your second script. You also hide that row with style="display:none" and then in javascript you hide or show next row (you can inslude whole table in one td with colspan="7")

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

Second option is to have your second script in different file and run ajax request with order_id of selected row. You can display that second table inside dialog box. Or if you want it to be next to the row then you can set postion of it using 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 will need to have data-order_id attribute.

Sign up to request clarification or add additional context in comments.

Comments

0

You may find something like this- In your first table

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

Your 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;
}
?>

3 Comments

i have only on page in that only i need to display input table as wel as output table
You can add the code of targetScript.php file on the page you want.
I have only one PHP file. but using two separate PHP tags. If I click first row in table of first PHP tag I need to display first row value in table of second PHP tag in same page. common field for two tables is order_id The output should display beside to the first table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.