0

I'm not experiencing any difficulty with pulling data from a db table in CodeIgniter and displaying it in a bootstrap modal window. Using this button link;

<a href='#' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a>

I'm able to access the modal window no problem and display the results of this following coding;

<!-- alert view -->
<div class='modal fade' id='myModal' role='dialog'>
<div class='modal-dialog'>

  <!-- Modal content-->
  <div class='modal-content'>
    <div class='modal-header'>
      <?php
      foreach ($article->result() as $a) {?>
      <button type='button' class='close' data-dismiss='modal'>&times;</button>
      <h1 class='modal-title'><?php echo $a->title; ?></h1>
    </div>
    <div class='modal-body'>
      <h4><?php echo $a->category_name; ?></h4>
      <h4><?php echo $a->public_date; ?></h4>
      <h4><?php echo $a->description; ?></h4>
      <?php } ?>
    </div>
    <div class='modal-footer'>
      <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
    </div>
  </div>

</div>

The exact issue I'm having is imposing a limit on the returned results of the foreach() loop. If I use something like $i=0; if($i==1) break; before the foreach(), I get the same content article_id result. To add to my confusion and difficulty, when I try to make article_id the href of the View link, ALL db table entries are pulled into the modal window.

Pictured is the situation the coding is in ATM

I tried working through the query builder chapter in the CI docs dealing with single result arrays as that was most applicable to my situation. That was the basis for the following approach below;

CONTROLLER

$data['a'] = $this->article_model->get_modal();
$this->load->view('article_submit',compact('article', 'page_link', 'category'), $data);  

MODEL

function get_modal()
    {
       $results = array();
       $this->db->select('*');
       $this->db->from($this->tbl_article);

       $query = $this->db->get();

    if($query->num_rows() > 0)
    {
         $results = $query->result();
    }
         return $results;
    }

VIEW

<?php
  foreach ($article->result() as $a) {?>
  <button type='button' class='close' data-dismiss='modal'>&times;</button>
  <h1 class='modal-title'><?php echo $a->title; ?></h1>
</div>
<div class='modal-body'>
  <h4><?php echo $a->category_name; ?></h4>
  <h4><?php echo $a->public_date; ?></h4>
  <h4><?php echo $a->description; ?></h4>
  <?php } ?>

I ended up keeping the VIEW coding as it was working for me without the model/controller code. I've actually spent an extra hour on research, trying desperately to ascertain there weren't any answers in the wild on Google or SO. I even gave it one more Doug Flutie Hail Mary;

VIEW

<?php
  $i=0;
  foreach ($article->result() as $a) if($i==1) break; {?>
  <button type='button' class='close' data-dismiss='modal'>&times;</button>
  <h1 class='modal-title'><?php echo $a->title; ?></h1>
</div>
<div class='modal-body'>
  <h4><?php echo $a->category_name; ?></h4>
  <h4><?php echo $a->public_date; ?></h4>
  <h4><?php echo $a->description; ?></h4>
  <?php $i++; ?>
  <?php } ?>

My head is literally pounding @ this point...thanks for any clues around this. It could also be something I'm completely missing as well as there's no holiday from Human Error (lol!).

UPDATE

Currently I'm stuck trying to retool using solutions suggested by @manu joseph & @webcrazymaniac - @webcrazymaniac: not sure if I understand your question about how the article list is built. It may be that I haven't posted enough code for you - please inform me as to what you need to see. As far as the inclusion of the echo for the #modalItem of the View link - not doing anything different for me. @manu joseph, I cannot change $article->result() to article->row() as the former is how the article data is displayed to the page (ie);

<?php 
foreach ($article->result() as $a)
{
   echo "<tr>".
   "<td>$a->article_id</td>".
   "<td>$a->title</td>".
   "<td>$a->category_name</td>".
   "<td>$a->access_level</td>".
   "<td>$a->public_date</td>".
 "<td><img class='img-responsive img-circle' style='width:100%; height:50px' src='".base_url()."images/posts/$a->image' title='Image' ></td>".
"<td>".
"<a href='".$a->article_id."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a> &nbsp;".
"<a href='".base_url()."article/view/$a->article_id' class='btn btn-primary btn-sm'>Edit</a> &nbsp;".
"<a href='".base_url()."article/delete/$a->article_id' class='btn btn-danger btn-sm'>Delete</a> ".
  "</td>".
  "</tr> ";
  }
?>

the above being the table that db results are being echoed out to. @manu joseph, I've noticed initially that the $i=0; lines are only ever functional when I don't try to add the article_id in the View button link. That's where the answer lies for me, since, when that happens, then I'm back to the problem that the modal only displays a single listing based on the click of the View button link, but it happens to grab the identical singular result to display within the modal no matter WHAT View button link is clicked upon. Funny how now my previous pagination problems are non-existant (lol!).

SO CLOSE I CAN TASTE IT...

An hour's worth of tweakling has gotten me to the desired (if skewed) result of just displaying a single articles' details;

MODAL WINDOW VIEW LINK

"<a href='".$a->article_id."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a> &nbsp;".

MODAL CONTENT

 <div class='modal-content'>
   <div class='modal-header'>
    <?php           
      foreach ($article->result() as $a) $i=0; if($i==1) break; {     
      ?>
     <button type='button' class='close' data-dismiss='modal'>&times;</button>
     <h1 class='modal-title'><?php echo $a->title; ?></h1>
   </div>
  <div class='modal-body'>
    <h4><?php echo $a->category_name; ?></h4>
    <h4><?php echo $a->public_date; ?></h4>
    <h4><?php echo $a->description; ?></h4>
  <?php $i++; ?>
      <?php } ?>
  </div>
  <div class='modal-footer'>
    <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
  </div>
  </div>

PICTURED RESULT: end desired result

I don't know that you can see (probably with image enlargement) the cursor arrow hovering over the 1st View button on page, but the specific article_id is not being returned. Like I said, so close I can taste it.

Misery Index UPDATE:

Thank you so much @manu joseph for his continuing attempt to set me right - the code;

<?php

  $i = 0;

   foreach ($article->result() as $a)
   { 
      echo $i."<br>";
      if( $i == 1 ) break;
?>
<button type='button' class='close' data-dismiss='modal'>&times;</button>
<h1 class='modal-title'><?php echo $a->title; ?></h1>
<div class='modal-body'>
  <h4><?php echo $a->category_name; ?></h4>
  <h4><?php echo $a->public_date; ?></h4>
  <h4><?php echo $a->description; ?></h4>
</div>
<?php 
$i++; 
} 

die();
?>


Results in the image below; Code rewrite by SO member @manu joseph


The value of $i is echoed at the top above the title and the incremented value $i++ is echoed at bottom of modal and the close button is vanished. I'm playing a bit with your sequencing as it's neater than what I have currently.

I think what needs to be addressed is the View button links as when they're hovered over, the correct article_id is shown, but the modal always only manages to pull in the 1st recordset and never increments to show the content under any of the other article ids;

"<a href='".$a->article_id."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a> &nbsp;".

I know I'm fluffing it right here and am in a fight to connect with the other article ids by missing something crucial. Any other questions you need answered, please feel free to ask. Gotta bed it now...

UPDATE
@webcrazymaniac listed a solution that took me several readings to actually catch his intent to help push me into a better direction and holding position @ the moment;

foreach ($article->result() as $a) {
echo "<a href='".base_url('#myModal/$a->art_id')."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a> &nbsp;".
<?php } ?>  

the above being the link that drops the modal into view. I'm now able to pull the 1st db result into all the modals of the page choices;

    <!-- alert view -->
<div class='modal fade' id='myModal' role='dialog'>
<div class='modal-dialog'>
  <!-- Modal content-->
  <?php 
  $i = 0;
  if (!$i = 0){
  foreach (array_slice($article->result(), 0, 1) as $a) {          
  ?>
  <div class='modal-content' id='myModal<?php echo $a->art_id;?>'>    
    <div class='modal-header'>
      <button type='button' class='close' data-dismiss='modal'>&times;</button>
      <h4 class='modal-title'><?php echo $a->title;?></h4>
    </div>
    <div class='modal-body'>
      <h2><?php echo $a->description;?></h2>
    </div>      
    <div class='modal-footer'>
      <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
    </div>      
  </div>
  <?php
   break;
   }
   }
  ?>
</div>

Current code rewrite results

Using array_slice() was a way for me to limit how many results were pulled in from the links as I couldn't specify a limit without breaking the previous (private) definition for limit related to pagination (and I definitely didn't want to disturb that) within the model. My current modal function;

public function modal($art_id=NULL)
    {
      $this->db->select('*');
      $this->db->from('tbl_article');
      $this->db->where('tbl_article.art_id =', $art_id);
      $result = $this->db->get();
      return $result->result_array();
    }  

With only a hairsbreadth distance from my resolution, I'm a little to brain-squirrely to see the forest from the trees at this point and am staunchly convinced it's the most trivial of resolutions, but just cannot see it.

HomeOffice
  • 139
  • 3
  • 16

3 Answers3

1

To summarize, your first issue

The exact issue I'm having is imposing a limit on the returned results of the foreach() loop. If I use something like $i=0; if($i==1) break; before the foreach(), I get the same content article_id result.

I believe u need to limit the number of rows displayed in view? if yes, you need to do the checking inside foreach loop (U have put this before the braces starts :) ) as below:-

    <?php
      $i=0;
      foreach ($article->result() as $a) { 

      if($i==1) break;
      ?>
      <button type='button' class='close' data-dismiss='modal'>&times;</button>

Moreover if you want to get only the first row, go for

foreach ($article->row() as $a) {

Edited Lines Below >>>>>>>>>

Hi, @HomeOffice I have modified the view code as below :-

<?php

$i = 0;

foreach ($article->result() as $a) { 
    echo $i."<br>";
    if( $i == 1 ) break;
    ?>
    <button type='button' class='close' data-dismiss='modal'>&times;</button>
    <h1 class='modal-title'><?php echo $a->title; ?></h1>
    <div class='modal-body'>
      <h4><?php echo $a->category_name; ?></h4>
      <h4><?php echo $a->public_date; ?></h4>
      <h4><?php echo $a->description; ?></h4>
    </div>
    <?php 
    $i++; 
} 

die();
?>
  1. Print the value of "i".
  2. Removed some unnecessary div.
  3. Put a die() at the end.

Let me know what is your output? ..

Also let me have you tried this with $article->row() ?

  • Unfortunately @manu joseph, I couldn't tag onto any sort of result limitation until I put $i=0; if($i==1) break; right before the opening brace of the `foreach()` statement. I'm sure it's biting my backside somehow...have tried all sort of manipulation of those aforementioned lines. – HomeOffice Jul 06 '16 at 14:28
  • Hello @manu joseph and thank you for your efforts - I **did** use your suggestion of `article->result() as row`, but it was just as I expected since the elements weren't accessed at all in that state. I'll be trying out your suggestion right now... – HomeOffice Jul 07 '16 at 13:55
  • Ignore my comment on the disappeared `close` button - **MY MISTAKE** - once again, thank you kindly for providing some grey matter on this... – HomeOffice Jul 07 '16 at 14:25
1

You should use an id for each element in your table. Then call that id in your button:

<a href='#modalItem<?php echo $a->id;?>' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a>

That should do it, although my answer could be more complete, but I get the feeling you haven't posted all you should have posted. More precise, I can't get it how you build the list of articles behind the modal window.

UPDATE: If I understand this right, this is the button that toggles the modal element:

<a href='".$a->article_id."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a>

If so, you must add the 'id'(#) simbol in the href:

<a href='#articleModal".$a->article_id."' class='btn btn-success btn-sm' data-toggle='modal' data-target='#myModal'>View</a>

(not sure what triggers your modal window, if it's data-target='#myModal', it's here where you must make the modification: #myModal".$a->article_id.") This tells the browser you have an unique id for each modal window, that corresponds to each article. And the result would be a link to #articleModal[x], on the same page.

Only at this point, you build only one modal, (with several articles inside when you started, and one article now). That's because you use foreach iteration inside the modal element, instead of using it to build a different modal window, with a different id, for each article. What you need to do is something like this:

<?php           
      foreach ($article->result() as $a){     
      ?>
  <div class='modal-content' id='articleModal<?php echo $a->article_id;?>'>
   <div class='modal-header'>

     <button type='button' class='close' data-dismiss='modal'>&times;</button>
     <h1 class='modal-title'><?php echo $a->title; ?></h1>
   </div>
  <div class='modal-body'>
    <h4><?php echo $a->category_name; ?></h4>
    <h4><?php echo $a->public_date; ?></h4>
    <h4><?php echo $a->description; ?></h4>


  </div>
  <div class='modal-footer'>
    <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
  </div>
  </div>
<?php } ?>

, or something like this.

The idea is to have a unique modal window, with it's unique id for each article, uniquely called with the corresponding button.

Vali S
  • 1,471
  • 2
  • 10
  • 18
  • check the `MODAL WINDOW VIEW LINK` code in my update above and tell me where I can get the `php echo` in there as I had numerous problems attempting to insert this into the named line above. Getting the article_id in there was the 1st thing I thought about. – HomeOffice Jul 06 '16 at 14:32
  • OK @webcrazymaniac - **YOUR** answer seems to be leading me in the right direction. Had a chance to go back and read what you posted, and, while it doesn't give me the result I'm after, helps push it a little more down the garden path. So sorry I just got what you were telling me...thanks. – HomeOffice Jul 24 '16 at 05:22
-1

In all actuality - according to the phrasing of my question - I've answered it already;

  <?php 
  foreach (array_slice($article->result(), 0, 1) as $a) {      
  ?>  

using array_slice I was able to achieve the limitation of results being pulled into the modal. The problem of not being able to return the matching article content by id is outside the scope of the original question and requires its' own separate focus. Thanks to @manu joseph and @webcrazymaniac, respectively, for their efforts on this issue.

HomeOffice
  • 139
  • 3
  • 16
  • 1
    It's such a complicated question, and yet you managed to find the simplest answer on your own! Well done! – Vali S Jul 26 '16 at 07:52
  • Thanks @webcrazymaniac - but I'm getting grilled by a down-voting hater - unfortunate when they offer no solution - but all I care about is getting 'er done (lol!). Thanks for the pointers - **appreciate that**... – HomeOffice Jul 26 '16 at 11:15