如何将正确的数据库发送给每个数据库

时间:2019-02-27 23:13:29

标签: php mysql

运行代码时,它会向我显示每部电影以及每部电影旁边的按钮(详细信息),当我单击第一部电影的详细信息按钮时,它将显示正确的详细信息,但是当我单击第二部电影时一个,它也向我显示了第一个。您能看看我应该怎么做才能使每个按钮显示正确的详细信息吗?谢谢。

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div class="content">
        <div class="wrap">
            <div class="content-top">
                <div class="listview_1_of_3 images_1_of_3">
                    <h3>Coming Soon</h3><?php
                                        $qry3=mysqli_query($con,"select * from tbl_news order by rand() limit 2");

                                        while($n=mysqli_fetch_array($qry3))
                                        {
                                        ?>
                    <div class="content-left">
                        <div class="listimg listimg_1_of_2"><img src="admin/%3C?php%20echo%20$n['attachment'];?%3E"></div>
                        <div class="text list_1_of_2">
                            <div class="extra-wrap">
                                <button class="btn btn-success" data-target="#details-1" data-toggle="modal" type="button">Details</button>
                                <div aria-hidden="true" aria-labelledby="details-1" class="modal fade details-1" id="details-1" role="dialog">
                                    <div class="modal-dialog modal-lg">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button aria-label="Close" class="close" data-dismiss="modal" type="button"><span aria-hidden="true">&times;</span></button>
                                                <h3 class="modal-tital text-center"><span class="data" style="text-color:#000"><strong><?php echo $n['name'];?></strong></span></h3>
                                            </div>
                                            <div class="modal-body">
                                                <span class="data" style="text-color:#000"></span>
                                                <div class="containers-fluid">
                                                    <span class="data" style="text-color:#000"></span>
                                                    <div class="row">
                                                        <span class="data" style="text-color:#000"></span>
                                                        <div class="col-sm-6">
                                                            <span class="data" style="text-color:#000"></span>
                                                            <div class="center-block">
                                                                <span class="data" style="text-color:#000"><img src="admin/%3C?php%20echo%20$n['attachment'];?%3E"></span>
                                                            </div><span class="data" style="text-color:#000"></span>
                                                        </div>
                                                        <div class="col-sm-6">
                                                            <span class="data" style="text-color:#000"></span>
                                                            <h4><span class="data" style="text-color:#000">Summry</span></h4><span class="data" style="text-color:#000"><span class="text-top"><?php echo $n['description'];?></span></span>
                                                            <hr>
                                                            <span class="data" style="text-color:#000"><strong>Type :<?php echo $n['type'];?></strong></span><br>
                                                            <br>
                                                            <div class="data">
                                                                Date to be published: <?php echo $n['news_date'];?>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div><?php
                    }

                    ?>
                    <div class="clear"></div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

您的循环生成的按钮完全相同 带有data-target =“#details-1”和相应的DIV id =“ details-1”

您需要“按记录”生成它们。

假设您的数据库记录包含名为“ id”的列,您只需将“ details-1”替换为生成的id。

data-target="#details-1"


data-target="#details-<?=$n['id']?>"

答案 1 :(得分:1)

两个按钮都具有相同的data-target属性-#details-1。您需要为循环中的按钮及其各自的div容器分配不同的ID:

$i = 0;
while($n=mysqli_fetch_array($qry3))
{
    $i++;
?>

...

<button class="btn btn-success" data-target="#details-<?php echo $i; ?>" data-toggle="modal" type="button">Details</button>
    <div aria-hidden="true" aria-labelledby="details-<?php echo $i; ?>" class="modal fade details-<?php echo $i; ?>" id="details-<?php echo $i; ?>" role="dialog">

这样,您的第一部电影将获得ID details-1(并且您的按钮将指向具有相同的data-target属性),第二部电影将获得details-2,依此类推。