Search code examples
phpmysqlxamppgenerated-code

How Generate data to alphabet on MySQL table using PHP?


Hello i have tables like this :

Employee

EmployeeID  EmployeeName 
1234        Sooyoung      
1235        Yoona     
1236        Tiffany     
1237        Hyoyeon     
1238        Taeyeon       
1239        Seohyun
1240        Sunny
1241        Yuri

i want to generate the employee id sequentially like this :

1234 as A     
1235 as B    
1236 as C   
1237 as D 
1238 as C      
1239 as E
1240 as D
1241 as F

may you know what mysql code and php what i have to use? thank you


Solution

  • This is how you would do it in PHP, I am assuming this is the method you want seeing as you tagged PHP.

    <?php
    
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    mysql_select_db("yourdatabase") or die(mysql_error());
    
    $employees = mysql_query("SELECT * FROM Employee ORDER BY EmployeeID") 
    or die(mysql_error());  
    
    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $position = 0;
    $position2 = 0;
    $toomany = '';
    
    while($row = mysql_fetch_array( $employees )) {
        echo "<DIV>" . $toomany.substr($letters, $position, 1) . " = " . $row['EmployeeName'] . " </div>";
        $position ++;
        if($position > 25) {
            $position = 0;
            $position2 ++;
            if($position2 > 25) { echo "We need to rethink this idea."; break; }
            $toomany = substr($letters, $position2, 1);
        }
    }
    
    ?>
    

    EDITED: What if you run out of alphabets ? what did you want to happen? I took a guess, but you will have to explain more if I am not right.

    EDITED: TurdPile has a simplified version of what I had. Accept his answer.