lundi 29 juin 2015

How to store values in a data attribute and pass the value with php

I'm trying to figure out how I can store a user_id value in my drag and drop table and pass the value along when dropping the item in a new column.

For instance with my code. I start all the users in the 'Owes' column. When someone pays I want to move them to the appropriate column with my drag and drop table. I am outputting all this information from my database, so when I move someone from 'Owes' to 'Paid' I will need to update the record in the db so it saves in the drag and drop table.

My table is like this..

 <table class="paymentTable" id="dragTable">
        <tr>
            <th class="thPayment">Paid</th>
            <th class="thPayment">Partially Paid</th>
            <th class="thPayment">Owes</th>
        </tr>
        <tr>
            <td class="tdPayment" id="paid">
                            <div>
            <?php
                if ($paid_name == true) {
                    echo $paid_name;
                } else {
                    echo "No one has paid";
                }
            ?>
                            </div>
            </td>
            <td class="tdPayment" id="partially_paid">
            <div>
            <?php 
                if ($partially_paid__name == true) {
                    echo $partially_paid__name . " - " . $partially_paid_amount;
                } else {
                    echo "No one has made a partial payment";
                }
            ?>  
            </div>
            </td>
            <td class="tdPayment" id="owes">
            <div>
            <?php
                if ($owes_name == true) {
                    echo $owes_name;
                } else {
                    echo "Everyone has paid something";
                }
            ?>  
            </div>
            </td>
        </tr>
    </table>

I drag and drop the table cells like this..

 $(function() {
        $( "#paid, #partially_paid, #owes" ).sortable({
          connectWith: ".tdPayment",
          remove: function(e, ui) {
            var $this = $(this);
            var childs = $this.find('div');
            if (childs.length === 0) {
               $this.text("Nothing");
            }
          },
          receive: function(e, ui) {
            $(this).contents().filter(function() {
                return this.nodeType == 3; //Node.TEXT_NODE
             }).remove();
          },
        }).disableSelection();
      });

My php to output the values is like this. I am going to consolidate the php so it is only one db table rather than three, but this conveys what I am trying to do...

<?php
    //Payment Section

        $con = mysqli_connect("localhost", "root", "", "db");
        $paid_run = mysqli_query($con,"SELECT * FROM paid ORDER BY id DESC");
        $partially_paid_run = mysqli_query($con,"SELECT * FROM partial_payment ORDER BY id DESC");
        $owes_run = mysqli_query($con,"SELECT * FROM owes ORDER BY id DESC");
        $paid_numrows = mysqli_num_rows($paid_run);
        $partially_paid_numrows = mysqli_num_rows($partially_paid_run);
        $owes_numrows = mysqli_num_rows($owes_run);

            if($paid_numrows > 0){
                while($row = mysqli_fetch_assoc($paid_run)){
                    $paid_id = $row['user_id'];
                    $paid_name = $row['name'];
                }
            }

        if($partially_paid_numrows > 0){
                while($row = mysqli_fetch_assoc($partially_paid_run)){
                    $partially_paid_id = $row['user_id'];
                    $partially_paid_name = $row['name'];
                    $partially_paid_amount = $row['payment'];
                }
            }

        if($owes_numrows > 0){
                while($row = mysqli_fetch_assoc($owes_run)){
                    $owes_id = $row['user_id'];
                    $owes_name = $row['name'];
                }
            }
    ?>  

How can I store the user_id, so that I can update the db with php in this drag and drop table?

Aucun commentaire:

Enregistrer un commentaire