PDA

View Full Version : Text area question


Temper
06-20-2003, 05:37 PM
I'm making a website that creates tournament brackets for users. What I want to do is have one text area where they can imput names, and they would be submitted into the brackets. In the text area, I want to turn each new line into a variable. For instance, if somone were to put two names into the text area, it would look like this:

User1
User 2

So, the two users in the text area are seperated by a new line, or a <br>, and the script reads it as

User1 = $name1
User2 = $name2
etc.

Is there any advice or tutorial that I can read that will teach me this?

Also, (a different question now):
If they get inserted into to tournament brackets, and the user dosen't like the names in that order, how do I random names appear in the brackets?

Ex.
If I don't make it random, down the bracket list it'll look somthing like

$name1
$name2
$name2
etc

Can I make the names put in a random order?

If I confused the heck out of you with these questions, let me know and I'll try to explain it again.

Thank you.
~Mike

Spookster
06-20-2003, 06:56 PM
I would avoid using a textarea for that. Just give them single line text inputs for each name. If the number of names is not know then first give them a text input where they can type in the number of names they need to enter and then based on that generate the inputs for those. This saves you the trouble of attempting to parse that. Never just hope the end user will type things in correctly or in the correct format. Always assume they won't and then remove the possibility for them to make mistakes.

Taylor_1978
06-20-2003, 10:20 PM
I'm currently working on a tournament system also - now... I know this is probably not the best way to go about it - but here is I made the random seeding:

Once all players were placed in the tournament's table - I have a coloumn called "random" - I then use rand() to genrate a number between 1,000 and 9,999. The "random" coloumn is set at "unique"... The number generated for each layer is inserted into the table in their row. Then, when doing the seedings, I use "ORDER BY random" at the end of my SELECT * FROM

Did that make sense? LOL

I dont have time now - but if that made no sense, let me know and I will post an example of the script I used next time I am here.

Taylor. :thumbsup:

Dylan Leblanc
06-21-2003, 01:17 AM
You can use something like explode("\n", $text) to separate the names into an array. It may also be good to trim() each element then.

Temper
06-25-2003, 06:15 PM
I've build a basic html file of what my tournament brackets will look like, and put in variables such as $player1, 2, 3 etc in all the starting areas. The user on another page imputs the names that will belong to the variables. What I want to do is have a php script of some sort that will randomly re-order the variables.
For example, the first time the user imputs names, the order will be
1, 2, 3, 4, 5, etc

Unfortunatly, the user dosen't like the order, so he/she presses the button to put the players into another random order, and the page reloads with the players in order of

2, 5, 4, 1, 3, etc.

How can I go about doing this?
My html file is here (http://www.pwcs.ca/test.html)
Thanks in advance.
~Mike

mordred
06-26-2003, 12:23 AM
Well, I would put all player names (the single variables) into an array and use array_rand to generate a random key array from that. This key list you iterate through and append the variable at the current key to a new array.

Quick hack:


srand ((float) microtime() * 10000000);
$players = array("Ben", "Bill", "Bob", "Sue");
$randomized = array_rand($players, count($players));

$newPlayers = array();
for ($i = 0; $i < count($randomized); $i++) {
$newPlayers[] = $players[$randomized[$i]];
}
var_dump($newPlayers);


Adapt the $players array to your needs.