如何在Model中获取PHP中的行值

时间:2015-11-23 05:49:39

标签: php mysql

每当我点击该行时,我想获取表格的行值。 在这里我必须在行中显示表的数据.. 每当我点击该行时,我需要返回该行的其他变量值..

喜欢当我点击主题时,我需要从数据库中显示相应主题的消息。

PHP代码从数据库返回行值。

此代码段不会运行,因为它是PHP代码(只需将snipppet添加到良好的格式中)

enter image description here enter image description here

在此图片中,弹出模型未显示正确的主题和消息。 我需要弹出窗口中的这个特定主题和消息

Database Structure

<?php
include('../config/conn.php');

$sql = "SELECT * FROM helpdesk where user_id='$user_id'";
$result = $conn->query($sql);
$sr=1;
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $user_id=$row["user_id"];
        $pooler_id=$row["pooler_id"];
        $date=$row["date"];
        $subject=$row["subject"];
        $req=$row['req_id'];
        $message=$row['message'];

        $sql1 = "SELECT * FROM pooler where id='$pooler_id' ";
        $result1 = $conn->query($sql1);
        if ($result1->num_rows > 0) {
            // output data of each row
            while($row1 = $result1->fetch_assoc()) {
                $username=$row1['user_name'];
                $email=$row1['email'];
            }
        }

        echo ' <tr>
                          <td>'.$sr.'</td>
                          <td>'.$username.'</td>
                          <td><a href="#" data-toggle="modal" data-target="#myModal">'.$subject.'</td>
                          <td>'.$date.'</td>


                        </tr>';
        $sr++;
    }
} else {
    echo "0 results";
}
?>

模态POP代码,应显示发件人姓名,主题和消息。

    

  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title"> <?php echo $username; ?> <br><br> <?php echo $subject;  ?>  </h4>
    </div>
    <div class="modal-body">
      <p>
      <?php 
      echo $message;
      ?>
      </p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>

</div>

2 个答案:

答案 0 :(得分:1)

在你的HTML代码中

<td><a href="#" data-toggle="modal" data-target="#myModal" onclick="showData('.$pooler_id.')">'.$subject.'</td>

请在javascript函数中传递您的唯一ID。

模态

  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title"> 
      <span id="user_name"><?php echo $username; ?></span> <br><br> <span id="subject"><?php echo $subject;  ?></span>  </h4>
    </div>
    <div class="modal-body">
      <p id="msg">
      <?php 
      echo $message;
      ?>
      </p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>

脚本

<script>
function showData(pooler_id)
{
    $.ajax({
        type: POST,
        url : 'some_file.php',
        data: {pooler_id: pooler_id},
        success: function(response){
            var resp = JSON.parse(response);
            $('#user_name').html(resp.user_name);
            $('#subject').html(resp.subject);
            $('#msg').html(resp.msg);
        }
    });
}
</script>

在some_file.php中

$pooler_id = $_POST['pooler_id'];
//get records of your primary key json_decode it
//get details from database
//this will be result fetched from database
$responseData['user_name'] = $name;
$responseData['subject'] = $subject;
$responseData['msg'] = $msg;


echo json_decode($responseData);

答案 1 :(得分:0)

如果您的问题尚未解决,并且您尝试避免使用JSON:

您可以尝试将个人ID(例如$sr)添加到您的<div class="modal-content"> ID属性和<a href="#" data-toggle="modal" data-target="#myModal">'.$subject.'视图中。从http://www.webdesignerdepot.com/2012/10/creating-a-modal-window-with-html5-and-css3/

得到了这个

也许这适合你。

添加到您的PHP代码<a href="#modal-content-'.$sr.'" data-toggle="modal" data-target="#myModal">'.$subject.'

<?php
include('../config/conn.php');

$sql = "SELECT * FROM helpdesk where user_id='$user_id'";
$result = $conn->query($sql);
$sr=1;
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $user_id=$row["user_id"];
        $pooler_id=$row["pooler_id"];
        $date=$row["date"];
        $subject=$row["subject"];
        $req=$row['req_id'];
        $message=$row['message'];

        $sql1 = "SELECT * FROM pooler where id='$pooler_id' ";
        $result1 = $conn->query($sql1);
        if ($result1->num_rows > 0) {
            // output data of each row
            while($row1 = $result1->fetch_assoc()) {
                $username=$row1['user_name'];
                $email=$row1['email'];        //note this value is never used

                echo ' <tr>
                           <td>'.$sr.'</td>
                           <td>'.$username.'</td>
                           <td>
                               <a href="#modal-content-'.$sr.'" data-toggle="modal" data-target="#myModal">'.$subject.'</a>
                           </td>
                           <td>'.$date.'</td>
                       </tr>';
                $sr++;
            }
        }
    }
} else {
    echo "0 results";
}
?>

以及您的模态<div class="modal-content-<?php echo $sr; ?>">

<!-- Modal content -->
<div class="modal-content-<?php echo $sr; ?>">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title"> 
        <span id="user_name"><?php echo $username; ?></span> <br><br> <span id="subject"><?php echo $subject;  ?></span>  </h4>
    </div>
    <div class="modal-body">
        <p id="msg"><?php echo $message; ?></p>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
</div>