[英]php code causing issue to bootstrap Modal in Codeigniter
我創建了一個簡單的mvc程序。
下面是查看代碼
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="<?php echo base_url('assets/css/bootstrap.min.css'); ?>" rel="stylesheet" type="text/css"/>
<link href="<?php echo base_url('assets/css/bootstrap.css'); ?>" rel="stylesheet" type="text/css"/>
</head>
<body>
<div style="margin-left: 5px" class="panel panel-info">
<div style="text-align:left;" class="panel panel-heading">Main
<a id="lnkEditInterests" style="float:right" href="#" data-toggle="modal" data-target="#myModal">
<span class="glyphicon glyphicon-pencil"></span>
</a>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">
<table>
<?php
// this is what causing issue.
$a=$allInterests->num_rows();
?>
<tr>
<td> </td>
</tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-body">Panel Content</div>
</div>
<script src="<?php echo base_url('assets/js/bootstrap.js'); ?>" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>
在上面的視圖中,$ allInterests變量是從下面的控制器傳遞的。
以下是控制器的代碼
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Profile extends CI_Controller
{
public function main()
{
$this->load->model('Model_Profile');
$data["allInterests"]= $this->Model_Profile->loadAllInterests();
$this->load->view('PROFILE\view_ProfileInterests',$data);
}
}
從控制器,我傳遞了具有行集的$ data數組。
我需要以表格格式在模態上顯示此數據。
但是,一旦我將php代碼添加到模態主體表中以顯示數組中的數據,模態就會停止工作,它停止出現,並且該div標簽的面板內容也會消失。
但是,簡單的echo語句不會導致任何問題,但是if或foreach語句會導致Modal出現問題。
我在這里想念什么嗎? 還是這不是應該的樣子?
有人可以幫忙嗎?
$ allInterests是一個變量。
如果必須訪問$ allInterests,則必須像普通變量或對象一樣訪問它。
如果$ allInterests是一個數組,
您可以像這樣訪問
If (!empty($allInterests))
{
for($i=0;$i<count($allInterests);$i++)
{
echo $allInterests[$i];
}
}
如果它是對象,則可以使用foreach循環。
您必須像處理普通變量或對象一樣處理它。
希望你有個主意。
我找到了解決方案。 $ allInterests變量為null,因此導致它以模態創建問題。
在該變量內傳遞適當的數據使其起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.