【问题标题】:php code causing issue to bootstrap Modal in Codeigniterphp代码导致在Codeigniter中引导模态的问题
【发布时间】:2017-04-16 05:38:21
【问题描述】:

我已经创建了一个简单的 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">&times;</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 代码添加到模态正文表中以显示数组中的数据,模态就会停止工作,它会停止出现,并且面板内容也会消失。

但是一个简单的 echo 语句不会导致任何问题,但是 if 或 foreach 语句会导致 Modal 出现问题。

这里有什么我遗漏的吗? 或者这不是应该的样子?

有人可以帮忙吗。

【问题讨论】:

  • 模态内容消失是什么意思?你能在模态不工作时发布重新生成的 html,我猜是有一些错误,并且 php 正在向你的 html 呈现错误消息,这会破坏 IT。
  • 面板内容
    这个 div 不会出现在 html 中,它不是 modal 的一部分,而是一个 div
  • 您是否尝试将$a=$allInterests-&gt;num_rows(); 放在table 标记之外,然后使用其中的值?这可能是问题所在。
  • 它在模态代码之外工作。但我需要将它放在模态体内。 :(
  • 我还是很想知道它在渲染什么,我认为在问题上一定有一些 php 错误导致行

标签: php twitter-bootstrap codeigniter bootstrap-modal


【解决方案1】:

$allInterests 是一个变量。

如果您必须访问 $allInterests,您必须像访问普通变量或对象一样访问它。

如果 $allInterests 是一个数组,

你可以访问它,

If (!empty($allInterests))
{
     for($i=0;$i<count($allInterests);$i++)
     { 
            echo $allInterests[$i];
     }
}

如果它是一个对象,您可以使用 foreach 循环。

您必须像处理普通变量或对象一样处理它。

希望你有一个想法。

【讨论】:

  • 我认为您没有理解问题所在。问题是我无法在 modal-body 中添加任何 php 代码。就像一个简单的 if 或 foreach 语句
  • 你能解释一下什么是num_rows(),它是codeigniter的函数吗?
【解决方案2】:

我找到了解决方案。问题在于 $allInterests 变量为空,因此导致它在模态中创建问题。

在该变量中传递适当的数据使其工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2012-06-04
    相关资源
    最近更新 更多