我的回声正在破坏我的div容器/行

时间:2019-04-25 14:44:49

标签: php post bootstrap-4 echo blogs

所以我想显示带有变量的帖子并回显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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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;
}