How to Display Random Data on MySQL using PHP? -
hello have tables :
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 source code :
<?php mysql_connect("localhost", "root", "1234") or die(mysql_error()); mysql_select_db("databasetransport") or die(mysql_error()); $employees = mysql_query("select * employee order 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 rethink idea."; break; } $toomany = substr($letters, $position2, 1); } } ?> to display these data :
= nayeon b = jihyo c = jungyeon d = dahyun e = sana f = mina g = tzuyu f = chaeyeong h = chaeyeong = momo the problem want random data (from result before):
c = jungyeon = nayeon h = chaeyeong b = jihyo = momo f = mina g = tzuyu e = sana f = chaeyeong d = dahyun so add codes :
<?php mysql_connect("localhost", "root", "1234") or die(mysql_error()); mysql_select_db("databasetransport") or die(mysql_error()); $employees = mysql_query("select * employee order 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 rethink idea."; break; } $toomany = substr($letters, $position2, 1); } } function generaterandomstring($length = 10) { $characters = $positions; $characterslength = strlen($characters); $randomstring = ''; ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, $characterslength - 1)]; } return $randomstring; } echo generaterandomstring(); ?> but nothing happened (lol) may know problem? thank you
just build array , 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 side here, shuffle not maintain array keys, , modifies array meaning actual return of shuffle boolean ( true | false ) value.
- it keep names letter assigned ( if desirable )
- is simple , fast
- is readable
- insures don't pull same row out twice.
Comments
Post a Comment