0

Hello i have tables like this :

Employee

EmployeeID  EmployeeName 
1234        Nayeon     
1235        Jihyo     
1236        Jungyeon     
1237        Dahyun     
1238        Sana       
1239        Mina
1240        Tzuyu
1241        Chaeyeong
1241        Chaeyeong
1242        Momo

i used this source code :

<?php

mysql_connect("localhost", "root", "1234") or die(mysql_error());
mysql_select_db("databasetransport") 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);
    }
}
?>

to display these data :

 A  = Nayeon     
 B  = Jihyo     
 C  = Jungyeon     
 D  = Dahyun     
 E  = Sana       
 F  = Mina
 G  = Tzuyu
 F  = Chaeyeong
 H  = Chaeyeong
 I  = Momo

The problem is i want to random that data like this (from the result before):

C  = Jungyeon 
A  = Nayeon 
H  = Chaeyeong    
B  = Jihyo 
I  = Momo        
F  = Mina
G  = Tzuyu
E  = Sana  
F  = Chaeyeong
D  = Dahyun     

so i add codes like this :

 <?php

mysql_connect("localhost", "root", "1234") or die(mysql_error());
mysql_select_db("databasetransport") 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['EmployeeID'] . " </div>";
      $position ++;
    if($position > 25) {
        $position = 0;
        $position2 ++;
        if($position2 > 25) { echo "We need to rethink this idea."; break; }
        $toomany = substr($letters, $position2, 1);
    }
}


function generateRandomString($length = 10) {
    $characters = $positions;
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}


echo generateRandomString();

?>

but nothing happened (LOL) may you know where is the problem? Thank you

1

3 Answers 3

1

Just build your array and then use this

http://php.net/manual/en/function.shuffle.php

cheers.

$a = array(
    'A = Nayeon',
    'B = Jihyo',
    'C = Jungyeon',
    'D = Dahyun',
    'E = Sana',
    'F = Mina',
    'G = Tzuyu',
    'F = Chaeyeong',
    'H = Chaeyeong',
    'I = Momo',
 );
 shuffle( $a );
 var_export( $a );

Outputs:

array (
  0 => 'I = Momo',
  1 => 'E = Sana',
  2 => 'F = Chaeyeong',
  3 => 'F = Mina',
  4 => 'B = Jihyo',
  5 => 'A = Nayeon',
  6 => 'C = Jungyeon',
  7 => 'G = Tzuyu',
  8 => 'D = Dahyun',
  9 => 'H = Chaeyeong',
)
  • As a side here, shuffle does not maintain the array keys, and it modifies the array meaning that the actual return of shuffle is a Boolean ( true | false ) value.
  • It does however keep the names with the letter assigned ( if that is desirable )
  • Is simple and fast
  • Is readable
  • Insures you don't pull the same row out twice.
Sign up to request clarification or add additional context in comments.

Comments

0

I think you can do all this in SQL:

select (@c := CHAR(ASCII(@c) + 1)) as c, EmployeeName
from pegawai cross join
     (select @c := 'A') params
order by rand();

This assigns the characters in random order. If you want them in EmployeeId order:

select p.*
from (select (@c := CHAR(ASCII(@c) + 1)) as c, EmployeeName
      from pegawai cross join
           (select @c := 'A') params
      order by EmployeeId
     ) p
order by rand();

1 Comment

Sorry i have tried this but when i run this code randomized only for EmployeeName not the alphabet, my problem is i want to random both the alphabeth and EmployeeName, with the same value but with a different sequence like my explanation above. Thank you for your help
0

You could change your query to:

$employees = mysql_query("SELECT * FROM pegawai ORDER BY RAND()") 

And then:

$letters = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ'); //Use is as array
shuffle($letters); //Mix the array
$position = 0;
$position2 = 0;
$toomany = '';
    while($row = mysql_fetch_array( $employees )) {
        echo "<DIV>" . $toomany[$position] . " = " . $row['EmployeeID'] . " </div>";
          $position ++;
        if($position > 25) {
            $position = 0;
            $position2 ++;
            if($position2 > 25) { echo "We need to rethink this idea."; break; }
            $toomany = substr($letters, $position2, 1);
        }
    }

4 Comments

sorry but i got message like this You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by RAND()' at line 1
i have tried this one too, but still got the error message like this Warning: explode() [function.explode]: Empty delimiter in C:\xampp\htdocs\coba\random.php on line 9 Warning: shuffle() expects parameter 1 to be array, boolean given in C:\xampp\htdocs\coba\random.php on line 10 Random is working but the generate's alphabet is not working, and the result only like this : = 1234 = 1239 = 1237 = 1235 = 1236 = 1238
oh sorry i mean the result is only like this : C = Jungyeon = Nayeon = Chaeyeong = Jihyo = Momo = Mina = Tzuyu = Sana = Chaeyeong = Dahyun
sorry but the result still like this = Jungyeon = Nayeon = Chaeyeong = Jihyo = Momo = Mina = Tzuyu = Sana = Chaeyeong = Dahyun

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.