<?php

function genUser($vorname, $nachname){

 $vorname = mb_convert_case($vorname, MB_CASE_LOWER, "UTF-8"); 
 $nachname = mb_convert_case($nachname, MB_CASE_LOWER, "UTF-8"); 
 
 $upas = Array("ä" => "ae", "ü" => "ue", "ö" => "oe", "Ä" => "Ae", "Ü" => "Ue", "Ö" => "Oe", "ß" => "ss"); 
 $vorname = strtr($vorname, $upas);
 $nachname = strtr($nachname, $upas);

 /* für 6+2 Regelung
 if(strlen($nachname) <=6){
   $anz_vorname = 8-strlen($nachname);
 }else{
   $anz_vorname = 2;
 }
 
 $vorname = substr($vorname,0,$anz_vorname);
 $nachname = substr($nachname,0,6);
 $uid = $nachname.$vorname;
 */
 
 
  
 $uid = $nachname.$vorname;
  
  

 $uid = substr($uid,0,8);
 
 
 $db = dbconnect();
 $zaehler=1;
 $found=0;
 do {
   $res_anz = $db->query("SELECT count(user) Anz
                             FROM prog_admin
                            WHERE user='$uid'
                         ")
             or die ("Cannot execute query genUser");
   $row_anz = $res_anz->fetch_array();

   if($row_anz['Anz'] == 0){
    $found=1;
   }else{
     if($zaehler <= 9){
       $uid = substr($uid,0,7);
       $uid = $uid.$zaehler;
     }elseif($zaehler <= 99){
       $uid = substr($uid,0,6);
       $uid = $uid.$zaehler;
     }elseif($zaehler <=999){
       $uid = substr($uid,0,5);
       $uid = $uid.$zaehler;
     }elseif($zaehler <=9999){
       $uid = substr($uid,0,4);
       $uid = $uid.$zaehler;
     }else{
      $uid = "Bitte Administrator kontaktieren";
     }
     $zaehler++;
   }
 } while($found<1);

return trim($uid);
}


?>