0

So I want to create a script where out of about ten mysql functions that give a user a different item, whenever the user clicks on the page, one of those functions is executed at random. It's kind of an rpg function I need, where depending on the user's level with hunting, they are more successful, but won't always get food. So they click on the page and from an array of items, one is at random returned and that item given to them via a mysql_query.

I hope that makes sense, if not, let me know...

so far I've got these starting points

$input = array("value 1", "value 2", "value 3", "value 4", "value 5");
 $rand_keys = array_rand($input, 1);
 echo $input[$rand_keys[0]] . "\n";

I don't know if this would allow mysql queries in the array though. I've also thought about using a redirect thing, but the one I tried kept redirecting, which was not what i wanted, and urls can be tampered to get the good result with that...

Anyway, I hope that made sense and any help would be greatly appreciated :)

1 Answer 1

1

I would tackle this challenge by having a table with the items and it's values. One of the columns would be prio_drop. If you want a certain item to be dropped more often it can have value 2 while the others have value 1. The higher value, the greater chance.

Then I'd do a:

$arr_items = array(1, 2, 3, 5); /* This event can drop items with these ids */
$str_items = implode(', ', $arr_items);

mysql_query("
SELECT id, name
FROM items
WHERE id IN ({$str_items})
ORDER BY (RAND() * prio_drop) DESC
LIMIT 1
");

Hope I understood you correctly?

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, this is exactly what I need! I'll try it when everything else is finalised around it, but is there any way I could also include the possibility of it returning nothing, so that the person using it won't always get something?
I guess you could have a joker item which gives nothing if chosen, and have an extra column which says if it's Joker or not. Else you should simply do a if (rand(1, 10) <= 8) return; in php and skip running the query at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.