Showing a subset of data with same table

Showing a subset of data with same table

bitmacherbitmacher Posts: 15Questions: 6Answers: 0

Description of problem:
One question regarding a structure of MySQL with datatables, because I have a knot in my head right now.

The idea is to show several tables of the same kind, e.g. Journal 1, Journal 2, Journal 3, etc.

The table behind it is always the same. It's just different journals. And the files journal.php, controller.php and journal.js should be always the same. What's the better alternative? Everything in a table and choose accordingly? Or take 3 times everything. So journal1.php, journal2.php, journal1.js, journal2.js, etc.

How can I manage that in the journal.js file if I have only one file?

if ( data.journal == "1")
  {
// show only table with data.journal = 1
  columns: [
   { data: "journal.id"},
   { data: "journal.trade"},
   { data: "journal.stock"},
   { data: "journal.swap"},
 }
// show only table with data.journal = 2
 else if (data.journal == "2") 
 {
  columns: [
   { data: "journal.id"},
   { data: "journal.trade"},
   { data: "journal.stock"},
   { data: "journal.swap"},
 }
.......

Table: journal1
+----+-------+------+------+
| id | trade | stock| swap |
+----+-------+------+------+
| 1  | 112   | 43  | 1.45  | 
| 2  | 223   | 34  | 1.23  |
| 3  | 322   | 23  | 1.24  |
+----+-------+-----+-------+
Table: journal2
+----+-------+------+------+
| id | trade | stock| swap |
+----+-------+------+------+
| 1  | 165   | 43  | 1.45  | 
| 2  | 256   | 34  | 1.27  |
| 3  | 672   | 23  | 1.24  |
+----+-------+-----+-------+

Table: journal
+----+-------+------+------+-----
| id | trade | stock| swap | journal
+----+-------+------+------+-----
| 1  | 112   | 43  | 1.45  |  1
| 1  | 165   | 43  | 1.45  |  2
| 2  | 223   | 34  | 1.23  |  1
| 2  | 256   | 34  | 1.27  |  2
| 3  | 322   | 23  | 1.24  |  1
| 3  | 672   | 23  | 1.24  |  2
+----+-------+-----+-------+-----

Thanks for a short hint....

Best regards
Mac

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,322Questions: 26Answers: 4,948
    Answer ✓

    Not sure if this is answering your question but you can use ajax.data to send the journal ID or whatever to the server. Then your server script can use the parameter value to filter the select results using a where statement. Does this help?

    Kevin

  • bitmacherbitmacher Posts: 15Questions: 6Answers: 0

    Hi Kevin,

    thanks for the hint. My solution is now this. You can build a link like journal.php?jid=1

    header.php:

    echo '<a class="dropdown-item" href="journal.php?jid=' . $record['id'] . '">'
    

    journal.php:

    include_once($path."inc/header.php");
    
    $jid = (INT) $_GET['jid'];
    
    <!-- Variable journal number send to Javascript -->
    <script>
      var jid = <?php echo json_encode($jid); ?>
    </script>
    <script type="text/javascript" src="js/journal.js"></script>
    

    journal.js:

    editor = new $.fn.dataTable.Editor( {
        ajax: { 
          url: "controllers/controller_journal.php",
          type: "POST",
          data: function (d) {
            d.jid = jid;
    

    controller_journal.php

        ->where(function ($q) {
          $q->where( 'jr_account', $_POST['jid'] );
        } )
      
        ->on( 'preCreate', function ( $editor, $values ) {
          $editor
            ->field( 'journal.jr_account' )
            ->setValue( $_POST['jid'] );
        } )
        
        ->on( 'preEdit', function ( $editor, $id, $values ) {
          $editor
            ->field( 'journal.jr_account' )
            ->setValue( $_POST['jid'] );
        } )
    

    In the controller_journal.php you have the where funtion to filter the table for the right id and set the id to the account. That's it.

    Thank you again for the hint.

    Best regards
    Mac

Sign In or Register to comment.