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
Software Development
Programming
Programming Q&A
[PHP] Loop until false
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="Divide" data-source="post: 214845" data-attributes="member: 35464"><p>[CODE]</p><p>function randomstring($length = 10)</p><p>{</p><p> $characters = ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;</p><p> $randstring = '';</p><p> for ($i = 0; $i < $length; $i++)</p><p> {</p><p> $randstring = $characters[rand(0, strlen($characters))];</p><p> }</p><p> return $randstring;</p><p>}</p><p> </p><p>function indatabase($string)</p><p>{</p><p> global $con;</p><p> $q = "SELECT * FROM identities WHERE identity = '{$string}'";</p><p> if(!$result = $con->query($q))</p><p> {</p><p> die("Could not execute query.");</p><p> }</p><p> $count = $result->num_rows;</p><p> if($count > 0)</p><p> {</p><p> return true;</p><p> }</p><p> else</p><p> {</p><p> return false;</p><p> }</p><p>}</p><p> </p><p> </p><p>public function createIdentity()</p><p>{</p><p> $found = false;</p><p> $checkstring = "";</p><p> $finalstring = "";</p><p> $times = 0;</p><p> while(!$found)</p><p> {</p><p> if($times > 20)</p><p> {</p><p> $found = true;</p><p> die("Attempted to find a key too many times!");</p><p> }</p><p> $times++;</p><p> $checkstring = randomstring();</p><p> if(!indatabase($checkstring))</p><p> {</p><p> $found = true;</p><p> $finalstring = $checkstring;</p><p> }</p><p> }</p><p> $return $finalstring;</p><p>}</p><p>[/CODE]</p><p> </p><p>Will return a string which isn't already in the database, and will only check 20 times.</p><p> </p><p>Took about 20 mins of my life to code. Will come in useful for me too <img src="/styles/default/xenforo/smilies/emojione/smile.png" class="smilie" loading="lazy" alt=":)" title="Smile :)" data-shortname=":)" /></p></blockquote><p></p>
[QUOTE="Divide, post: 214845, member: 35464"] [CODE] function randomstring($length = 10) { $characters = ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’; $randstring = ''; for ($i = 0; $i < $length; $i++) { $randstring = $characters[rand(0, strlen($characters))]; } return $randstring; } function indatabase($string) { global $con; $q = "SELECT * FROM identities WHERE identity = '{$string}'"; if(!$result = $con->query($q)) { die("Could not execute query."); } $count = $result->num_rows; if($count > 0) { return true; } else { return false; } } public function createIdentity() { $found = false; $checkstring = ""; $finalstring = ""; $times = 0; while(!$found) { if($times > 20) { $found = true; die("Attempted to find a key too many times!"); } $times++; $checkstring = randomstring(); if(!indatabase($checkstring)) { $found = true; $finalstring = $checkstring; } } $return $finalstring; } [/CODE] Will return a string which isn't already in the database, and will only check 20 times. Took about 20 mins of my life to code. Will come in useful for me too :) [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
[PHP] Loop until false
Top