Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/254171-help-needed-for-another-newbie/
Share on other sites

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

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.

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 />';
    }

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.