繁体   English   中英

添加PHP条件时CSS不适用于div

[英]CSS not applying to div when adding PHP condition

我想有条件地显示一个div,以便在没有值时不显示div。 没有php if部分,一切工作正常,css样式适用于div。 但是当我添加if条件时,CSS不再适用。

这是div部分:

<?php
$getCar_1 = getCar_1();
$getCar_2 = getCar_2();

if(!empty($getCar_1) || !empty($getCar_2)){

?>

<div class="block">
    <div class="up">
        <div class="title"><h4>Cars</h4></div>
    </div>

    <div class="down">
        <div class="column_1"><b><?php getCar_1(); ?></b></div>
        <div class="column_2"><b><?php getCar_2(); ?></b></div>
    </div>
</div>

<?php
}
?>

这是CSS部分:

.block{
    width:900px;
    margin:0 auto;
    overflow:hidden;
    float:left;
}

.up{
    width:100%;
    text-align:center;
    border:1px solid;
    margin-top:10px;
    margin-bottom:10px;
    background-color:skyblue;
}

.down{
    width:100%;
    clear:both;
}

.column_1{
    float:left;
    width:47%;
    padding:10px;
}

.column_2{
    float:right;
    width:47%;
    text-align:right;
    padding:10px;
}

我知道这段代码非常简单,但是我不明白为什么它无法正常工作。

更加具体。
如果我仅使用此代码:

<div class="block">
    <div class="up">
        <div class="title"><h4>Cars</h4></div>
    </div>

    <div class="down">
        <div class="column_1"><b><?php getCar_1(); ?></b></div>
        <div class="column_2"><b><?php getCar_2(); ?></b></div>
    </div>
</div>

它显示正常: 仅使用div

并且在添加php if部分时显示如下: ading php if

因此,当我添加php时,css不再起作用。 我还尝试了内联样式变体,例如<div class="block" style="......">但它也不能以这种方式工作。

编辑:我回来了,因为我认为问题来自功能getCar_1();。 和getCar_2(); 在functions.php页面中,如下所示:

<?php
function getCar_2(){
global $connect;

$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());

while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
    $car_model_2 = $row_car_2['car_name'];
    $car_image_2 = $row_car_2['car_image'];

    if(isset($car_model_2) && isset($car_image_2)){
        echo "
            <div id='' style='white-space:nowrap;'>
                <p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a> 
                <img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
            </div>";
        }
    }
}
?>

代替:

if(!empty($getCar_1) || !empty($getCar_2)){

?>

尝试这个:

if((!empty($getCar_1) || (!empty($getCar_2)): ?>

而不是:

<?php } ?>

试试这个: <?php endif; ?> <?php endif; ?>

您不会从getCar_2()函数返回任何内容。 你所期望的值将在$getCar_2一行: $getCar_2 = getCar_2(); 如果与getCar_2()相同,但没有return语句,则该问题与getCar_1()函数相同。 问题是您的if block将永远不会执行,因为您的条件是: if((!empty($getCar_1) || (!empty($getCar_2)):

因为$getCar_1$getCar_2变量将始终为空,因为getCar_1()getCar_2()不返回任何值,因此永远不会为真。

因此,只有echo你是从功能提供getCar_2()getCar_1()被打印为输出到HTML和你的if块没有被执行的。

如果希望if块起作用,则应将return语句放入getCar_2()getCar_1()函数中。

代替:

<?php
function getCar_2(){
global $connect;

$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());

while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
    $car_model_2 = $row_car_2['car_name'];
    $car_image_2 = $row_car_2['car_image'];

    if(isset($car_model_2) && isset($car_image_2)){
        echo "
            <div id='' style='white-space:nowrap;'>
                <p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a> 
                <img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
            </div>";
        }
    }
}
?>

用这个:

<?php
function getCar_2(){
global $connect;
$response = NULL;
$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());

while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
    $car_model_2 = $row_car_2['car_name'];
    $car_image_2 = $row_car_2['car_image'];

    if(isset($car_model_2) && isset($car_image_2)){
    $response = 'success';
        echo "
            <div id='' style='white-space:nowrap;'>
                <p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a> 
                <img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
            </div>";
        }
    }
return $response;
}
?>

希望对您有所帮助

对我来说,在打印数据时似乎有问题,应该是:

<?php
$getCar_1 = getCar_1();
$getCar_2 = getCar_2();

if(!empty($getCar_1) || !empty($getCar_2)){

?>

<div class="block">
    <div class="up">
        <div class="title"><h4>Cars</h4></div>
    </div>

    <div class="down">
        <div class="column_1"><b><?php echo $getCar_1; ?></b></div>
        <div class="column_2"><b><?php echo $getCar_2; ?></b></div>
    </div>
</div>

<?php
}
?>

编辑:在另一个论坛的管理员的帮助下解决了这个问题。 正如Banzay所说,如果我发布get getCar_1()会很好。 和getCar_2(); 功能更早。
问题在于功能。 无论如何,如果像我这样的其他初学者会在这里遇到这个问题,那就是解决方案。
我修改了这个:

<?php
function getCar_2(){
global $connect;

$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());

while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
    $car_model_2 = $row_car_2['car_name'];
    $car_image_2 = $row_car_2['car_image'];

    if(isset($car_model_2) && isset($car_image_2)){
        echo "
            <div id='' style='white-space:nowrap;'>
                <p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a> 
                <img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
            </div>";
        }
    }
}
?>

到这个:

<?php
function getCar_2(){
global $connect;

$text = "";

$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());

while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
    $car_model_2 = $row_car_2['car_name'];
    $car_image_2 = $row_car_2['car_image'];

    if(isset($car_model_2) && isset($car_image_2)){
        $text =  "
            <div id='' style='white-space:nowrap;'>
                <p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a> 
                <img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
            </div>";
        }
    }
    return $text;
}
?>

我用$ text =“ ...”替换了echo“ ...”,并在闭括号后返回了变量,如下所示:return $ text;
为了工作,必须在查询之前启动$ text变量,如下所示:$ text =“”;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM