Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9015193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:41:24+00:00 2026-06-16T03:41:24+00:00

I have develop this function which uses recursion to call itself. I need to

  • 0

I have develop this function which uses recursion to call itself. I need to generate a unique activation code.

It goes like this:
1. A random string is generated using the function in seperate class file. (See 2nd Code)
2. I am checking for uniqueness of that string in the activation table so that there are no duplicates.
3. In case if the random string is re-generated though I know that the chances are very less, but I dont want to take any chances. So I am checking it against the table records.

If the generated activated code is already there, then function should call itself again to generate a new activate code and the whole process goes on till we get a unique activation code which doesnt exist in the table records.

My question is that, Have I coded it correctly i.e. the recursion part. Please let me know if its not correct or if anybody has better or effective way to achieve this.

Note: I am calling these functions from a class file. Simply speaking I am using OOPs.
So I need to know the code to call the recursion should be

return generateUniqueActivationCode();

or

return $this->generateUniqueActivationCode();

Any help will be highly appreciated.

    // Generate Unique Activation Code
    //*********************************************************************************
    public function generateUniqueActivationCode()
    {
        $mysql = new Mysql();
        $string = new String();

        // This is coming from the 2nd part of code snippet that I have added
        $activation_code = $string->generateActivationCode();

        // Is Activation Code Unique Check
        $sql = "SELECT activation_id FROM ". TABLE_ACTIVATION_CODES ." WHERE activation_code='$activation_code' LIMIT 1";
        $query = $mysql->query($sql);

        if($mysql->rowCount($query) > 0)
        {
            // This function is calling itself recursively
            return generateUniqueActivationCode();
        }
        else
        {
            return $activation_code;
        }
    }

This is the code for generating Random Activation code

class String
{
    // Generates A Random String (Can be used for generating password etc.)
    //*********************************************************************************
    public function generateRandomString($noofchars=8)
    {
        $salt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabchefghjkmnpqrstuvwxyz0123456789";
        srand((double)microtime()*1000000);
        $i = 0;
        while ($i <= $noofchars)
        {
            $num = rand() % 33;
            $tmp = substr($salt, $num, 1);
            $string = $string . $tmp;
            $i++;
        }
        return $string;
    }   
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:41:25+00:00Added an answer on June 16, 2026 at 3:41 am

    I would have typed it like this:

    public function generateUniqueActivationCode(){
            $mysql = new Mysql();
            $string = new String();
            do{
                $activation_code = $string->generateActivationCode();
                $sql = "SELECT activation_id FROM ". TABLE_ACTIVATION_CODES ." WHERE activation_code='$activation_code' LIMIT 1";
                $query = $mysql->query($sql);
            }while($mysql->rowCount($query) > 0);
            return $activation_code;
        }
    

    Do (that runs ATLEAST one time), but repeats as long as your rowCount returns something bigger than 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a Python system for which I develop, we usually have this module structure.
**No direct answers or code examples please, this is my homework which i need
I have been asked to re-develop an old php web app which currently uses
I have a problem in C. This is the question: Develop a C function
I have developed a c++ program which uses OpenCV. Now i want to develop
We have a web site with domain http://209.59.154.150/~phnxaudi/ ,. To develop this website we
I have to develop an application for an Android Tablet. This will do the
Let's say I have a develop branch. I create a feature branch from this
I have to develop a application (Winforms-application) which is connected to a database within
I have to develop an Intensity Meter, which will basically display the average intensity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.