在codeigniter中用foreach()传递给bootstrap模态的数据增加限制 [英] add limit to data passed to bootstrap modal with foreach() in codeigniter

查看:118
本文介绍了在codeigniter中用foreach()传递给bootstrap模态的数据增加限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从CodeIgniter的数据库表中提取数据并在引导模式窗口中显示时遇到任何困难。使用此按钮链接;

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

我可以访问模态窗口没有问题,并显示以下编码的结果; / p>

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

<! - 模态内容 - >
< 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'>关闭< / button>
< / div>
< / div>

< / div>



foreach()循环的返回结果施加了限制。如果我使用 $ i = 0; if($ i == 1)break; 之前的 foreach(),我得到相同的内容article_id结果。为了增加我的困惑和困难,当我试图使 article_id href 视图链接,所有数据库表条目将被拖入模态窗口。





我试过工作通过查询构建器一章,在CI文档中处理单个结果数组,这是最适用于我的情况。这是以下方法的基础:



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

我最终保留了 VIEW 模型/控制器代码。我实际上花了一个多小时的研究,竭力确定在谷歌或SO没有任何答案在野外。我甚至还给了一个 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}?>

我的头在这一点上真的很重要...感谢任何线索。它也可能是我完全错过,以及没有假日从人为错误(lol!)。



UPDATE



目前我无法使用建议的解决方案@manu joseph& @webcrazymaniac - @webcrazymaniac:不知道我是否理解你的问题关于文章列表是如何构建的。这可能是因为我没有为您发布足够的代码 - 请通知我您需要看到什么。对于 View 链接的#modalItem包含 echo - 对我没有什么不同。 @manu joseph,我不能将 $ article-> result()更改为 article-> row()前者是文章数据如何显示到页面(即);

 <?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'查看< / a>& nbsp;。
< 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>;
}
?>

上面是db结果被回显到的表。 @manu joseph,我最初注意到$ i = 0;当我不尝试在 View 按钮链接中添加 article_id 这是的答案在哪里,因为,当发生,那么我回到问题,模态只显示一个单一的列表,基于点击视图按钮链接,但它碰巧抓住相同的奇异结果显示在模态内,无论是什么查看按钮链接被点击。有趣的是,现在我之前的分页问题是不存在的(lol!)。



所以关闭我可以吃东西... >

一小时的tweakling让我得到只显示一篇文章详细信息的所需结果(如果有偏差);



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'>关闭< / button>
< / div>
< / div>

PICTURED RESULT:



我不知道你可以看到(可能有图像放大)光标箭头悬停在第一 View 按钮,但不返回特定的article_id。像我说的,这么接近我可以品尝它。



苦难指数更新:



谢谢这么多的@manu joseph为他继续努力让我正确 - 代码;

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



结果如下图所示:





$ i 在标题顶部回显,增量值 $ i ++ 在模态底部回显, close 按钮已消失。我对你的排序有点担心,因为它比我现在更整洁。



我认为需要解决的是 View 按钮链接,当它们被悬停在其上时,显示正确的 article_id ,但模式总是只能拉入第一个记录集,以显示任何其他文章ID下的内容;

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

我知道我正在这里蓬松它,并且正在努力与其他文章ids通过缺少关键的东西。任何其他问题,你需要回答,请随时问。现在就买吧...



更新

@webcrazymaniac列出了一个解决方案,他的目的是帮助我推进一个更好的方向,并持有立场@

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

上面是将模式拖放到视图中的链接。我现在可以将第一个db的结果拖到页面选择的所有模态;

 <! -  alert view  - > 
< div class ='modal fade'id ='myModal'role ='dialog'>
< div class ='modal-dialog'>
<! - 模态内容 - >
<?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'>关闭< / button>
< / div>
< / div>
<?php
break;
}
}
?>
< / div>





使用array_slice()是我限制从链接中提取的结果数量的一种方法,因为我不能指定限制打破之前(私人)定义的限制相关的分页(和我绝对不想打扰)在模型内。我目前的模态功能;

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

从我的解决方案只有一个hairsbreadth距离,我有点大脑 - 在这一点上轻松地从树上看到森林,并坚定地相信它是最微不足道的决议,但是看不到它。

解决方案

实际上,根据我的问题的短语 - 我已经回答了;

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

使用 array_slice 限制结果被拉入模态。无法通过id返回匹配的文章内容的问题超出了原始问题的范围,需要其自己单独的重点。感谢 @manu joseph @webcrazymaniac ,分别就这个问题的努力。


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.

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:

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;


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>

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.

解决方案

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.

这篇关于在codeigniter中用foreach()传递给bootstrap模态的数据增加限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆