Need just a tiny bit of help =]

salukigirl

New Member
Messages
78
Reaction score
0
Points
0
So this is pretty simple. I want to randomize input.

I'm making a pet game, and in this game animals have "stats" and when you buy an animal I want those stats to be randomized so you don't know what you are going to get.

These would be from 1-10

If that didn't make sense I'll try to explain it a bit more.

I have the ears stat and everything in the animal table in the database, but I want to randomize what each animal's actually stats are via the pet shop. This is what I have so far...

PHP:
<?php
include ('connect.php');
?>

All rats costs 3000 RTD.


<?php
error_reporting(E_ALL^E_NOTICE);
?>


<form name="form 1" method="post" action="breeder.php">
Rat Name:
<input name="petname" type="text">
<br>
Rat Gender:
<select name="petgender">
<option value="Buck">Buck</option>
<option value="Doe">Doe</option>
</select>
<br>
Rat Image:
<input name="image" type="text"> From <a href=imagebank.php>Image Bank?</a>
<br>
<input type="submit" name="Submit" value="Buy Rat">
</form>

<?php
$name = trim($_POST['name']);
$color= trim($_POST['color']);
$gender = trim($_POST['gender']);
$image = trim($_POST['image']);
?>




<?php
$ownerid = $_SESSION['id']
?>


<?php
$pet = @mysql_query("INSERT INTO rats (owner, image, name, color, gender) VALUES ('$owner','$image','$name','$color','$gender')") or die ( 'error' . @mysql_error());

$result = @mysql_query("UPDATE players SET money=money-3000 WHERE id='$id'") or die ('could not update pets name');
echo "<br>Animal Created!<BR><BR>";

?>


Note I haven't put in a place where it inserts the stats into the rats table because I'm not sure how to do that.

Any help is greatly appreciated. ;)
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Random numbers can be generated with mt_rand(min, max). So to get a random number from 1 to 10, you'd use mt_rand(1, 10). However, this function doesn't take into account previous calls to it. So if you use it 5 times with 1 and 10 as the arguments, it's possible that each time it will return 1, or even 10.

If you want to avoid this, you'd have to add logic to consider the previous values and determine an appropriate min and max based on them. Something like:

PHP:
$min = 1; $max = 10;
$x = mt_rand($min, $max);
$min += ($x == 1 ? 1 : 0);
$max -= ($x == 10 ? 1 : 0);
$y = mt_rand($min, $max);
//and so on

Of course you'd also need to ensure that $min and $max don't go beyond certain amounts. It'd probably be better to create a function to handle this, rather than having to repeat similar code.
 

salukigirl

New Member
Messages
78
Reaction score
0
Points
0
I want it to be completely random, no matter what was before it,

but then how do I get the randomized stat inserted into the table in the right spot?
 

mattura

Member
Messages
570
Reaction score
2
Points
18
Choose you data structures carefully!
I would have a table with fields:
id,animal,owner,name,colour,gender,image,ears,head,body,tail
example values:
1,rat,123,dennis,green,male,rat1.jpg,4,8,6,6
(if you want to store info about the owner, another table provides the owner id)

With the above in mind:
PHP:
//assume you have clean POST values $name, $colour etc
$ears=mt_rand(1,10);
$head=mt_rand(1,10);
$body=mt_rand(1,10);
$tail=mt_rand(1,10);
$query="INSERT INTO $db (`animal`,`owner`,`name`,`colour`,`gender`,`image`,`ears`,`head`,`body`,`tail`)"; 
$query.="VALUES ('$animal','$ownerid','$name','$colour','$gender','$img','$ears','$head','$body','$tail');";
if (!@mysql_query($query)) {echo "Error inserting values";}
else {
 echo "Animal created!";
}
Edit:
Alternatively, you could have 1 field for holding all the stats
Say you have an array of stats in php:
PHP:
$stats=Array(5,8,6,9);  //you could use an associated array for clarity here
$insert=implode(",",$stats); //converts into comma separated string
$query="... (`stats`...) VALUES ('$insert' ..."; //as before, but with`stats`

//obtain the values for the stats:
$query="SELECT * FROM $db WHERE `owner`='$ownerid'";  //retrieve owner's pet
$result=mysql_query($query);
$row=mysql_fetch_assoc($result);
$stats=explode(",",$row['stats']);
print_r($stats);
//$stats now looks like this: Array(5,8,6,9)

//not that explode and implode should be used with separators which can never be part 
//of the data (eg if a 'stat' included a comma, this would mess up the array)
 
Last edited:
Top