3

I am doing an amount of users check for our website, below is the code. How can i use the word "user" if there is only 1 account and how can i use "users" if there is >1.

code:

       $result = mysql_query("SELECT * FROM users WHERE user_id='$userid'");
       $num_rows = mysql_num_rows($result);

        echo "amount of users.";
AAA
  • 3,120
  • 11
  • 53
  • 71
  • 2
    The current way you get the amount of users is terrible, the better could be `SELECT COUNT(*) AS cnt ...` and retrieve the data from `cnt` array key, not from `mysql_num_rows` – zerkms Feb 21 '11 at 14:17
  • Can you give me the full code, i am learning... – AAA Feb 21 '11 at 14:24
  • 1
    Could this be handled my MySQL during select? There is a conditional operator 'CASE' in MySQL, maybe you can make use of it, I'm not sure. – Dmitri Feb 21 '11 at 14:31

4 Answers4

10

All of these answers will work well, but if you're looking for a reusable way, you can always externalise it:

function get_plural($value, $singular, $plural){
    if($value == 1){
        return $singular;
    } else {
        return $plural;
    }
}

$value = 0;
echo get_plural($value, 'user', 'users');

$value = 3;
echo get_plural($value, 'user', 'users');

$value = 1;
echo get_plural($value, 'user', 'users');

// And with other words
$value = 5;
echo get_plural($value, 'foot', 'feet');

$value = 1;
echo get_plural($value, 'car', 'cars');

Or, if you want it to be even more automated, you can set it up to only need the $plural variable set when it is an alternate word (eg: foot/feet):

function get_plural($value, $singular, $plural = NULL){
    if($value == 1){
        return $singular;
    } else {
        if(!isset($plural)){
            $plural = $singular.'s';
        }
        return $plural;
    }
}

echo get_plural(4, 'car');   // Outputs 'cars'
echo get_plural(4, 'foot');  // Outputs 'foots'
echo get_plural(4, 'foot', 'feet');  // Outputs 'feet'
Adam
  • 2,851
  • 1
  • 20
  • 20
6

Maybe I get it wrong, but it is obvious:

echo $num_rows > 1 ? 'users' : 'user';
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
  • OK, so all i have to do is add the $num_rows in front of user and users right? – AAA Feb 21 '11 at 14:19
  • Yeah, I guess so. If you use it on multiple places consider putting it in a variable for further use so you will be DRY. :) – Adam Arold Feb 21 '11 at 22:18
4

Try

if($num_rows === 1)
{
    echo "user";
}
else
{
    echo "users";
}

or in short form

echo $num_rows === 1 ? "user" : "users";
Nalum
  • 4,143
  • 5
  • 38
  • 55
  • 1
    the short form is correct, but your long form is bad -- it fails if `$num_rows` is zero (in fact it fails if it's any number <1, but since it's an integer count variable, zero is the only plausible value). You can fix it by simply not specifying the `if(>1)` condition along with the `else`; it isn't necessary. – Spudley Feb 21 '11 at 14:20
  • If for example, your $userid variable is coming from a GET or POST request, then it could be possible for a user to input a value that would make your SQL query return 0 rows, so it's always good to take into account such a case where $num_rows could be 0. – WNRosenberg Feb 21 '11 at 14:40
4
if ($num_rows === 1) {
    echo "a user.";
}
else if ($num_rows > 1) {
    echo "amount of users.";
}
else {
    echo "no users".
}
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158