Lister471 Posted January 1, 2012 Share Posted January 1, 2012 Hello all, i have been looking around the net but i am to be honest not 100% sure of what i should be searching for. I am trying to create a script that can generate a random list of players for a men of war event we have planned. I have managed to generate a list of random names but i have no idea how to sort them. $sql = "select * from players ORDER BY Rand() LIMIT 32"; $res = mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($res)) { extract($row); echo"$player<br>"; } This has you know generates a list of 32 names and its very hard to read, if there away to make it so after ever forth name there is a space. Player 1 Player 2 Player 3 Player 4 Player 5 Player 6 Player 7 Player 8 And so on. Also once the list is generate is there anyway way of adding something in the database that would link the 4 names togeather. Like team a team b and so on. Thanks in advance for any help. Quote Link to comment https://forums.phpfreaks.com/topic/254171-help-needed-for-another-newbie/ Share on other sites More sharing options...
SergeiSS Posted January 1, 2012 Share Posted January 1, 2012 Change your code $sql = "select * from players ORDER BY Rand() LIMIT 32"; $res = mysql_query($sql) or die (mysql_error()); $players=array(); while ($row = mysql_fetch_array($res)) { $players[]=$row['player']; // some more code } // do whatever you wish with array $players Quote Link to comment https://forums.phpfreaks.com/topic/254171-help-needed-for-another-newbie/#findComment-1303128 Share on other sites More sharing options...
wolfcry Posted January 2, 2012 Share Posted January 2, 2012 As SergeiSS mentioned, change your code to what he typed and if you'd like to display the names in a list format use what I type below just after the while loop closing bracket. foreach($players as $key => $pList){ echo $pList.'<br>'; } Also once the list is generate is there anyway way of adding something in the database that would link the 4 names togeather. Like team a team b and so on. Yes, but you need to have them stored in the database then when doing the query use the ORDER BY command. So for instance, let's say your list of names is stored a database named " "Team_DB" in the following tables: 1. Player_name 3. Team_Name then in your query you'd do something like this: $query = "SELECT `Player_name`, `Team_Name` FROM `Team_DB` ORDER BY `Team_Name`"; The above will list them by team, or you can simply remove the ORDER BY command and use PHP to display the output based on team if you're going for aesthetics. Quote Link to comment https://forums.phpfreaks.com/topic/254171-help-needed-for-another-newbie/#findComment-1303203 Share on other sites More sharing options...
Lister471 Posted January 2, 2012 Author Share Posted January 2, 2012 Many thanks for your replys and your help. I have managed to get something close to what i had in mind and it should do the job. Thanks again. Lister471 Quote Link to comment https://forums.phpfreaks.com/topic/254171-help-needed-for-another-newbie/#findComment-1303344 Share on other sites More sharing options...
jcbones Posted January 2, 2012 Share Posted January 2, 2012 You are pulling random names and then sorting them in list of 4 players for each team? <?php $sql = "select * from players ORDER BY Rand() LIMIT 32"; $res = mysql_query($sql) or die (mysql_error()); $i = 0; $team = 0; while ($row = mysql_fetch_array($res)) { if($i++ % 4 == 0) { echo '<br />Team: ' . ++$team . '<br />'; } echo $row['player'] . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/254171-help-needed-for-another-newbie/#findComment-1303356 Share on other sites More sharing options...
Lister471 Posted January 2, 2012 Author Share Posted January 2, 2012 I pulled the names and then used css to simply highlight every 4th to break up the text and make it easier on the eye. But just tried your code and wow you have no idea how many hours i have spent trying to do that and you prob typed it up in a matter of minutes. That is outstanding sir and i thank you once again. Your code is going to make everything alot better. Thanks again. Lister471 Quote Link to comment https://forums.phpfreaks.com/topic/254171-help-needed-for-another-newbie/#findComment-1303471 Share on other sites More sharing options...
jcbones Posted January 2, 2012 Share Posted January 2, 2012 You are most welcome. Glad it is sorted out. Quote Link to comment https://forums.phpfreaks.com/topic/254171-help-needed-for-another-newbie/#findComment-1303481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.