【问题标题】:Get ID from PHP loop and use in a MYSQL query in a bootstrap modal从 PHP 循环中获取 ID 并在引导模式中的 MYSQL 查询中使用
【发布时间】:2015-04-19 18:27:38
【问题描述】:

我有一个 php 循环,它从 mysql 表中收集所有数据并将其显示在 html 表中。在该表中,每一行都有一个详细信息按钮'

当用户单击详细信息按钮时,会出现一个模式。

这是我的代码

<table id="open" class="table table-bordered table-striped">
<thead>
   <tr>
      <th style="text-align: center;">Job Title</th>
      <th style="text-align: center;">Assigned To</th>
      <th style="text-align: center;">Name Or Class</th>
      <th style="text-align: center;">Priority</th>
      <th style="text-align: center;">Date</th>
      <th style="text-align: center;">Added By</th>
      <th style="text-align: center;">Days Open</th>
      <th style="text-align: center;">Replies</th>
      <th style="text-align: center;">Actions</th>
    </tr>
</thead>
<tbody>
<?php 
    $getopenjobs = mysql_query("SELECT support.id, support.title, support.description, departments.name, support.assigned_to, support.name_class, support.priority, support.datetime, users.firstname, users.lastname, support.status, COUNT(support_replies.job_id) AS replies, DATEDIFF(CURDATE(), support.datetime) as 'difference', support_replies.reply
                FROM support JOIN departments ON support.assigned_to = departments.id JOIN users ON support.added_by = users.id LEFT JOIN support_replies ON support.id = support_replies.job_id WHERE support.status ='1' GROUP BY id");
    $count = 0;
    echo '<tr>';
    if (mysql_num_rows($getopenjobs) == 0) {  
       echo '<td colspan="9">No Open Jobs Found</td>'; 
    } else {
       while ($row = mysql_fetch_array($getopenjobs))
       {
            echo '<td style="';

            if($row['assigned_to'] == '1') {
                echo 'background: #00C0EF; ';
            }

            if($row['assigned_to'] == '2') {
                echo 'background: #F39C12; ';
            }

            if($row['assigned_to'] == '3') {
                echo 'background: #00A65A; ';
            }

            if($row['assigned_to'] == '4') {
                echo 'background: #F56954; ';
            }

            echo 'text-align: left;">' . $row['title'] . '</td>';
            echo '<td style="';

            if($row['assigned_to'] == '1') {
                echo 'background: #00C0EF; ';
            }

            if($row['assigned_to'] == '2') {
                echo 'background: #F39C12; ';
            }

            if($row['assigned_to'] == '3') {
                echo 'background: #00A65A; ';
            }

            if($row['assigned_to'] == '4') {
                echo 'background: #F56954; ';
            }


            echo 'text-align: center;">' . $row['name'] . '</td>';

            echo '<td style="';

            if($row['assigned_to'] == '1') {
                echo 'background: #00C0EF; ';
            }

            if($row['assigned_to'] == '2') {
                echo 'background: #F39C12; ';
            }

            if($row['assigned_to'] == '3') {
                echo 'background: #00A65A; ';
            }

            if($row['assigned_to'] == '4') {
                echo 'background: #F56954; ';
            }


            echo 'text-align: center;">' . $row['name_class'] . '</td>';

            echo '<td style="';

            if($row['assigned_to'] == '1') {
                echo 'background: #00C0EF; ';
            }

            if($row['assigned_to'] == '2') {
                echo 'background: #F39C12; ';
            }

            if($row['assigned_to'] == '3') {
                echo 'background: #00A65A; ';
            }

            if($row['assigned_to'] == '4') {
                echo 'background: #F56954; ';
            }

            echo 'text-align: center;">';

            if($row['priority'] == '1') {
                echo 'Low';
            }

            if($row['priority'] == '2') {
                echo 'Medium';
            }

            if($row['priority'] == '3') {
                echo '
                High';
            }

            if($row['priority'] == '4') {
                echo '
                Very High';
            }

            if($row['priority'] == '5') {
                echo '
                Critical';
            }
            echo '<td style="';

            if($row['assigned_to'] == '1') {
                echo '
                background: #00C0EF; ';
            }

            if($row['assigned_to'] == '2') {
                echo '
                background: #F39C12; ';
            }

            if($row['assigned_to'] == '3') {
                echo '
                background: #00A65A; ';
            }

            if($row['assigned_to'] == '4') {
                echo '
                background: #F56954; ';
            }


            echo 'text-align: center;">' . $row['datetime'] . '</td>';

            echo '<td style="';

            if($row['assigned_to'] == '1') {
                echo '
                background: #00C0EF; ';
            }

            if($row['assigned_to'] == '2') {
                echo '
                background: #F39C12; ';
            }

            if($row['assigned_to'] == '3') {
                echo '
                background: #00A65A; ';
            }

            if($row['assigned_to'] == '4') {
                echo '
                background: #F56954; ';
            }


            echo 'text-align: center;">' . $row['firstname'] . ' ' . $row['lastname'] . '</td>';

            echo '<td style="';

            if($row['assigned_to'] == '1') {
                echo '
                background: #00C0EF; ';
            }

            if($row['assigned_to'] == '2') {
                echo '
                background: #F39C12; ';
            }

            if($row['assigned_to'] == '3') {
                echo '
                background: #00A65A; ';
            }

            if($row['assigned_to'] == '4') {
                echo '
                background: #F56954; ';
            }


            echo 'text-align: center;"><span style="color: black;" class="step">' . $row['difference'] . '</span></td>';

            echo '<td style="';

            if($row['assigned_to'] == '1') {
                echo '
                background: #00C0EF; ';
            }

            if($row['assigned_to'] == '2') {
                echo '
                background: #F39C12; ';
            }

            if($row['assigned_to'] == '3') {
                echo '
                background: #00A65A; ';
            }

            if($row['assigned_to'] == '4') {
                echo '
                background: #F56954; ';
            }


            echo 'text-align: center;"><span style="color: black;" class="step">' . $row['replies'] . '</span></td>';

            echo '<td style="';

            if($row['assigned_to'] == '1') {
                echo '
                background: #00C0EF; ';
            }


            if($row['assigned_to'] == '2') {
                echo '
                background: #F39C12; ';
            }

            if($row['assigned_to'] == '3') {
                echo '
                background: #00A65A; ';
            }

            if($row['assigned_to'] == '4') {
                echo '
                background: #F56954; ';
            }


            echo 'text-align: center;"><a data-datetime="' . 
                 $row['datetime'] . '" data-recipientname="' . 
                 $row['firstname'] . ' ' . $row['lastname'] . '" 
                 data-id="' . $row['id'] . '" data-title="' . 
                 $row['title'] . '" data-desc="' . $row['description'] . 
                 '" data-toggle="modal" data-target="#details" 
                 class="open-details btn btn-primary btn-sm">Details</a> </td>';
            echo '</tr>';
            $count++;
            }


            echo '<div class="modal fade" id="details" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <div class="modal-body">
                        <!-- Chat box -->
                        <div class="box-body chat" id="chat-box">


            <!--JOB -->
            <div style="position: left; width: 80%; background: #F2DEDE; border: 1px solid #A94442; border-radius: 8px; padding-left: 10px; padding-right: 10px; padding-top: 10px;" class="item">
               <img src="img/avatar-placeholder.png" alt="user image"/>
               <p class="message">
                  <a href="#" class="name">
                     <small class="time text-muted pull-right"><i  class="fa fa-clock-o"></i></small>
                    <p class="recipientname"></p>
                  </a>
                  <p class="title"></p></br>

                  <p class="description"></p>
                </p>
             </div><!-- /.JOB -->';

    $query = ("SELECT support_replies.reply, support_replies.datetime, 
                      users.firstname, users.lastname 
               FROM support_replies 
                 JOIN users on support_replies.added_by = users.id 
               WHERE support_replies.job_id = '" . $row['id'] . "' 
               ORDER BY support_replies.id DESC");
    $row =  mysql_query($query);    
    while( false !== ($replies = mysql_fetch_assoc($row)))
    {
        echo '<ul style="list-style:none; margin-left: 0; padding-left: 0;">';
        echo '<li>';

        echo '<div style="width: 80%; background: #DFF0D8; border: 1px solid #46763D; border-radius: 8px; padding-left: 10px; padding-right: 10px; padding-top: 10px; margin-left: 20%;" class="item">
               <img src="img/avatar-placeholder.png" alt="user image"/>
               <p class="message">
                  <a href="#" class="name">
                      <small class="text-muted pull-right"><i class="fa fa-clock-o"></i> ' . $replies['datetime'] . '</small>
                      ' . $replies['firstname'] . ' ' . $replies['lastname'] . '
                  </a>
                  ' . $replies['reply'] . '
                </p>
                </div><!-- /.item -->';

            echo '</li>';
            echo '</ul>';
        }   
        echo  '</div><!-- /.chat -->
               <div class="box-footer">
                  <div class="input-group">
                      <input class="form-control" placeholder="Type reply ..."/>
                       <div class="input-group-btn">
                            <button class="btn btn-success"><i class="fa fa-plus"></i></button>
                       </div>
                  </div></div>
                </div>
              </div>
        </div>
     </div>';
    }
?>
</tbody>
<tfoot>
    <tr>
        <th style="text-align: center;">Job Title</th>
        <th style="text-align: center;">Assigned To</th>
        <th style="text-align: center;">Name Or Class</th>
        <th style="text-align: center;">Priority</th>
        <th style="text-align: center;">Date</th>
        <th style="text-align: center;">Added By</th>
        <th style="text-align: center;">Days Open</th>
        <th style="text-align: center;">Replies</th>
        <th style="text-align: center;">Actions</th>
    </tr>
</tfoot>

我希望能够获取用户单击详细信息按钮的当前表行的 ID 并使用模式中的 ID,我想在模式中运行另一个 MYSQL 查询并将 ID 用作MYSQL 在哪里。

例如

SELECT * FROM TABLE WHERE ID = "HERE WILL GO THE ID PULLED THROUGH"

我知道可以传递数据并只显示它,因为我已经这样做了,但我不想显示它我想在新的 mysql 查询中使用它。

如何根据表格行 ID 获取模型中所有回复的列表

【问题讨论】:

  • 下次编辑您的代码,以便无需左右滚动即可阅读。我们需要这么多代码吗!

标签: php jquery mysql twitter-bootstrap modal-dialog


【解决方案1】:

您可以这样编写,以便单击详细信息按钮触发 ajax 调用以获取单独的页面,例如/some/other/page?id=XXXX.

在该单独页面上,您使用传入的 ID 运行查询,然后返回内容(HTML/JSON 等)并在模式中显示。

【讨论】:

  • 你能举个例子吗?
  • 在您的代码中,您可以使用&lt;a ... data-toggle="modal" data-target="#details" ...&gt;Details&lt;/a&gt;,如果您只是将 href 属性添加到其中,例如&lt;a href="/some/other/page?id=XXXX" ... data-toggle="modal" data-target="#details" ...&gt;Details&lt;/a&gt; 然后应该加载其他页面的内容。正如我之前所说,您的单独页面可以在返回内容之前执行任何操作(例如运行您的 SELECT 查询)。
  • 我已经做到了 :) 谢谢你,没有你我不会做到的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 2014-02-11
  • 2018-05-17
相关资源
最近更新 更多