PHP& Mysql:根据id显示某些数据

时间:2017-10-13 21:01:05

标签: php mysql

基本上我有这个代码,我想从数据库中提取一些数据并在弹出窗口中显示它。问题是,当我点击眼睛图标(标记为更多)时,我希望它显示该表行的对应ID,但我似乎无法弄清楚如何做到这一点。 有关更多参考资料,我设置了一个临时网站,以便更好地查看问题:http://twgtest-org.stackstaging.com/bau50/bau50_extract.php

您可以看到,当我点击更多时,显示的ID实际上是数据库中的所有ID,而不是它只是该行的ID

<?php
//load database connection  
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
    <ul class="text-align">
        <?php

        if (!$query->rowCount() == 0) {

            while ($show_all = $query->fetch()) {
                echo '<tr><th scope="row">';
                echo $show_all['id_Bau50'];
                echo '</th><td>';
                echo $show_all['name'];
                echo '</td><td>';
                echo $show_all['produkt'];
                echo '</td><td>';
                echo $show_all['preis']," €";
                echo '</th><td>';
                echo $show_all['Ergebniss'];
                echo '</td>
                            <td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-title="more" data-toggle="modal" data-target="#more" ><span class="fa fa-eye"></span></button></p></td>
                            <td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
                            <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';

            }
            echo '</tbody></table>';
        }
        ?></ul>
</div>
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
echo'<div class="modal fade" id="more" tabindex="-1" role="dialog" aria-labelledby="more" 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"><span class="fa fa-remove" aria-hidden="true"></span></button>
                            <h4 class="modal-title custom_align" id="Heading">More</h4>
                        </div>
                        <div class="modal-body">
                            <div>';
while ($show= $query2->fetch()) {
    $id= $show ['id_Bau50'];
    echo $id;
}
echo '</div>
            <div class="modal-footer ">
                <button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
            </div>
            </div>
            <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
            </div>


            </div>';
?>

1 个答案:

答案 0 :(得分:0)

这是一个非常简单的解决方案:为每个由其id标识的行添加一个新模态,例如id&#34; 4&#34;你将用id&#34; modal4&#34;打开模态。等等:

<?php
//load database connection  
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
    <ul class="text-align">
        <?php

        if (!$query->rowCount() == 0) {

            while ($show_all = $query->fetch()) {
                echo '<tr><th scope="row">';
                echo $show_all['id_Bau50'];
                echo '</th><td>';
                echo $show_all['name'];
                echo '</td><td>';
                echo $show_all['produkt'];
                echo '</td><td>';
                echo $show_all['preis']," €";
                echo '</th><td>';
                echo $show_all['Ergebniss'];
                echo '</td>
                            <td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-title="more" data-toggle="modal" data-target="#more'.$show_all['id_Bau50'].'" ><span class="fa fa-eye"></span></button></p></td>
                            <td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
                            <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';

            }
            echo '</tbody></table>';
        }
        ?></ul>
</div>
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
while ($show= $query2->fetch()) {
    $id= $show ['id_Bau50'];
    echo'<div class="modal fade" id="more'.$id.'" tabindex="-1" role="dialog" aria-labelledby="more" 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"><span class="fa fa-remove" aria-hidden="true"></span></button>
                            <h4 class="modal-title custom_align" id="Heading">More</h4>
                        </div>
                        <div class="modal-body">
                            <div>';
    echo $id;
    echo '</div>
            <div class="modal-footer ">
                <button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
            </div>
            </div>
            <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
            </div>


            </div>';
}

?>

没有那么多行,或者你不想写一些javascript,这是可以的。

一个更好的解决方案是使用一个包含所有内容的单一模态并显示/隐藏所选内容,为此您需要使用一些js代码并且不要使用标准的bootstrap属性驱动方法。 在这个例子中,我使用了jquery和js代码(se在底部)。

<?php
//load database connection  
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
    <ul class="text-align">
        <?php

        if (!$query->rowCount() == 0) {

            while ($show_all = $query->fetch()) {
                echo '<tr><th scope="row">';
                echo $show_all['id_Bau50'];
                echo '</th><td>';
                echo $show_all['name'];
                echo '</td><td>';
                echo $show_all['produkt'];
                echo '</td><td>';
                echo $show_all['preis']," €";
                echo '</th><td>';
                echo $show_all['Ergebniss'];
                echo '</td>
                            <td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-moreid="'.$show_all['id_Bau50'].'" ><span class="fa fa-eye"></span></button></p></td>
                            <td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
                            <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';

            }
            echo '</tbody></table>';
        }
        ?></ul>
</div>
<div class="modal fade" id="more" tabindex="-1" role="dialog" aria-labelledby="more" 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"><span class="fa fa-remove" aria-hidden="true"></span></button>
                        <h4 class="modal-title custom_align" id="Heading">More</h4>
                    </div>
                    <div class="modal-body">
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
while ($show= $query2->fetch()) {
    $id= $show ['id_Bau50'];
    echo '<div id="more'.$id.'">'.$id.'</div>';
}

?>
                    </div>
            <div class="modal-footer ">
                <button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
            </div>
       </div>
    <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- YOU NEED JQUERY FOR THIS: -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
    $(function(){
        $("button[data-moreid]").click(function(){
            $("#more .modal-body > div").hide(); //hide all more div
            var id=$(this).attr("data-moreid"); //get id from pushed button
            $("#more .modal-body #more"+id).show(); //show only pushed id
            $("#more").modal("show"); //show modal
        });
    });
</script>