If value is true show, if value is false show something else

I have some image file names stored in my database table with a category_id for each image.

I have the gallery all on one page and query each image by the category_id.
Example:

[ul][li]Gallery 1 = images with value of category_id 1[/li]
[li]Gallery 2 = images with value of category_id 2[/li]
[li]Gallery 3 = images with value of category_id 3[/li][/ul]

So my question is, if my category_id’s value is 3 then I want it to show the image(s).

If the category_id is not 3 as there is no images with the category_id value of 3, I want to display a message saying, “no content”, or whatever.

Here is the part of my code I’m trying to figure out. Hoping someone can give me a hand.

[php]$images = DB::getInstance()->query("SELECT category_id FROM images WHERE category_id = 3 ");
$category_id = (int)[“category_id”] == 3 ?true:false;

if($category_id == (int)3){ ?>

Content

<?php }

if(!$category_id == (int)3 ){ ?>

No Content

<?php } ?>[/php]

you dont need to tell it that those values are integers. that 2nd line makes no sense. looks like you’re trying to do a shorthand if else. That’s why those other if statements dont work. just define $category_id, get rid of those (int) and it should work.

Why not write it with if then else?

[php]
if($category_id == 3){
$content=“Content”;
}
else {
$content=“No Content”;
}
echo ‘

’.$content.’

’;
[/php]

This could be simplified even more with

[php]
$content=“Content”;
if(!$category_id == 3){
$content=“No Content”;
}
echo ‘

’.$content.’

’;
[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service