random avatars on register

DDDDec

Tongue Boxing Champion 2023
May 30, 2017
405
248
PHP:
$enable = array(
        "avatar" => true,
    );

    function randomChance($min, $max)
    {
        mt_rand($min, $max);
    }

    public function avatar()
    {
        global $db, $enable, $configAvatar;

        $users = $db->prepare("SELECT id FROM users");
        $users->execute();
        $userCount = $users->RowCount();

        if (!$enable['avatar'] || !$userCount > 0) {
            return $configAvatar;
        }

        $randomChance = randomChance(1, $userCount);
        $getAvatar = $db->prepare("SELECT avatar FROM users WHERE id = :id");
        $getAvatar->bindParam(":id", $randomChance);
        $getAvatar->execute();

        return $getAvatar;
    }

gives new users a random avatar from already registered users if there is any
 
Last edited:

Object

?
Nov 10, 2017
414
328
Pretty cool release & idea :)

Just a tip to improve your code readability - Try to follow "happy pathing"

So instead of checking if something is true then do this else do that, you just check if not true return something. This way your code becomes much "cleaner" and easier to read.

I've refactored your code to show a production example

PHP:
    $enable = array(
        "avatar" => true,
    );

    function randomChance($min, $max)
    {
        mt_rand($min, $max);
    }

    public function avatar()
    {
        global $db, $enable, $configAvatar;

        $users = $db->prepare("SELECT id FROM users");
        $users->execute();
        $userCount = $users->RowCount();

        if (!$enable['avatar'] || !$userCount > 0) {
            return $configAvatar;
        }

        $randomChance = randomChance(1, $userCount);
        $getAvatar = $db->prepare("SELECT avatar FROM users WHERE id = :id");
        $getAvatar->bindParam(":id", $randomChance);
        $getAvatar->execute();

        return $getAvatar;
    }
 

DDDDec

Tongue Boxing Champion 2023
May 30, 2017
405
248
Pretty cool release & idea :)

Just a tip to improve your code readability - Try to follow "happy pathing"

So instead of checking if something is true then do this else do that, you just check if not true return something. This way your code becomes much "cleaner" and easier to read.

I've refactored your code to show a production example

PHP:
    $enable = array(
        "avatar" => true,
    );

    function randomChance($min, $max)
    {
        mt_rand($min, $max);
    }

    public function avatar()
    {
        global $db, $enable, $configAvatar;

        $users = $db->prepare("SELECT id FROM users");
        $users->execute();
        $userCount = $users->RowCount();

        if (!$enable['avatar'] || !$userCount > 0) {
            return $configAvatar;
        }

        $randomChance = randomChance(1, $userCount);
        $getAvatar = $db->prepare("SELECT avatar FROM users WHERE id = :id");
        $getAvatar->bindParam(":id", $randomChance);
        $getAvatar->execute();

        return $getAvatar;
    }
I know i should but if im rushing to get it done ill just slam everything in i made this for a friend so hell do it for himself anyways, ill change thread for everyones eyes much love bb
 

Users who are viewing this thread

Top