Jump to content

Problem with using while loop in function & using that function in for loop


slyte33

Recommended Posts

I have this code:

function bfinds($id, &$db)
{
$query=$db->execute("select * from allpitems");
while($item = $query->fetchrow())
{
$rand = mt_rand(1, $item[itemfind]);

if ($rand == $item[itemfind])
{
echo "You found $item[name]<br><br>";
}else if ($rand != $item[itemfind]){
echo "You did not find anything<br><br>";
}


}
}
$times = $_POST[turns];
for($i=1; $i<=$times; $i++)
{
echo "#$i: ";
bfinds($player->id, $db);
}

 

What it does is select a number for the all_items table, and it generates a random number 1-$item[itemfind]

 

if $rand = $item[itemfind] display that you got that item.

 

The problem is this:

 

Lets say I had 3 items in the all_items table(even though I have like 200, but 3 for example)

 

For loop:

#1: You found [itemnamehere]

You found [itemnamehere]

You found [itemnamehere]

 

#2: You found [itemnamehere]

You found [itemnamehere]

You found [itemnamehere]

 

As you can see, for each $i that is shown, it generates the bfinds function X times(X being the total row count of items in the table all_items).

What I want is for if someone entered 3 in the text box named turns, display:

 

#1: You found [itemnamehere]

#2: You found [itemnamehere]

#3: You found [itemnamehere]

 

I hope I explained this well

All feedback is greatly appreciated, thanks :)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.