I propose you to create a variable $ i that takes the sequence of the numbers that you will use as suffixes in the name, each time the if is evaluated and the user already exists, this variable is increased by 1, it is concatenated with the name of the user that initially had and the query is repeated, so on until finding a JPerez user. $ i available:
// Separamos los Nombres
list($pri_nombre, $seg_nombre) = explode(" ", $nombre);
// Separamos los Apellidos
list($pri_apellido, $seg_apellido) = explode(" ", $apellido);
// QUITAMOS LOS ESPACIOS EN BLANCO, EJEMPLO DE APELLIDO: DEL CAMPO
$pri_nombre = preg_replace('/[ <>\'\"]/', '', $pri_nombre);
$seg_nombre = preg_replace('/[ <>\'\"]/', '', $seg_nombre);
$pri_apellido = preg_replace('/[ <>\'\"]/', '', $pri_apellido);
$seg_apellido = preg_replace('/[ <>\'\"]/', '', $seg_apellido);
// Ticket de Condicion
$username_encontrado = FALSE;
$largo_nombre = strlen($pri_nombre); //largo del nombre
$j = 1; //INDICA LA CANTIDAD DE CARACTERES A EXTRAER DEL NOMBRE
$username = cortar_string(substr($pri_nombre, 0, $j) . $pri_apellido, 12);
$i = 0;//Sufijo numero del username
$username_inicial = $username;
$username_encontrado = FALSE;
while (!$username_encontrado) {
$ver_username = "SELECT * FROM personal WHERE NomUser = '$username'";
$resultt = mysqli_query($conn, $ver_username);
$resultCheck = mysqli_num_rows($resultt);
if ($resultCheck > 0) {
$username_encontrado = FALSE;
// Aqui agregar el username se repite
++$i;//Se incrementa en uno
$username = $username_inicial.$i;//Se agrega al nombre de usuario original
} else {
$username_encontrado = TRUE;
}
}
var_dump($username_encontrado);
var_dump($username);
That option would work for you, however it seems inefficient, considering all the queries you have to ask the database, if for example, in the database exists from JPerez, JPerez1 ... to Jperez10, then by going in sequence only until query No. 12 would get an available username. Therefore, as a second alternative I propose this (although it uses PDO and not mysqli):
The advantage of this option is that it prevents SQL injections, be careful when inserting variables directly into the SQL with mysqli_query, and secondly, do not make repeated queries until you get a valid user. Basically, it makes the first query to verify if the user exists. If it exists, it makes a second query where it extracts all the users that start with that user name (thus it obtains all the suffixes assigned). Stores all users occupied in an array, which is used as a reference to compare whether new calculated users are available or not.
Additionally the variable $ i does not start from scratch, but from the next number to the number of usernames occupied with that name, so we do not have to go all the way from the beginning, although this does not guarantee that there will be only one iteration, but if it reduces them, there may be Jperez6, but the result of the count is better than 6 because JPerez5 has been erased. I hope it is understood:
<?php
$conn = new PDO('mysql:host=localhost;dbname=nombreBD', "root", "");
// Separamos los Nombres
list($pri_nombre, $seg_nombre) = explode(" ", $nombre);
// Separamos los Apellidos
list($pri_apellido, $seg_apellido) = explode(" ", $apellido);
// QUITAMOS LOS ESPACIOS EN BLANCO, EJEMPLO DE APELLIDO: DEL CAMPO
$pri_nombre = preg_replace('/[ <>\'\"]/', '', $pri_nombre);
$seg_nombre = preg_replace('/[ <>\'\"]/', '', $seg_nombre);
$pri_apellido = preg_replace('/[ <>\'\"]/', '', $pri_apellido);
$seg_apellido = preg_replace('/[ <>\'\"]/', '', $seg_apellido);
// Ticket de Condicion
$username_encontrado = FALSE;
$largo_nombre = strlen($pri_nombre); //largo del nombre
$j = 1; //INDICA LA CANTIDAD DE CARACTERES A EXTRAER DEL NOMBRE
$username = cortar_string(substr($pri_nombre, 0, $j) . $pri_apellido, 12);
$username_inicial = $username;
$username_encontrado = FALSE;
$sql_1 = "SELECT username FROM personal WHERE NomUser = :usr";
$query = $conn->prepare($sql_1);
$query->execute(array(':usr' => $username));
if ($query->rowCount() > 0) {
$sql_2 = "SELECT username FROM personal WHERE NomUser LIKE :usr";
$query_2 = $conn->prepare($sql_2);
$query_2->execute(array(':usr' => "{$username}%"));
$resultado = $query_2->fetchAll(PDO::FETCH_COLUMN);//Obtiene array con usuarios ocupados
$i = $query_2->rowCount();//Sufijo numero del username
while (!$username_encontrado) {
++$i;
$username = $username_inicial.$i;
if (in_array($username, $resultado)) {
$username_encontrado = FALSE;
} else {
$username_encontrado = TRUE;
}
}
} else {
$username_encontrado = TRUE;
}
echo $username;
?>
I hope it works for you, I remain attentive.