【问题标题】:php select fetch while loop into bootstrap modalphp select fetch while 循环进入引导模式
【发布时间】:2016-04-15 04:03:24
【问题描述】:

有一些我正在尝试进入某些模式的数据库信息。

问题似乎是模式只从最后一个 while 循环中获取变量。页面上的所有 php 是否首先运行?即使它没有被调用?

所以我知道使用 get_results() 和 fetch_array 和 fetch_row 可能有更简单的方法,但在 php 5.5 中这些方法似乎对我不起作用。

另外,我在某处读到使用 AJAX。好吧,我以前从未使用过 ajax,这是我应该研究的吗?

<div class="col-md-4">
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    require ($_SERVER['DOCUMENT_ROOT'].'/db-connect.php');
    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    //echo $conn->host_info . "\n";

    if ($stmt = $conn->prepare("SELECT time, title, tool, descript, thumbpath, smallpath, mediumpath, largepath FROM websites ORDER BY id DESC LIMIT 1")){

        //$stmt->bind_param('s',$id);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($time, $title, $tool, $descript, $thumbpath, $smallpath, $mediumpath, $largepath);

        while ($stmt->fetch()) {


        }

        $stmt->free_result();
        $stmt->close();
    }

    $conn->close();
    ?>

    <img class="img-responsive" title="<?php echo $tool; ?>" data-toggle="modal" data-target="#modalPort" sizes="100vw" src="<?php echo $thumbpath; ?>" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
    <span class="time line-height-small"><?php echo $time; ?></span>
</div>

变量在这里工作正常。问题是我使用相同的 bind_result 变量多次运行相同的 php 脚本。我真的不想更改每个模态的变量。

模态:

<!-- Website Modals-->
<div class="modal fade" id="modalPort" tabindex="-1" role="dialog" aria-labelledby="portfolioModallabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <h4 class="modal-title" id="portfolioModallabel"><?php echo $title; ?></h4>
        </div>
        <div class="modal-body text-center">
            <img class="img-responsive center-block" src="<?php echo $thumbpath; ?>" sizes="100vw" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
            <p class="line-height-small"><?php echo $descript; ?></p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
</div>

【问题讨论】:

    标签: php twitter-bootstrap modal-dialog


    【解决方案1】:

    一种方法是将模态放入while ($stmt-&gt;fetch()) { } 中,同时分配模态触发按钮和模态本身unique id

    注意模态触发按钮中的data-target="#modalPort&lt;?php echo $id;?&gt;" 和模态HTML 中的id="modalPort&lt;?php echo $id;?&gt;"

    <?php while ($stmt->fetch()) { ?>
        <button data-toggle="modal" data-target="#modalPort<?php echo $id;?>" class="btn btn-default">Modal</button>
    
        <div class="modal fade" id="modalPort<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="portfolioModallabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="portfolioModallabel"><?php echo $title; ?></h4>
                </div>
                <div class="modal-body text-center">
                    <img class="img-responsive center-block" src="<?php echo $thumbpath; ?>" sizes="100vw" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
                    <p class="line-height-small"><?php echo $descript; ?></p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
      </div>
    <?php } ?>
    

    Alternate 如果要使用 ajax,请将模态 HTML 放在 while loop 之外,将 &lt;?php echo $id;?&gt; 作为数据属性添加到模态触发按钮 data-id="&lt;?php echo $id;?&gt;"

    <?php while ($stmt->fetch()) { ?>
        <button data-toggle="modal" data-target="#modalPort" data-id="<?php echo $id;?>" class="btn btn-default">Modal</button>
    <?php } ?>
    

    模态 event 侦听器获取 data attribute 值,然后在 Ajax 方法中使用以获取相应的行数据以在模态中显示

    $(document).ready(function(){
        $('#modalPort').on('show.bs.modal', function (e) {
            var dataid = $(e.relatedTarget).data('id');
            var dataString = 'dataid=' + dataid;
            $.ajax({
                type: "POST",
                url: "get_data.php",
                data: dataString,
                cache: false,
                success: function(data){
                    $("#content").html(data);
                }
            });
         });
    });
    

    get_data.php 将是

    <?php
        //Include database connection
        if($_POST['dataid']) {
        //run query against `dataid`
    ?>
        <div class="modal-header">
            <h4 class="modal-title" id="portfolioModallabel"><?php echo $title; ?></h4>
        </div>
        <div class="modal-body text-center">
            <img class="img-responsive center-block" src="<?php echo $thumbpath; ?>" sizes="100vw" srcset="<?php echo $smallpath; ?> 500w, <?php echo $mediumpath; ?> 1000w, <?php echo $largepath; ?> 1500w" alt="Portfolio Site">
            <p class="line-height-small"><?php echo $descript; ?></p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    <?php } ?>
    

    模态 HTML 将是(在循环之外)

    <div class="modal fade" id="modalPort" tabindex="-1" role="dialog" aria-labelledby="portfolioModallabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                 <div id="content"></div> //display data output via ajax
            </div>
        </div>
    </div>
    

    【讨论】:

    • 非常感谢 Shehary 的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 2011-05-31
    • 1970-01-01
    • 2017-04-04
    相关资源
    最近更新 更多