如何在循环外显示数组变量

时间:2018-07-13 03:30:49

标签: php

你好,我有这样的代码                           

                <div class="table-wrapper-2">

                    <!--Table-->
                    <table class="table table-striped">
                        <thead class="mdb-color lighten-4">
                            <tr>
                                <th>#</th>
                                <th class="th-lg">Name</th>
                                <th class="th-lg">Surname</th>
                                <th class="th-lg">Country</th>
                                <th class="th-lg">City</th>
                                <th class="th-lg">Position</th>
                                <th class="th-lg">Age</th>
                                <th class="th-lg">Position</th>
                                <th class="th-lg">Position</th>


                            </tr>
                        </thead>
                        <tbody>
                        <?php
                        $a='TAJUL';
                           $stid = oci_parse($conn, 'SELECT def_usr,problem,solution,def_date,closed_date FROM hd_report  where def_usr=:num order by def_date DESC');
                       oci_bind_by_name($stid, ":num", $a);
                          oci_execute($stid);
                       $count = 0;

                                 while (($row = oci_fetch_row($stid)) != false) {
                                   $def_usr=$row[0];
                                   $problem=$row[1];
                                   $solution=$row[2];
                                   $def_date=$row[3];
                                   $closed_date=$row[4];
                                   $count=$count+1;
                             ?>
                            <tr>
                                <th scope="row"><?php echo $count ?></th>
                                <td><?php echo $def_usr ?></td>
                                <td><?php echo $def_date ?></td>
                                <td><?php echo $closed_date ?></td>
                                <td><?php echo $problem ?></td>
                                            <td>
                                            <button type="button" href="#myModalDelete"
                                                    data-toggle="modal" class="btn btn-danger"
                                                    data-problem="<?php $problem?>">
                                                    <span class="glyphicon glyphicon-trash"></span>Detail
                                            </button>
                                            </td>           


        <?php
        }
        ?>      
                            </tr>

                        </tbody>

                </table>
                    <!--Table-->

                </div>

            </div>
        </div>

    <!-- Start Modal for Delete -->
        <div id="myModalDelete" class="modal fade">
            <div class="modal-dialog">
                <div class="modal-content">

                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">&times;</button>
                        <h4 class="modal-title">Alert message !!!</h4>
                    </div>

                    <div class="modal-body">

                        <!-- The form is placed inside the body of modal -->
                        <form method="post" action="view_detailquestion">

                            <h3>Are you sure you want to View this question ?</h3>

                            <div class="form-group">
                                <label>ID</label> <input type="text" class="form-control"
                                         name="problem" value="<?php echo $problem?>" readonly>
                            </div>

                            <button type="submit" class="btn btn-danger" name="action"
                                value="view">view</button>

                        </form>

                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    </div>

                </div>
            </div>
        </div>
    </div>

我的问题是我想在循环之外回显$ problem。当我这样做时,它仅包含1个值。所以我在想是否有任何解决方案可以使$ problem不在括号内。在开始删除的模态中,我尝试回显$ problem,但是结果很喜欢我说只携带1个值,而不是数组... 所以我希望你们能成为我的向导,谢谢。...

4 个答案:

答案 0 :(得分:0)

然后您只需要使$ problem成为数组即可。将行更改为$problems[] = $row[1];

答案 1 :(得分:0)

extension ViewController: CustomCellDelegate {
   func someFunctionToPassSomeValue(name: String) {
      print("Delegate is working. Value : \(name)")
   }
}

$(document).ready(function(){
    $("#myBtn").click(function(){
      var problem_statement = $(this).data('problem');
      $(".modal-body #problemText").val( problem_statement );
      $("#myModalDelete").modal('show');
    });
});

您可以使用一些JavaScript尝试一下吗?看来您的模态弹出窗口中需要这个$问题吗? IMO将“ id”添加到您的按钮,在javascript中以及使用模式弹出窗口在回调集数据中附加click事件监听器。

答案 2 :(得分:0)

就像这样:

$problem=array();
while (($row = oci_fetch_row($stid)) != false) {
  $problem[]=$row[1];
}

foreach($var in $problem){
  echo $var . "<br />"
}

答案 3 :(得分:0)

当前,您正在覆盖先前的值,同时将数据添加到$ problem变量中。因此,您必须为此创建数组,并且需要将下一个值附加到现有数组中而不是覆盖。 示例:

while (($row = oci_fetch_row($stid)) != false) {
      $def_usr[]     =$row[0];
      $problem[]     =$row[1];
      $solution[]    =$row[2];
      $def_date[]    =$row[3];
      $closed_date[] =$row[4];
      $count=$count+1;
}

在此while循环中,您将获得$problem的所有数组。如果您想在其他地方展示,请使用foreach loop

示例:

if(!empty($problem)) {
    foreach($problem as $value ) {
       echo $value;
    }
}