If statement

Hi guys…Very new to PHP so please bear with me.

I have a section of code in our files that i want to only show if the actual database has entries in that category.

We run a dog rescue website and have 3 categories of dogs for adoption…Male/Female/Couples.

We will always have Male and Female dogs for adoption but not always couples for adoption. So what i would like to do is only show the category “Couples” if there are dogs in it.

The code i have at present is :-

[php]


Couples For Adoption

    <?php foreach($foliorow as $frow):?> <?php $url = ($core->seo == 1) ? $core->site_url . '/' . $frow['slug'] . '.html' : $core->site_url . '/modules.php?module=portfolio&do=portfolio&productname=' . $frow['slug'];?> <?php if (( $frow['catname'] == "Reserved") || ($frow['catname'] == "Coming soon")) : ?> <?php else: if ( $frow['sex'] == 2) : ?>
  • <?php echo $frow['title'.$core->dblang];?>

      <div><?php echo cleanSanitize($frow['short_desc'.$core->dblang],100);?></div>
    </div>
    
    <?php endif; endif; endforeach;?>
    [/php]

    “Sex” is obviously the category, in this case being “2”.

    Is there a way to only display this category if there are any dogs in it and to hide it if the query produces no results?

    Thanks guys…Look forward to your reply :slight_smile:

I know it’s best to try to keep php and html separate, but there are times it’s best not to. An in this case it difficult to debug as you can already tell.

In my opinion it’s better to use braces {} than the hardly used endif, etc…

[php]if ( $rich === “yes”) {
echo “I’m going to buy an island and have my own government”;
} else {
echo “I’m going to be a bum on a beach in Hawaii!”;
}[/php]

You are also mixing if-statements with the ternary statement (I think?), while the ternary statement is used as an if-statement it isn’t one.

the ternary statement is written like the following example:

[php]$user = isset($_SESSION[‘user’]) ? $_SESSION[‘user’] : NULL;[/php]

This is an old CMS system which we started to use over 3 years ago now…I’m reluctant to change it as it would mean a whole revamp of the site and as a charity we do not have the funds to pay vast sums to revamp the site.

I know its a difficult ask, but i’d really like a solution to what we have as opposed to what we shold have if you know what i mean. Sorry, i don’t wish to sound rude or anything and honestly it wasn’t meant to sound like that at all :slight_smile:

Without seeing more of the code, it is difficult to show what you need to do. You essentially need a count of that column. If the count is greater than 0, display it, if not skip it.

I would recommend putting a post up on LinkedIn. There are a number of developers that will donate time for a cause they feel strongly for. You get what you need and they get charity work on their resume, everyone wins.

[member=57087]Strider64[/member], I actually like the alternate syntax for long blocks. It is far easier to know where the bracket ties in with the block when there is a verbiage stating what it ends.

Sponsor our Newsletter | Privacy Policy | Terms of Service