如何从foreach打印单个行?

时间:2018-03-03 06:11:01

标签: javascript php jquery codeigniter

我在php中有一个foreach数组,我正在显示有很多行的记录,我的视图文件如下,

         <?php
          $count = 1;
          foreach ($resultlist as $student) {
            ?>
            <div class="slide-row" id="printableArea" style="padding:30px;border:2px solid #999;">
              <div class="row">
                <div class="col-md-10">
                  <div class="row">
                    <div class="col-md-4">
                      <label> Name: </label>
                      <?php echo $student['firstname']; ?>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-md-5">
                      <label> Standard: </label>
                      <?php echo $student['class']; ?>
                    </div>
                    <div class="col-md-5">
                      <label> Section: </label>
                      <?php echo $student['section']; ?>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-md-5">
                      <label> Admission No: </label>
                      <?php echo $student['admission_no']; ?>
                    </div>
                    <div class="col-md-5">
                      <label> House: </label>
                      <!-- <?php echo $student['section']; ?> -->
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-md-5">
                      <label> Religion: </label>
                      <?php echo $student['religion']; ?>
                    </div>
                    <div class="col-md-5">
                      <label> Caste: </label>
                      <?php echo $student['cast']; ?>
                    </div>
                  </div>
                </div>
                <div class="col-md-2">
                  <img class="img-responsive img-thumbnail width150" alt="Cinque Terre" src="<?php echo base_url(); ?>backend/dist/img/jeevaschool.png" alt="Image">
                </div>
              </div>
              <br>
              <table class="table table-bordered">
                <thead>
                  <tr>
                    <th>Community</th>
                    <th>DOB</th>
                    <th>Age</th>
                    <th>Blood Group</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td><?php echo $student['community']; ?></td>
                    <td><?php echo $student['dob']; ?></td>
                    <td></td>
                    <td><?php echo $student['blood_group']; ?></td>
                  </tr>
                </tbody>
              </table>
              <div class="row">
                <div class="col-md-4">
                  <label> Name of the Father: </label>
                  <?php echo $student['father_name']; ?>
                </div>
                <div class="col-md-4">
                  <label> Occupation: </label>
                  <?php echo $student['father_occupation']; ?>
                </div>
                <div class="col-md-4">
                  <label> Name of the Mother: </label>
                  <?php echo $student['mother_name']; ?>
                </div>
              </div>
              <div class="row">
                <div class="col-md-4">
                  <label> Occupation: </label>
                  <?php echo $student['mother_occupation']; ?>
                </div>
                <div class="col-md-4">
                  <label> Mobile Number: </label>
                  <?php echo $student['mobileno']; ?>
                </div>
                <div class="col-md-4">
                  <label> Email ID: </label>
                  <?php echo $student['email']; ?>
                </div>
              </div><br><br>
            </div>
            <button class="btn btn-primary" onclick="printDiv('printableArea');">Print</button>
            <br><br><br>
              <?php
            }
            $count++;
          }
          ?>
      </div>

记录逐个显示,我需要通过单击相应行上的打印按钮单独打印每一行。

我有id

的html
<div class="slide-row" id="printableArea" style="padding:30px;border:2px solid #999;">

并打印按钮

<button class="btn btn-primary" onclick="printDiv('printableArea');">Print</button>

JS

  function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

我尝试使用上面的一个,它只会打印第一行记录,但我需要为每一行获取相应的打印结果。

1 个答案:

答案 0 :(得分:1)

Id在页面中应该是唯一的。根据您的代码,您的网页中会有许多ID为printableArea的元素。相反,您可以添加动态ID。

例如,将您的计数变量添加到Ids

<div class="slide-row" id="printableArea_<?php echo $count; ?>" style="padding:30px;border:2px solid #999;">

在按钮中

<button class="btn btn-primary" onclick="printDiv('printableArea_<?php echo $count; ?>');">Print</button>