duanchi4184 2017-10-25 06:43
浏览 31
已采纳

在循环php7中的视图中访问对象方法

I'm pulling an array of objects from my database and iterating through them using PHP to display a table. Each result row of the table has the option of deleting that particular record from the database. Each row is also an object, and the object type has a method that will delete itself from the database.

The problem I'm having is thinking through how to get this to work in the view. Right now if you click the delete button, it sets a POST variable to "delete" and reloads the same page wherein a listener process detects the variable and initiates the delete via a hidden input that contains the records database id.

This is all well and good, but these are all objects. Shouldn't each object be able to access it's own methods? It seems the only barrier to this is the interplay between PHP script and HTML forms. My code is below. Ideally, when the delete button is pressed, that particular instance of the object teamleader can run its method teamleader->delete_team_leader().

        <?php foreach($teamleaders as $teamleader) { ?>
    <tr>
        <td><?php echo $teamleader->first_name; ?></td>
        <td><?php echo $teamleader->last_name; ?></td>
        <td><img src="<?php echo '../img/' . $teamleader->photo;?>" alt="" class="img-fluid tiny-tl-img"></td>
        <td><?php echo nl2br(substr($teamleader->bio, 0, 100));?>...</td>
        <td><?php echo $teamleader->url_name;?></td>
        <form action="index.php?a=edittl" method="post">
            <td>
                <input type="hidden" name="id" value="<?php echo $teamleader->id?>">
                <input type="submit" class="btn btn-default" name="edit" value="Edit">
            </td>
        </form>
        <form action="index.php" method="post">
            <td>
                <input type="hidden" value="<?php echo $teamleader->id?>" name="id">
                <input type="submit" class="btn btn-danger" name="delete" value="Delete" onclick="return confirm('Are you sure you want to delete this team leader?')">
            </td>
        </form>
    </tr>
    <?php } ?>
  • 写回答

1条回答 默认 最新

  • douturan1807 2017-10-25 07:32
    关注

    In your case, the best solution I think you can use ajax/jQuery for editing or deleting action. You don't need to use many forms as now.

    The code in front-end like this:

    <head>
    <script src="jquery.min.js"></script>
    </head>
    ...
    <?php foreach($teamleaders as $teamleader) { ?>
        ...
        <td><?php echo $teamleader->url_name;?></td>        
        <td>
            <input type="submit" class="btn btn-default btn-edit" name="edit" value="Edit" data-teamleaderid="<?php echo $teamleader->id?>">
        </td>
        <td>
            <input type="submit" class="btn btn-danger btn-delete" name="delete" value="Delete" data-teamleaderid="<?php echo $teamleader->id?>">
        </td>
    </tr>
    <?php } ?>
    

    And the code with using ajax/jQuery is:

    $(document).ready(function(){
       $('.btn-delete').on('click',function(){
          var _this = $(this);
          var _teamleaderid = $(this).data('teamleaderid');
    
          if (!confirm('Are you sure you want to delete this team leader?')) return false;
          $.ajax(
              url:'delete.php',
              type:'post',
              data: {
                  teamleaderid: _teamleaderid
              },
              dataType:'json',
              success:function(data) {
                 if (data.return) {
                     $(_this).parent().parent().remove();/*Remove the current row in table html*/
                     alert('This teamleader has been deleted');
                 }
              }
          );
       });
    });
    

    The delete.php code in backend is:

    <?php 
    ... #connect database
    $teamleader_id = filter_input(INPUT_POST,'teamleader');
    $check_delete = $dbconn->query("DELETE from teamleader_table WHERE id={$teamleader_id}");
    print json_encode('return'=>$check_delete);
    die();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog