简体   繁体   English

使用PHP记录集数据的Bootstrap 3 Modal

[英]Bootstrap 3 Modal using PHP recordset data

I have developed a content management system with PHP. 我已经用PHP开发了一个内容管理系统。 I would like to use the bootstrap modal to list the data from a recordset. 我想使用引导程序模式来列出记录集中的数据。 A short blurb is displayed with a button, that when clicked, should open the modal and display the details based on the ID of the record. 显示带有按钮的简短内容,单击该按钮应打开模式并根据记录的ID显示详细信息。

My issue is that three records are being displayed. 我的问题是正在显示三个记录。 When I click on each of the three buttons, only the first record is being displayed in the modal window. 当我单击三个按钮中的每个按钮时,模态窗口中仅显示第一条记录。 Reading and implementing many of the solutions has unfortunately not help me to achieve the objectives. 不幸的是,阅读和实施许多解决方案并没有帮助我实现目标。

My Button: 我的按钮:

        <ul class="bullet">
          <li class="bullet"><?php echo $row_rsEvents['eventBlurb']; ?>
            <button type="button" class="btn btn-custom pull-right" data-toggle="modal" data-id="<?php echo $row_rsEvents['eventID']; ?>" data-target="#myModal1" >Read More ...</button>

My Modal: 我的模态:

<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h1 class="modal-title" id="myModalLabel1"><?php echo $row_rsEvents['eventTitle']; ?></h1>
</div>
<div class="modal-body">
<div class="row">
<div id="eventHolder" class="col-sm-12">
<div class="col-sm-7"><?php echo $row_rsEvents['eventContent']; ?></div>
<div class="col-sm-5 pull-right">
<div><img src="img/events/<?php echo $row_rsEvents['eventPhoto']; ?>" alt="ALT"></div>
<div><?php echo $row_rsEvents['eventPhotoCaption']; ?></div>
</div></div></div></div>
<div class="modal-footer"><div class="pull-left"><?php echo(($row_rsEvents['eventDate'])?date('d M Y',strtotime($row_rsEvents['eventDate'])):'') ; ?></div>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div></div></div></div> 

I have read many of the posts trying to find a solution and implement. 我已经阅读了许多试图找到解决方案和实施的帖子。 Unfortunately, I'm lacking the JavaScript and jQuery knowledge to get my modal working. 不幸的是,我缺乏JavaScript和jQuery知识来使我的模式工作。

I greatly appreciate any help and solutions 我非常感谢任何帮助和解决方案

See this php i whipped up. 看到这个PHP我鞭打。 This would make your modals unique and the appropriate information would be inputted into the modal based on which modal id is open. 这将使您的模态唯一,并且将基于打开的模态ID将适当的信息输入到模态中。 you could do the same to echo the buttons to open the modal using the dynamic id. 您可以执行相同的操作以使用动态ID呼应按钮以打开模态。

EDIT: Added a connect to sql script. 编辑:添加了一个连接到SQL脚本。 EDIT: Set query to your select query EDIT: Added mysql_num_rows, set it to $totalRows_rsEvents 编辑:将查询设置为您选择的查询编辑:添加了mysql_num_rows,将其设置为$totalRows_rsEvents

$db_host = "host url"; 
$db_username = "username"; 
$db_pass = "password"; 
$db_name = "database name"; 
mysql_connect("$db_host","$db_username","$db_pass"); 
mysql_select_db("$db_name");

$modalList = '';
$rsEvents = mysql_query("SELECT * FROM ri_Events WHERE eventPromote AND eventStatus = '1' ORDER BY eventDate ASC");
$totalRows_rsEvents = mysql_num_rows($rsEvents);
while($row_rsEvents = mysql_fetch_array($rsEvents)){
$modalList .= '<div class="modal fade" id="myModal'.$id = $row_rsEvents["id"].'" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h1 class="modal-title" id="myModalLabel1">'.$row_rsEvents['eventTitle'].'</h1>
</div>
<div class="modal-body">
<div class="row">
<div id="eventHolder" class="col-sm-12">
<div class="col-sm-7">'.$row_rsEvents['eventContent'].'</div>
<div class="col-sm-5 pull-right">
<div><img src="img/events/'.$row_rsEvents['eventPhoto'].'" alt="ALT"></div>
<div>'.$row_rsEvents['eventPhotoCaption'].'</div>
</div></div></div></div>
<div class="modal-footer"><div class="pull-left">'.(($row_rsEvents['eventDate'])?date('d M Y',strtotime($row_rsEvents['eventDate'])):'').'</div>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div></div></div></div>';
}

print $modalList; // since a modal isnt visible in bootstrap until opened this should be acceptable.  

if not, you can add <?php print $modalList ?> anywhere else in the page. 如果没有,则可以在页面中的其他任何地方添加<?php print $modalList ?>

My PHP Code for my button and modal: 我的按钮和模式的PHP代码:

<div class="news-events" style="clear:both; ">
<h1 class="light-grey tm">WHATS ON?</h1>
<?php do { ?>
<div id="event-holder">
<h4><?php echo(($row_rsEvents['eventDate'])?date('d M Y',strtotime($row_rsEvents['eventDate'])):'') ; ?></h4>
<ul class="bullet">
<li class="bullet"><?php echo $row_rsEvents['eventBlurb']; ?>
<button type="button" class="btn btn-custom pull-right" data-toggle="modal" data-id="<?php echo $row_rsEvents['eventID']; ?>" data-target="#myModal1" >Read More ...</button>
</li>
<div class="clearfix"></div> 
<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h1 class="modal-title" id="myModalLabel1"><?php echo $row_rsEvents['eventTitle']; ?></h1>
</div>
<div class="modal-body">
<div class="row">
<div id="eventHolder" class="col-sm-12">
<div class="col-sm-7"><?php echo $row_rsEvents['eventContent']; ?></div>
<div class="col-sm-5 pull-right">
<div><img src="img/events/<?php echo $row_rsEvents['eventPhoto']; ?>" alt="ALT"></div>
<div><?php echo $row_rsEvents['eventPhotoCaption']; ?></div>
</div></div></div></div>
<div class="modal-footer"><div class="pull-left"><?php echo(($row_rsEvents['eventDate'])?date('d M Y',strtotime($row_rsEvents['eventDate'])):'') ; ?></div>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div></div></div></div> 
</ul>
</div><!-- event-holder -->
<?php } while ($row_rsEvents = mysql_fetch_assoc($rsEvents)); ?>
</div><!-- end-news-events -->

Recordset code in previous answer. 上一个答案中的记录集代码。 Do you need anything esle? 您需要什么吗?

MY IMPLEMENTATION AND RESULTS 我的执行和结果

When I click on button to open model nothing happens. 当我单击按钮打开模型时,没有任何反应。

I did some debugging on your edited code, results below 我对您编辑的代码进行了一些调试,结果如下

101 $totalRows_rsEvents = mysql_num_rows($rsEvents);
102 var_dump($modalList);
103 while($row_rsEvents = mysql_fetch_array($rsEvents)){

string(0) "" 字符串(0)“”


101 $totalRows_rsEvents = mysql_num_rows($rsEvents);
102 var_dump($rsEvents);
103 while($row_rsEvents = mysql_fetch_array($rsEvents)){

resource(13) of type (mysql result) 类型的resource(13)(mysql结果)


101 $totalRows_rsEvents = mysql_num_rows($rsEvents);
102 var_dump($totalRows_rsEvents);
103 while($row_rsEvents = mysql_fetch_array($rsEvents)){

int(3) INT(3)


I tested my original recordset I used for my modal button. 我测试了用于模式按钮的原始记录集。

40 $totalRows_rsEvents = mysql_num_rows($rsEvents);
41 var_dump($row_rsEvents);

array(9) { ["eventID"]=> string(1) "1" ["eventTitle"]=> ... array(9){[“” eventID“] =>字符串(1)” 1“ [” eventTitle“] => ...

Working well. 运作良好。

So next I wanted to debug the new connection code supplied 所以接下来我想调试提供的新连接代码

83 mysql_connect("$db_host","$db_username","$db_pass"); 
84 mysql_select_db("$db_name");
85 var_dump($db_host, $db_username, $db_pass, $db_name);

string(9) "localhost" string(4) "root" string(0) "" string(8) "roll_db5" 字符串(9)“本地主机”字符串(4)“根”字符串(0)“”字符串(8)“ roll_db5”

Next test 下一次测试

88 $totalRows_rsEvents = mysql_num_rows($rsEvents);
89 var_dump($totalRows_rsEvents);

int(3) INT(3)

Next test 下一次测试

88 $totalRows_rsEvents = mysql_num_rows($rsEvents);
89 var_dump($rsEvents);

resource(12) of type (mysql result) 类型的resource(12)(mysql结果)

Lastly I notice a difference: 最后,我注意到了一个区别:

My button data-target="#myModal1" 我的按钮data-target =“#myModal1”

Your $modalList id="myModal'.$id = 您的$ modalList id =“ myModal'。$ id =

Should your id not be id="myModal1'. 您的ID应该不是id =“ myModal1'。

I changed but made no difference. 我改变了但没有改变。 Modal still not opening. 模态仍然没有打开。

Any further suggestions please. 任何进一步的建议,请。 Rather desperate to get this working. 宁愿拼命工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM