【问题标题】:The dynamic tabs are presenting the same records动态选项卡显示相同的记录
【发布时间】:2021-03-02 18:31:36
【问题描述】:

我正在尝试使用我正在创建的电影网站的 Bootstrap 框架使用 PHP 和 MySQL 创建一个动态选项卡。这些电影将分为正在放映和即将上映。

预期结果:电影应根据类别显示(正在放映或即将上映)。

实际结果:动态标签不起作用。 “即将上映”下的电影显示在“正在放映”中。此外,该网站仅显示第二条记录(来自数据库)。不显示“Now Showing”的第一条记录,即“The”和“Coming Soon”。

无论单击“正在显示”或“即将推出”选项卡,都会显示相同的内容(“即将推出”选项卡只有在我悬停时才能看到,这是我需要修复的另一个问题)。电影“被诅咒的教训”应该在即将上映。未显示“Now Showing”和“Coming Soon”的第一条记录。

非常感谢您在解决这些问题方面的帮助和建议。这几天我一直在尝试解决这些问题,但我似乎无法弄清楚。

这是我的代码:

<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'movie';

$conn = new mysqli($servername, $username, $password, $dbname);
$tab_query = "SELECT * FROM category ORDER BY Category_ID";
$tab_result = mysqli_query($conn, $tab_query);
$tab_menu = '';
$tab_content = '';
$count = 0;

while ($row = mysqli_fetch_array($tab_result)) {
    if ($count == 0) {
        $tab_menu .= '<li class="nav-item"><a class="nav-link active" href="#' . $row["Category_ID"] . '" data-toggle="tab">' . $row["Category_Name"] . '</a></li>';
        $tab_content .= '<div id="'.$row["Category_ID"].'" class="tab-pane fade in active"';
    } else {
        $tab_menu .= '<li class="nav-item"><a class="nav-link" href="#' . $row["Category_ID"] . '" data-toggle="tab">' . $row["Category_Name"] . '</a></li>';
        $tab_content .= '<div id="'.$row["Category_ID"].'" class="tab-pane fade"';
    }
    
    $product_query = "SELECT * FROM movie WHERE Category_ID = '".$row["Category_ID"]."'";
    $product_result = mysqli_query($conn, $product_query);
    
   
    while($sub_row = mysqli_fetch_array($product_result))
    {   
        $tab_content .= '
        <div class="col-md-3" style="margin-bottom:36px;">
            <img src="'.$sub_row["Image_URL"].'" class="img-responsive img-thumbnail" />
            <h4>'.$sub_row["Title"].'</h4>
        </div>
        ';
    }
    $tab_content .= '<div style="clear:both"></div></div>';
    $count++;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Movie Testing Website</title>

        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="HandheldFriendly" content="true">

        <!--Bootstrap CSS-->
        <link rel="stylesheet"
              href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"    integrity=   
              "sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"crossorigin="anonymous">

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

        <!--Custom CSS-->
        <link rel="stylesheet" href="css/main.css">

        <!--jQuery-->
        <script defer 
                src="https://code.jquery.com/jquery-3.4.1.min.js"  
                integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="  
        crossorigin="anonymous"></script>

        <!--Bootstrap JS--> 
        <script defer   
                src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"    
                integrity="sha384-6khuMg9gaYr5AxOqhkVIODVIvm9ynTT5J4V1cfthmT+emCG6yVmEZsRHdxlotUnm"  
        crossorigin="anonymous"></script>
        <script defer src="js/main.js"></script>
    </head>

    <body>
        <div class="container">
            <ul class="nav nav-tabs">
                <?php echo $tab_menu; ?>
            </ul>
            
            <div class="tab-content">
                <?php echo $tab_content; ?>
            </div>
        </div>
    </body>
</html>

这是我的数据库的 sn-ps:

电影台:

分类表:

【问题讨论】:

  • 能否也请您添加生成的HTML?
  • 嗨@PraveenKumarPurushothaman 我已经编辑了我的帖子。
  • 我建议你找:MVC

标签: php mysql bootstrap-4


【解决方案1】:

我在视图中发现了问题,而不是在从 db 收集的数据中。 我根据这个示例修改了html结构:https://bootsnipp.com/snippets/exE6D

了解有关 Bootstrap 选项卡导航的更多信息:https://www.w3schools.com/bootstrap4/bootstrap_navs.asp

<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'movie';

$conn = new mysqli($servername, $username, $password, $dbname);
$tab_query = "SELECT * FROM category ORDER BY Category_ID";
$tab_result = mysqli_query($conn, $tab_query);
$tab_menu = '';
$tab_content = '';
$count = 0;

while ($row = mysqli_fetch_array($tab_result)) {
    if ($count == 0) {
        $tab_menu .= '<a class="nav-item nav-link active" id="' . $row["Category_ID"] . '" data-toggle="tab" href="#nav-' . $row["Category_ID"] . '" role="tab" aria-controls="nav-' . $row["Category_ID"] . '" aria-selected="true">' . $row["Category_Name"] . '</a>';
        $tab_content .= '<div class="tab-pane fade show active" id="nav-' . $row["Category_ID"] . '" role="tabpanel" aria-labelledby="nav-' . $row["Category_ID"] . '-tab">';
    } else {
        $tab_menu .= '<a class="nav-item nav-link" id="' . $row["Category_ID"] . '" data-toggle="tab" href="#nav-' . $row["Category_ID"] . '" role="tab" aria-controls="nav-' . $row["Category_ID"] . '" aria-selected="false">' . $row["Category_Name"] . '</a>';
        $tab_content .= '<div class="tab-pane fade show" id="nav-' . $row["Category_ID"] . '" role="tabpanel" aria-labelledby="nav-' . $row["Category_ID"] . '-tab">';
    }

    $product_query = "SELECT * FROM movie WHERE Category_ID = '".$row["Category_ID"]."'";
    $product_result = mysqli_query($conn, $product_query);


    while($sub_row = mysqli_fetch_array($product_result))
    {   
        $tab_content .= '
        <div class="col-md-3" style="margin-bottom:36px;">
            <img src="'.$sub_row["Image_URL"].'" class="img-responsive img-thumbnail" />
            <h4>'.$sub_row["Title"].'</h4>
        </div>';
    }
    $tab_content .= '<div style="clear:both"></div></div>';
    $count++;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Movie Testing Website</title>

        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="HandheldFriendly" content="true">

        <!--Bootstrap CSS-->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"    integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"crossorigin="anonymous">

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

        <!--Custom CSS-->
        <link rel="stylesheet" href="css/main.css">

        <!--jQuery-->
        <script defer 
            src="https://code.jquery.com/jquery-3.4.1.min.js"  
            integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="  
    crossorigin="anonymous"></script>

        <!--Bootstrap JS--> 
        <script defer src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"    
            integrity="sha384-6khuMg9gaYr5AxOqhkVIODVIvm9ynTT5J4V1cfthmT+emCG6yVmEZsRHdxlotUnm"  
    crossorigin="anonymous"></script>
        <script defer src="js/main.js"></script>
    </head>

    <body>
        <div class="container">
            <div class="row">
                <div class="col-xs-12 ">
                    <nav>
                        <div class="nav nav-tabs nav-fill" id="nav-tab" role="tablist">
                        <?php echo $tab_menu; ?>
                        </div>
                    </nav>
                    <div class="tab-content py-3 px-3 px-sm-0" id="nav-tabContent">
                    <?php echo $tab_content; ?>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    相关资源
    最近更新 更多