AndreiCT AndreiCT - 19 days ago 6
CSS Question

CSS not applying to div when adding PHP condition

I want to conditionally display a div so when there are no values, the div is not displayed.
Without the php if part, all is working fine, the css styles are applying to the divs.
But when i add the if condition, the CSS doesn't apply anymore.

Here is the div part:

<?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
}
?>


and here is the CSS part:

.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;
}


I know this code is very simple but i can't understand why it is not working properly.

To be more specific.

If i use only this code:

<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>


it display normal:
using only div

And when adding the php if part it display this:
ading php if

So when i add the php if, the css has no effect anymore. I also tried the inline styling variant like
<div class="block" style="......">
but it won't work this way either.

EDIT: I'm back because i think the issue comes from the functions get getCar_1(); and getCar_2(); which are in functions.php page and look like this:

<?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>";
}
}
}
?>

Answer

Instead of:

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

?>

Try this:

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

and instead of:

<?php } ?>

Try this: <?php endif; ?>

You are not returning anything from getCar_2() function. What value you expect to be in $getCar_2 by line : $getCar_2 = getCar_2(); same question about getCar_1() function if its same like getCar_2() with no return statement in it. Problem is that your if block will never execute because your condition: if((!empty($getCar_1) || (!empty($getCar_2)):

will never be true as $getCar_1 and $getCar_2 variables will always be empty since getCar_1() and getCar_2() are not returning anything.

So only echo you are providing from the function getCar_2() and getCar_1() is being printed as output to HTML and your if block is not executing at all.

You should put return statement in getCar_2() and getCar_1() functions if you want if block to work.Like this:

Instead of:

<?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>";
        }
    }
}
?>

Use this:

<?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;
}
?>

I hope it helps