我的回声正在摧毁我的div容器/行

问题描述 投票:-1回答:2

所以我想用变量显示我的帖子并回显html表单。问题是,我需要

        <div class='container'>
        <div class='row'>

因为我希望它连续显示。但是因为每个帖子都得到了div类,它不起作用并显示完全错误的帖子......

此外,它在顶部显示我的按钮“Loeschen”和“Bearbeiten”,但它应该在文本字段的正下方。

应该如何看待:

Example of how It should look

它现在看起来如何:

And that's how it actually looks

<?php   
        $ort = 'Beitr&auml;ge';
        include ("head.php");
        include("database.php");
        include("navigation.php");

?>
<!--Beitrage auflisten-->
<?php 

    $sql = "SELECT * FROM beitrag ORDER BY beitrag_id DESC";

    $res = mysqli_query($db, $sql) or die(mysqli_error());

    $beitrag = "";

    if(mysqli_num_rows($res) > 0) {
        while($row = mysqli_fetch_assoc($res)) { 
            $beitrag_id = $row['beitrag_id'];
            $titel = $row['titel'];
            $leistung = $row['leistung'];
            $bezugsort = $row['bezugsort'];
            $kosten = $row['kosten'];
            $p_text = $row['p_text'];



        $beitrag .= "  

        <div class='album py-5 bg-light'>
        <div class='container'>
        <div class='row'>
        <div class='col-md-4'>
          <div class='card mb-4 box-shadow'>
             <div class='card-header'>
                <a href='siehe_beitrag.php?pid=$beitrag_id'><h4 class='my-0 font-weight-normal'>$titel</h4></a
                <h4 class='my-0 font-weight-normal'>$kosten&euro;</h4 >
                </div>
                <div class='card-body'>
                    <p class='card-text'>$p_text</p>
                    <div class='d-flex justify-content-between align-items-center'>
              </div>
            </div>
          </div>
        </div>"; 


    if (isset($_SESSION["login"])){ 
                if($_SESSION["login"] == 1){
                    echo "  
                    <div class='btn-group' >
                            <a href='loeschen_beitrag.php?pid=$beitrag_id'>
                                <button type='button' class='btn btn-sm btn-outline-secondary'>Loeschen</button>
                            <a href='bearbeiten_beitrag.php?pid=$beitrag_id'>
                                <button type='button' class='btn btn-sm btn-outline-secondary'>Bearbeiten</button>
                            </div>";
                    }else{
                    echo "";
                }
            }





        }
        echo $beitrag;
    } else {
        echo "Keine Beiträge vorhanden ";
    }

    ?>  

    <a href='neuer_beitrag.php' target='_blank'>Neuer Beitrag</a>

最终版本:Thanks to aynber and the others, now it works like I imagined it <3

php post bootstrap-4 echo blogs
2个回答
0
投票

您有两个问题:1)您没有关闭所有div标签,2)您正在为每行创建一个宽度为4列的新容器。您需要在循环外部创建一个新容器,并且只有在超过3个结果时才关闭/重新打开:

if(mysqli_num_rows($res) > 0) {
    $i = 0; // Create a counter to see when to start a new row
    $beitrag .= "

            <div class='album py-5 bg-light'>
            <div class='container'>
            <div class='row'>";
    while($row = mysqli_fetch_assoc($res)) {
        $beitrag_id = $row['beitrag_id'];
        $titel = $row['titel'];
        $leistung = $row['leistung'];
        $bezugsort = $row['bezugsort'];
        $kosten = $row['kosten'];
        $p_text = $row['p_text'];


        $beitrag .= "

            <div class='col-md-4'>
                <div class='card mb-4 box-shadow'>
                    <div class='card-header'>
                        <a href='siehe_beitrag.php?pid=$beitrag_id'><h4 class='my-0 font-weight-normal'>$titel</h4></a
                        <h4 class='my-0 font-weight-normal'>$kosten&euro;</h4 >
                    </div>
                    <div class='card-body'>
                        <p class='card-text'>$p_text</p>
                        <div class='d-flex justify-content-between align-items-center'>
                        </div>
                    </div>";

        if(isset($_SESSION["login"]) && $_SESSION["login"] == 1) {
                $beitrag .= "
                        <div class='btn-group' >
                                <a href='loeschen_beitrag.php?pid=$beitrag_id'>
                                    <button type='button' class='btn btn-sm btn-outline-secondary'>Loeschen</button>
                                <a href='bearbeiten_beitrag.php?pid=$beitrag_id'>
                                    <button type='button' class='btn btn-sm btn-outline-secondary'>Bearbeiten</button>
                                </div>";

        }

        $beitrag .= "    </div>
            </div>";

        $i++;

        if($i % 3 == 0) { // 3 results in the div, start a new one
            $beitrag .= "
                </div>
              </div>
            </div>";
            $beitrag .= "

            <div class='album py-5 bg-light'>
            <div class='container'>
            <div class='row'>";
        }


    }

    if(($i - 1) % 3 != 0) { // It would have been closed on the last loop
        $beitrag .= "
                </div>
              </div>
            </div>";
    }

    echo $beitrag;
}

0
投票

你在html中有错误,你没有关闭按钮的链接。

<div class='btn-group' >
                            <a href='loeschen_beitrag.php?pid=$beitrag_id'>
                                <button type='button' class='btn btn-sm btn-outline-secondary'>Loeschen</button></a>
                            <a href='bearbeiten_beitrag.php?pid=$beitrag_id'>
                                <button type='button' class='btn btn-sm btn-outline-secondary'>Bearbeiten</button></a>
                            </div>
© www.soinside.com 2019 - 2024. All rights reserved.