1
tober47
Counting iterations
  • 2010/3/3 14:14

  • tober47

  • Just popping in

  • Posts: 1

  • Since: 2010/3/3 1


I am new to PHP and need some help. I have a script to generate a random number, then to generate another random number until it matches the first one. All works well, but I want to count how many numbers were generated until the match was made. The loop I am using is DO......WHILE.

Thanks for any help

2
ghia
Re: Counting iterations
  • 2010/3/3 21:12

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


Is this for your school home work assignment?

3
culex
Re: Counting iterations
  • 2010/3/4 0:16

  • culex

  • Module Developer

  • Posts: 711

  • Since: 2004/9/23


lol or maybe for this weeks lotto numbers

Try this, providint of course the computer is allowed to try the same number more than once

// Numbers 1 to 1000 randomized
$numbers rand 11000 );

// Start guessing with no-guesses at all
$guesses 0;

// Keep guessing until you get it right
do {
 
$myGuess rand 11000 );
 
$guesses++;
 echo 
"I'm guessing: $myGuess";
} while ( 
$myGuess != $numbers );

echo 
"Right guess is $myGuess! I guessed it in $guesses tries";

4
ghia
Re: Counting iterations
  • 2010/3/5 15:53

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


An array.

5
culex
Re: Counting iterations
  • 2010/3/5 17:00

  • culex

  • Module Developer

  • Posts: 711

  • Since: 2004/9/23


I agree with Ghia. Imagine 1000+ queries to insert numbers and then after to compare Just an example using array to store numbers and in_array to check against after. This is pretty fun though. You could make $numbers an array fi. $numbers[1]..[2]..[3] etc ect and then see how many guesses it takes to hit right combination with 6 numbers forinstance
// Numbers 1 to 1000 randomized
$numbers rand 11000 );

// Array to hold guesses
$guessed = array();

// Start guessing with no-guesses at all
$guesses 0;

// Keep guessing until you get it right
do {
 
$myGuess rand 11000 );
// Push your guess in array if not already exist 
 
if (!in_array($myGuess$guessed)) {
 echo 
"I'm guessing: $myGuess";
 
array_push($guessed$myGuess);
 
$guesses++;
 } 
// If your guess is already there it will not count
} while ( $myGuess != $numbers );

echo 
"Right guess is $myGuess! I guessed it in $guesses tries";

6
tober47
Re: Counting iterations
  • 2010/3/5 18:32

  • tober47

  • Just popping in

  • Posts: 1

  • Since: 2010/3/3 1


Brilliant!! Works great. I'm going to have to come up with something more challenging..... How about this. I have a DB with some user names in it and a password field but no data in it. I want the user to enter their name and then create a password. Using the $_GET method I want the script to search DB for user name and if it finds the name, to insert password in that row. I tried this but keep getting error message.

$pass =$_GET['password'];
$name =$_GET['name'];

$sql = "INSERT INTO pass (password) VALUES ('".$pass."')
WHERE name = ('".$name."')";

Script does open DB

What am I doing wrong here?

Thanks!

7
culex
Re: Counting iterations
  • 2010/3/5 19:31

  • culex

  • Module Developer

  • Posts: 711

  • Since: 2004/9/23


Hmmm a number of things. Xoops uses it's own classes for dealing with queries, and this way is ignoring these. Also you're not sanitizing your sql input. intval(), addslashes, stripslahes() etc. When you put something from $_get, $_post directly into your queries you're inviting someone to try posting bad things to your database. But ok. here goes. To continue your script this is how.
$pass addSlashes($_GET['pass']);
$name addSlashes($_GET['name']);
$db mysql_connect("localhost","username","sqlpassword");
if (!
$db)
  {
  die(
'Could not connect: ' mysql_error());
  }

$sql "INSERT INTO pass (password) VALUES ('$pass') WHERE name = ('$name')";
if (!
mysql_query($sql,$db))
  {
  die(
'Error: ' mysql_error());
  }
echo 
"Success";
mysql_close($db);
?>
This is better though
global $xoopsDB;
 
$myts =& MyTextSanitizer::getInstance(); 
    
$pass $myts->addSlashes($_GET['pass']);
    
$name $myts->addSlashes($_GET['name']);

     
$sql "INSERT INTO ".$xoopsDB->prefix('pass')." VALUES ('$pass') WHERE name ='$name'";
     
$result $xoopsDB->queryF($sql);
?>

Login

Who's Online

336 user(s) are online (255 user(s) are browsing Support Forums)


Members: 0


Guests: 336


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits