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'>×</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.
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'>×</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'>×</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> ".
"<a href='".base_url()."article/view/$a->article_id' class='btn btn-primary btn-sm'>Edit</a> ".
"<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> ".
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'>×</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>
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'>×</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();
?>
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> ".
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> ".
<?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'>×</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>
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.