/    Sign up×
Community /Pin to ProfileBookmark

Echo dash if 0

I’m getting data from my database by SUM and COUNT.

When I display it on the page, I’m displaying 0 (zero) when it’s nothing. How can I in SQL or PHP end echo a – (dash) if it’s not a zero.

I can do a if statement each time I echo it but it’s a lot of code and am wondering if there’s a simpler way?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@VITSUSAJan 25.2021 — Can you please share the code with us it will help us to understand exact problem.
Copy linkTweet thisAlerts:
@kiwisauthorJan 25.2021 — It's Simple really.

SELECT SUM(myculumn) as mySum FROM myTable GROUP BY myculumn

Then

echo $row['mySum']

If mySum is zero I want to print a dash. If not the value
Copy linkTweet thisAlerts:
@NogDogJan 25.2021 — A good candidate for the [ternary operator](https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) ( ? : ).
<i>
</i>echo $row['mySum'] == 0 ? '-' : $row['mySum'];
Copy linkTweet thisAlerts:
@kiwisauthorJan 25.2021 — @NogDog#1627159

Oh that's a perfect solution. I've never heard of it so I'll look into it. Thanks
Copy linkTweet thisAlerts:
@JennieMillerJan 26.2021 — i don't understand the problem.
×

Success!

Help @kiwis 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 4.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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...