Forum Moderators: coopster

Message Too Old, No Replies

assigning a variable from a drop down

         

Tinman

11:25 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Ok, in the last 2 days I have spent about 10 hours trying and reading.
I'm finally going to humbily ask for help.

I am trying to build a scrabble letter generator (I won't be infringing on the copyright, it's a temporary name)

My code works if I hand code the string $numofletters

I am having great difficulty in having a drop down menu assign that string.

page name is scrabble.php

here is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

</head>
<body>

<?php
mysql_connect("#*$!", "#*$!", "#*$!") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("tindes_scrabble") or die(mysql_error());
echo "Connected to Database";
echo "<br />";

if(isset($_GET["letters"])) $numofletters = "0";
else $numofletters = $_GET["letters"];

// Retrieve all the data from the "letters" table
$recordsquery = "SELECT * FROM letters ORDER BY RAND() LIMIT $numofletters";
// Assign data to string
$rowresult = mysql_query($recordsquery) or die(mysql_error());
//Loop and display for all records
WHILE($row = mysql_fetch_array( $rowresult)){
echo " ID: ".$row['ID']. "----"." Letter: ".$row['Letter'];
echo "<br />";
}
?>
<p>How many letters would you like?</p>

<select name="letters" id="quantity">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>

</body>
</html>

Thank you for reading/replying,
Tin

Readie

12:53 am on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World Tinman.

Something like this should work:

HTML:
<form method="post">
<select name="letter_count">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<input type=submit" value="Go" />
</form>

PHP:
$lets = $_POST['letter_count'];
if(isset($lets) && $lets !== "") {
$letters = range('A', 'Z');
$return = '';
for($i = 0; $i < $lets; $i++) {
$num = rand(0, 25);
$return .= $letters[$num];
}
echo $return;
} else {
// Some error message?
}

Tinman

1:27 am on Apr 9, 2010 (gmt 0)

10+ Year Member



Thank you for your response Readie,

I did test your suggestion, and it does work (other than you missed a quote before "submit" button.

But, I kinda need my original code to function since:
I am connecting to a MySQL database for the letters to use when playing scrabble ( I mean a word game :o) ).
Also there's like 9 letter "A"'s for example, plus I will be deleting the letters from the database once they are generated so they will not be assigned to a player later.

So everything works fine database wise, I just need the string $numofletters to be assigned from the dropdown menu.
I am stuck, still trying, and still stuck.

Does that make sense?
Thanks again.

Readie

2:03 am on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, so we're getting every piece of data under the column header "letter" in a MySQL table, and randomly selecting (1-7) letters based on a select input, and after that remove all the letters that were used up from the database?

In that case:

$lets = $_POST['letter_count'];
if(isset($lets) && $lets != "") {
$sql = 'SELECT letter FROM letters';
$result = mysql_result($sql);
$rows = mysql_num_rows($result);

$let_count = ($rows - 1);
$return = '';
$check = '';

for($i = 0; $i < $lets; $i + 0) {
$num = rand(0, $let_count);
if(!array_search($num, $check)) {
$check[$i] = $num;
$new_let = mysql_result($result, $num, "letter");
$delete[$i] = $new_let;
$return .= $new_let;
$i++;
}
}
foreach($delete as $del) {
$sql = 'DELETE FROM letters WHERE letter = "' . $del . '" LIMIT 1';
mysql_query($sql);
}

echo $return;
}

Not 100% certain that'll work, was typed off the cuff, but the basic principle is sound.

you missed a quote before "submit" button.

Well, I've got to keep you sharp somehow :P

Readie

3:42 pm on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a follow up, should probably change this line:
if(isset($lets) && $lets != "") {

To this:
if(isset($lets) && $lets != "" && is_int($lets) && $lets <= 7) {

If you do that, people who try and cause damage by sending data to your site from (even just a .html file of the desktop) will not get to see a PHP error.

Should also add an if before the for loop too, something along the lines of:
if($lets <= $rows) {

Tinman

3:16 pm on Apr 10, 2010 (gmt 0)

10+ Year Member



Thank you very much Readie,

I got it figured out from your first post and it works.
Now I will try the second example you gve me.

<p>How many letters would you like?</p>
<!--<form name="request" action="scrabble.php" method="get">-->
<form method="POST">
<select name="letters">
<option value="" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<input type="submit" value="Go" />

</form>

<?php
mysql_connect("#*$!", "#*$!", "#*$!") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("#*$!") or die(mysql_error());
echo "Connected to Database";
echo "<br />";

$numofletters = 0;
$ddamount = 0;
$rowresult = 0;
$recordsquery = 0;
$row = 0;

$ddamount = $_POST['letters'];
if(isset($ddamount) && $ddamount !=="") {
$numofletters = $ddamount;
}
else echo "Please select the number of letters you require.";

// Retrieve all the data from the "letters" table
$recordsquery = "SELECT * FROM letters ORDER BY RAND() LIMIT $numofletters";
// Assign data to string
$rowresult = mysql_query($recordsquery) or die(mysql_error());
//Loop and display for all records
WHILE($row = mysql_fetch_array( $rowresult)){
echo " ID: ".$row['ID']. "----"." Letter: ".$row['Letter'];
echo "<br />";
}

?>


One question, Is there a way to set the variables to null so that if I refresh the page it will resubmit the form with 0 instead of the last selected $numofletters.

Thanks again Readie,
I will try the second example you gave me.

Tin

Readie

3:36 pm on Apr 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One question, Is there a way to set the variables to null so that if I refresh the page it will resubmit the form with 0 instead of the last selected $numofletters.

I'm not sure... If you're going to store the current letters in use in a database or a session, you could perhaps use

header("Location: $_SERVER['REQUEST_URI']");

But if not... I don't think so. I will emphasize that I don't know for certain though.

Tinman

6:18 pm on Apr 10, 2010 (gmt 0)

10+ Year Member



Thanks Readie