/    Sign up×
Community /Pin to ProfileBookmark

CSS not applying to div when adding PHP condition

Hello!

In the first place, i’m sorry if my question mathes the previous one, they are quite similar. I try to adapt the answer from the previous question for my problem but it didn’t work.

So here is the thing.
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:

[CODE]<?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
}
?>[/CODE]

and here is the CSS:

[CODE].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;
}[/CODE]

If i use only this code:

[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>[/CODE]

it displays normal. Here is a picture:
[URL=”https://i.stack.imgur.com/fj1RH.jpg”]https://i.stack.imgur.com/fj1RH.jpg[/URL]

And when adding the php if part it displays this:
[URL=”https://i.stack.imgur.com/dMPUY.jpg”]https://i.stack.imgur.com/dMPUY.jpg[/URL]

So when i add the php if, the css has no effect anymore. I tried many variants but did not find the solution.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 18.2016 — Does getCar_1() and getCar_2() actually output HTML? If so, then trying to assign whatever it actually returns (if anything?) to those $getCar_[1|2] variables might be messing up your HTML by outputting whatever it does right at that point in the code.
Copy linkTweet thisAlerts:
@AndreiCTauthorNov 18.2016 — Now i see if that i write this i have no output:

<?php

$getCar_1 = getCar_1();

$getCar_2 = getCar_2();

echo $getCar_1;

echo $getCar_2;

?>
Copy linkTweet thisAlerts:
@AndreiCTauthorNov 18.2016 — Sorry I made a mistake. It outputs the cars.
Copy linkTweet thisAlerts:
@AndreiCTauthorNov 19.2016 — I think the issue comes from the functions get getCar_1(); and getCar_2(); which are in functions.php page and look like this:

[CODE]<?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>";
}
}
}
?>[/CODE]
Copy linkTweet thisAlerts:
@NogDogNov 19.2016 — Yeah, that's the problem with functions actually echoing output: you can't call them to test their result or modify the output. If you want to test the result then use it later, you would modify that function to return the text instead of outputting it. You could do something like:
[code=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());
$text = '';
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;
}
?>
[/code]

Then you can call the function and assign it's output to a variable like you did, earlier, or if you just want to use it directly, echo the function.
Copy linkTweet thisAlerts:
@AndreiCTauthorNov 19.2016 — @NogDog Thank you so much, now it works, and i learned something new.
Copy linkTweet thisAlerts:
@AndreiCTauthorNov 20.2016 — Sorry to return, but i have one more issue. mysql_fetch_array is returning only one row. It's returning only the last car for each getCar_1(); getCar_2(); functions, where there are more cars in them. And i don't figure out how to modify the function.
Copy linkTweet thisAlerts:
@AndreiCTauthorNov 20.2016 — It works now by replacing [CODE]$text = "[/CODE] with [CODE]$text .= "[/CODE].
Copy linkTweet thisAlerts:
@NogDogNov 20.2016 — Good catch.
×

Success!

Help @AndreiCT spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...