1

I have a table, where I get the Data from the DB. Now, I want to edit each of the rows separately and save the edited value in DB. I browsed in google and came to know that it can be done using JS/Jquery, but I am not able to figure out exactly how to do it.

I am new to PHP so I am not able to figure out how to do this.

My code for the Table and the values generated are:

<?php

$menu = $conn -> query("Select * from menu"); ?>

<table class="table table-hover">

<tr>

    <th>Item ID</th>
    <th>Item Name</th>
    <th>Item Description</th>
    <th>Item Price</th>
    <th>Item Qty</th>
    <th>Item Type</th>
    <th>Status</th>
    <th>Manage</th>
</tr>
<?php
 foreach ($menu as $row) {
    echo "<tr>";
    echo "<td>".$row['ItemId']."</td>";
    echo "<td>".$row['ItemName']."</td>";
    echo "<td>".$row['ItemDesc']."</td>";
    echo "<td>".$row['ItemPrice']."</td>";
    echo "<td>".$row['ItemQty']."</td>";
    echo "<td>".$row['ItemType']."</td>";   ?>
    <td class="dropdown">
        <div class="btn-group" style="width: 100%; margin-bottom: 10px;">
            <button type="button" class="btn btn-danger btn-block btn-sm dropdown-toggle color-chooser-btn" data-toggle="dropdown">Status <span class="caret"></span></button>
            <ul class="dropdown-menu color-chooser">
                <li><a class="text-green" href="#"><i class="fa fa-square"></i> Available </a></li>
                <li><a class="text-red" href="#"><i class="fa fa-square"></i> Not Available</a></li>
            </ul>
        </div>

    </td>
    <td>
        <a data-toggle="modal" data-target="#compose-modal">
            <button class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i></button></a>
        <a data-toggle="modal" data-target="#delete-modal">
            <button class="btn btn-primary btn-xs"><i class="fa fa-trash-o"></i></button></a>
    </td>
    <?php
    echo "</tr>";
    } ?>
   </table>
2
  • what about status? do you want to change it dynamically from each row(like a toggle button)? Commented Feb 14, 2016 at 16:59
  • Yeah.. status will be changed like a toggle button dynamically from each row. Commented Feb 14, 2016 at 17:32

1 Answer 1

1

Here we have list.php.And we can make the status like a toggle button. Try like this.Here i hope you are using Bootstrap framework.

    <div class="row">
      <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9" style="padding-right:50px;">
        <div class="table-responsive">
           <table class="table table-striped">
             <thead>
               <tr>
                <th style="text-align:left">Item ID</th>
                <th style="text-align:left">Item Name</th>
                <th style="text-align:left">Item Description</th> 
                <th style="text-align:left">Item Price</th>                                
                <th style="text-align:left">Item Qty</th>
                <th style="text-align:left">Item Type</th>
                <th style="text-align:left">Status</th>
                <th style="text-align:left">Manage</th>
              </tr>
            </thead>
            <tbody id="content-body">

            </tbody>
          </table>
       </div><!-- Closed tabel responsive -->
     </div>

Now we have the list template.

and the jquery functions are below

    listItems:function(){
    _this=this;
        $.ajax({
          url:"listitem.php", // data should come from here
          type:"GET",
          success:function(data){
            if(!data.error)
            {
                _this.renderList(data); //sending data to another function.
            }
            else
            {
             $("#content-body").html("<tr><td colspan='11'>"+data.error+"</td></tr>"); // showing error
            }

        },
        error:function(data){
        $("#content-body").html("<tr><td colspan='11'>No item available.</td></tr>"); //showing error
        }
      });
    },
    renderList:function(data){ // here we will receive data and list
      _this=this;           
    var obj = data;//console.log(obj);  

    if(obj.length < 15)//no of items per page for pagination
    {
        $(".next").addClass("disabled");
    }else
    {
        $(".next").removeClass("disabled");
    }

    var testcontent = "";
    linkstatus = '<a class="anchorstatus" href="#">';
    linkmanage = '<a class="anchoredit" href="#">';


    var row='<tr id={itemid}>'+
    '<td>{ItemName}</a></td>'+
    '<td>{ItemDesc}</a></td>'+
    '<td>{Itemprice}</a></td>'+
    '<td>{ItemQty}</a></td>'+
    '<td>{ItemType}</a></td>'+
    '<td>'+linkstatus+'{Itemstatus}</a></td>' +
    '<td>'+linkedit+'Edit</a></td>' +
    "</tr>";

    for(i in obj)
    {
        str=row;

        for(j in obj[i])
        {
            thisvalue=obj[i][j];

            if(thisvalue===null)
            thisvalue="Not Defined";

            if(thisvalue==="Available")
                thisvalue="<p class='ok'>"+thisvalue+"</p>";

            if(thisvalue==="NotAvailable")
                thisvalue="<p class='notice'>"+thisvalue+"</p>";
            var test = "{"+j+"}";
            str=str.replace("{"+j+"}",thisvalue);

        }

        testcontent+=str;
    }
    $("#content-body").html(testcontent); 
},
statusEvent:function(){ // for changing the status dynamically.
    _this=this;

    $("body").on("click",".anchorstatus",function(){
        status=$(this).text();
        id=$(this).parents("tr").attr("id");

        if(status=="Available") // checking status
        {
            statusvalue="NotAvailable";
    _this.updateItem({  // sending status to update db.
        "itemstatus":"NotAvailable"},id,statusvalue);  
        }
        else
        {
            statusvalue="Available";
              _this.updateItem({ // sending status to update db.
    "itemstatus":"Available"},id,statusvalue);
        }
    });
  },
   updateItem(contentdata,itemid,statusvalue){ // updating data

    $.ajax({
        url: "admin/v1/list.php/"+itemid,
        type: "PUT",
        data: JSON.stringify(contentdata),
        success: function(data){
            if(statusvalue==="Available")
                statusvalue="<p class='ok'>"+statusvalue+"</p>";

            if(statusvalue==="NotAvailable")
                statusvalue="<p class='notice'>"+statusvalue+"</p>";

            if(data.success)
            {
                $("#"+id+" .anchorstatus").html(statusvalue);   // success message                  
            }
        },
        error:function(data){
            console.log(data);  
            if(data.status === 401)
            {
                console.log(data.status);
                showError("Session Expired");
                setTimeout(function (){
                  window.location="index.php";        
              },1000);
            }           
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

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.