0

I have created a simple mvc program.

Below is the code for view

<?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>

In above view, $allInterests variable is passed from below controller.

Below is the code for Controller

<?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);

    }


}

From controller I am passing $data array which has set of rows.

I need to show this data on modal in table format.

But as soon as I add php code to within table of modal body to display data which is in array, modal stops working, It stops appearing and also Panel Content this div tag disappears.

However a simple echo statement doesnt cause any issue, But if or foreach statements cause issue to Modal.

Is there anything I am missing here? Or Is this not how it is supposed to be?

Can someone please help.

6
  • What do you mean with modal content disaapearing ? Can you post reaulting html when modal is not working, I gues that there is some error and php is rendering error message to your html, which breaks IT. Commented Apr 16, 2017 at 5:47
  • <div class="panel panel-body">Panel Content</div> This div doesnt appear in html which is not part of modal but a div Commented Apr 16, 2017 at 5:50
  • Have you tried to put the $a=$allInterests->num_rows(); outside the table tag and then using the values within? That may be the problem. Commented Apr 16, 2017 at 5:52
  • It works outside the modal code. But I need to have it inside modal body. :( Commented Apr 16, 2017 at 5:56
  • Still I'm courious what it is rendering, I think there must be some php error rendered on issue causing line Commented Apr 16, 2017 at 6:06

2 Answers 2

1

$allInterests is a variable.

If you have to access $allInterests, you have to access it just like normal variable or object.

If $allInterests is an array,

You can acces it like,

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

If its an object you can use foreach to loop around.

You have to handle it just like a normal variable or object.

Hope you get an idea.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think you have understood what problem is. Problem is that I can't add any piece of php code inside modal-body. like a simple if or foreach statement
Can you explain whats num_rows(), is it a function of codeigniter?
1

I found solution. Issue was with $allInterests variable which was null and hence causing it to create issue in modal.

Passing proper data inside that variable made it work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.