-1

i have a 3 table and im doing inner join in the output below enter image description here

how can i achive something like this enter image description here

so far i already have code for expanding the row. the real problem here is how do i get all the signatoryname for each tracknum. im using php and html

this is my code

                    <table  class="table table-bordered ">
                    <thead>
                      <tr>
                    <th>Track Number</th>
                    <th>Document Title</th>
                    <th>Document Type</th>
                    <th>Date Filled</th>
                    <th> </th>
                      </tr>
                    </thead>
                <?php while ($r = $q->fetch()): ?>
                        <tr>
                            <td><?php echo $r['tracknum'] ?></td>
                            <td><?php echo $r['doctitle'] ?></td>
                            <td><?php echo $r['doctype'] ?></td>
                            <td><?php echo $r['datefilled'] ?></td>
                             <td>
                             <a href="#"><span class="btnshow glyphicon glyphicon-plus-sign"></span></a>

                             </td>

                        </tr>
                        <tr><td colspan="5"><p><?php echo $r['signatoryname'] ?></p>
                        </td></tr>
                <?php endwhile; ?>

                </table> 

for the table to expand

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
    $("td[colspan=5]").find("p").hide();
    $("table").click(function(event) {
        event.stopPropagation();
        var $target = $(event.target);
        if ( $target.closest("td").attr("colspan") > 1 ) {
            $target.slideUp();
        } else {
            $target.closest("tr").next().find("p").slideToggle();
        }                    
    });
});
});//]]> 

</script>

this is the output of the code

enter image description here i need to group the data below by tracknum but i need the signatoryname i want the html row to be expandable and list the signatoryname of that tracknum bellow it. thanks.

update: so far this is my code

UPDATE: below is the correct code:

<?php
                require_once 'dbconfig.php';
                try {
                    $conn = new PDO("mysql:host=$host;dbname=$dbname",
                            $username, $password);
                    // execute the stored procedure
                    $sql = 'CALL sp_trasactionsignatory()';
                    $q = $conn->query($sql);
                    $q->setFetchMode(PDO::FETCH_ASSOC);
                } catch (PDOException $pe) {
                    die("Error occurred:" . $pe->getMessage());
                }
                ?>                  
                <table  class="table table-bordered ">
                    <thead>
                      <tr>
                    <th>Track Number</th>
                    <th>Document Title</th>
                    <th>Document Type</th>
                    <th>Date Filled</th>
                    <th> </th>
                      </tr>
                    </thead>
                <?php while ($r = $q->fetch()): ?>
                        <tr>
                            <td><?php echo $r['tracknum'] ?></td>
                            <td><?php echo $r['doctitle'] ?></td>
                            <td><?php echo $r['doctype'] ?></td>
                            <td><?php echo $r['datefilled'] ?></td>
                             <td>
                             <a href="#"><span class="btnshow glyphicon glyphicon-plus-sign"></span></a>

                             </td>

                        </tr>
                        <tr><td colspan="5">


                        <?php 


                        require_once 'dbconfig.php';
                        try {

                        $conn = new PDO("mysql:host=$host;dbname=$dbname",
                            $username, $password);

                        $_tempp1 = $r['tracknum'];
                        $stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
                        $stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30); 
                        $stmt->execute();
                        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                            echo "<p>" . $row['signatoryname'] . "</p>";
                        }

                        } catch (PDOException $pe) {
                    die("Error occurred:" . $pe->getMessage());
                }

                        ?>


                        </td></tr>
                <?php endwhile; ?>

                </table> 
knowmeifyou
  • 217
  • 7
  • 17

1 Answers1

1

Problem:

How do I get all the signatoryname for each tracknum?

Solution:

Your very first initial query would be like this,

(Since you didn't provide the table name, change the table name from the below queries)

$q = $connection->query("SELECT tracknum, doctitle, doctype, datefilled FROM tablename GROUP BY tracknum"); 

Then comes to your table,

<table  class="table table-bordered ">
<thead>
<tr>
    <th>Track Number</th>
    <th>Document Title</th>
    <th>Document Type</th>
    <th>Date Filled</th>
    <th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch_assoc()): ?>
    <tr>
        <td><?php echo $r['tracknum']; ?></td>
        <td><?php echo $r['doctitle'] ?></td>
        <td><?php echo $r['doctype'] ?></td>
        <td><?php echo $r['datefilled'] ?></td>
         <td>
         <a href="#"><span class="btnshow glyphicon glyphicon-plus-sign"></span></a>
         </td>

    </tr>
    <tr><td colspan="5">    
    <?php
        $result_set = $connection->query("SELECT signatoryname FROM tablename WHERE tracknum = {$r['tracknum']}");
        while ($row = $result_set->fetch_assoc()){
            echo "<p>" . $row['signatoryname'] . "</p>";
        }
    ?>
    </td></tr>
<?php endwhile; ?>

</table>

Don't forget to change the tablename in both the queries.

Edited:

If you using PDO extensions to execute your queries then you can do something like this,

<?php
    $_tempp1 = $r['tracknum'];
    $stmt = $connection->prepare("SELECT signatoryname FROM tablename WHERE tracknum = :tracknum");
    $stmt->bindParam(':tracknum', $_tempp1, PDO::PARAM_STR, 30); 
    $stmt->execute();

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        echo "<p>" . $row['signatoryname'] . "</p>";
    }
?>
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • sir i updated the code above. the problem now is it only retrieve the signatoryname of the second row. but not on the first row of the html table. i mean the query doesn't work on the first row of the table. but works on the second one. sorry for my bad english – knowmeifyou Nov 21 '15 at 13:02
  • Why `CALL sp_gettransactsignatory(?)`? Is there any specific need for stored procedure? Coz a simple query will you give the required results. – Rajdeep Paul Nov 21 '15 at 13:54
  • im using stored procedure sir for lesser code and easier to change the sql. ive tried the code sir but it's still the same. i even echo the $r['tracknum']; to check if the tracknum is correct. but i don't understand. why it doesn't query the first row even if the tracknum is correct – knowmeifyou Nov 21 '15 at 21:55
  • 1
    I've tested your code on my system, and it's working just as you expect(including the javascript code). Run [this tested code](http://pastebin.com/f5yQVAUB) on your machine. And if you still want to use `sp_gettransactsignatory(?)` stored procedure, make sure everything is correct in there. – Rajdeep Paul Nov 21 '15 at 23:58
  • ive tested the sp_gettransactsignatory on mysql sir. so i know it's correct, and if its wrong it will not query the second row correctly. and ill try you're code sir and ill try to find the problem with mine, thanks – knowmeifyou Nov 22 '15 at 00:17
  • thank you so much for you're help sir. i found my problem by comparing your code to mine, the problem is the connection on the first row. it is not connected, but i dont know why, it connects to the database after getting the data of second row – knowmeifyou Nov 22 '15 at 00:46
  • Happy to hear. Cheers! :-) – Rajdeep Paul Nov 22 '15 at 00:48