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("employee") 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("employee") 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 not work, may know problem? thank you
you use shuffle randomize array mysql_fetch_array.
it this:
$letters = 'abcdefghijklmnopqrstuvwxyz'; $array = array(); while($row = mysql_fetch_assoc($employees)){ $array[] = $row } $i = 0; // assign letter each employee in correct order foreach($array $key => $row){ $array[$key]["letter"] = substr($letters, $i, 1); $i++; if($i > 25){ $i = 0; } } // shuffle array shuffle($array); // print each entry correctly assigned letter , name. foreach($array $row) { echo "<div>" . $row["letter"] . " = " . $row['employeename'] . " </div>"; }
Comments
Post a Comment