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

  • SEARCH
  • Home
  • 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 9307523
In Process

The Archive Base Latest Questions

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

I am a bit stuck with my code. I am practising and would like

  • 0

I am a bit stuck with my code.

I am practising and would like to achive the following.

Before an user sign up, i want to check the users username exists, if exists than increase the name by one.

So my logic works like this. User gives his/her first and last name, generates an username based on that.

function

function unique_name($first_name, $last_name) {

$username = strtolower($first_name.'.'.$last_name)

$check = mysqL_query(" SELECT username WHERE username = '".$username."' ");

    if(count($check == 1)) {
        $i = 2;
        foreach($check as $user) {
            return $user['username'].$i;
            $i++;
        }
    } else {
        return $username;
    }
}

$username = unique_name($_POST['first_name'], $_POST['last_name']);
// saveing the increased username

So actually the script should check if the generated unique username exsits if exsits increase, and encrease it even if it exsits with a number

example

Tom Anderson signs up, username will be tom.anderson, saved as tom anderson

Second Tom Anderson signs up, username will be tom.anderson, it already exists, save the new one as tom.anderson2.

The third user signs up he is tom anderson too, username tom.anderson* exsits twice, increase to tom.anderson3.

So i dont have a problem checking if the user exsits and saveing it, my problem is, how to increase every username if exist?

I am not asking anybody to write this for me i am nust looking for a hint

Thank you

EDIT

Thank you for the replies but this does not work

if(count($check) == 1) {
    $i = 2;
    foreach($check as $user) {
        return $user['username'].count($check);
        $i++;
    }
} else {
    return $username;
}

This only check the username with out a number, so if username tom.anderson exists it increases to tom.anderson1 but if tom.anderson1 exists too it wont increase to tom.anderson2

  • 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-19T00:24:43+00:00Added an answer on June 19, 2026 at 12:24 am

    Can you have something that just appends the count of the rows returned, unless it’s 0?

    if(count($check) > 0) {
       return $user['username'].count($check);
    } else {
        return $username;
    }
    

    change the SQL:

    " SELECT username WHERE username LIKE '".$username."%' " 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a little bit stuck: I would like to change the max option
I am once again a bit stuck in my practising. I want an MP3
I was following this tutorial, bit stuck here: This code doesn't compile, and the
I'm a bit stuck on the following code: $(document).ready(function () { /* Init DataTables
I'm a bit stuck with my code: def setVenueImage(img): img = images.Image(img.read()) x, y
I haven't implemented a search feature before and feel a bit stuck. I have
I'm a bit stuck on the following linq query. I'm trying to find all
I am a bit stuck up with the following question, Consider the following MIPS
I'm a bit stuck on my code right now. I've dynamicly made a form,
I'm a bit stuck with refactoring my code to make a method available to

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.