Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Server Development
Habbo Retros
Habbo Releases
random avatars on register
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Object" data-source="post: 467491" data-attributes="member: 78351"><p>Pretty cool release & idea <img src="/styles/default/xenforo/smilies/emojione/smile.png" class="smilie" loading="lazy" alt=":)" title="Smile :)" data-shortname=":)" /></p><p></p><p><strong>Just a tip</strong> to improve your code readability - Try to follow "happy pathing"</p><p></p><p>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.</p><p></p><p>I've refactored your code to show a production example</p><p></p><p>[PHP]</p><p> $enable = array(</p><p> "avatar" => true,</p><p> );</p><p></p><p> function randomChance($min, $max)</p><p> {</p><p> mt_rand($min, $max);</p><p> }</p><p></p><p> public function avatar()</p><p> {</p><p> global $db, $enable, $configAvatar;</p><p></p><p> $users = $db->prepare("SELECT id FROM users");</p><p> $users->execute();</p><p> $userCount = $users->RowCount();</p><p></p><p> if (!$enable['avatar'] || !$userCount > 0) {</p><p> return $configAvatar;</p><p> }</p><p></p><p> $randomChance = randomChance(1, $userCount);</p><p> $getAvatar = $db->prepare("SELECT avatar FROM users WHERE id = :id");</p><p> $getAvatar->bindParam(":id", $randomChance);</p><p> $getAvatar->execute();</p><p></p><p> return $getAvatar;</p><p> }</p><p>[/PHP]</p></blockquote><p></p>
[QUOTE="Object, post: 467491, member: 78351"] Pretty cool release & idea :) [B]Just a tip[/B] 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; } [/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Server Development
Habbo Retros
Habbo Releases
random avatars on register
Top