Jump to content
MakeWebGames

Recommended Posts

Posted

A friend of mine coded this for me and i dont know if it right.

 

{
$iq=$db->query("SELECT * FROM items WHERE itmbuyable=1 ORDER BY rand() LIMIT 1",$c);
$r=mysql_fetch_array($iq);
$item=$r['itmid'];
$userid=$ir['userid'];
$db->query("INSERT INTO inventory VALUES ('', $item, $userid, 1)",$c);
event_add($userid,"You found a {$r['itmname']} While Walking Around the city.",$c);
}

 

Users have been geting items that dont exist.

Posted

Re: Finding Items in the city

Have you deleted items in the past? If so, then users are getting an 'itmid' that is no longer valid.

Example,

ITEM ID

1 - GUN

2 - ARMOUR

3 - DRUGS

4 - CAT

5 - HAT

You decide you don't want cats in the game, delete itemid 4, now itemid 4 is blank, so rand(['itmid'] can include items that do not exist any longer.

Solution? TRUNCATE your table and reinsert all items, otherwise create items in deleted itemid's. Or.. use killah's script??

Posted

Re: Finding Items in the city

You can try this:

 

$rand = rand(1,5); //Rand to check if he get's the item or not.
if($rand == 3) //Now if it is rand 3 continue
{
$data = mysql_query("SELECT itmid,itmname FROM items WHERE itmbuyable=1 ORDER BY itmid rand() LIMIT 1"); //Query the item
$soc = mysql_fetch_assoc($data); //Fetch variable $data
$item = $soc['itmid']; //Item ID

//Check if item exists and etc
if(!$item) //Give false event
{
	event_add($ir['userid'],"You thought you saw something lying on the ground while walking the city, but it was nothing."); //Event false event
}
else //Else give item
{
	//Now we check if the user has already got this item.
	$while = mysql_query("SELECT * FROM inventory WHERE inv_userid=".$ir['userid']." AND inv_itmid=".$item);
	if(mysql_num_rows($while) > 0) //If he has the item just plus up the qty
	{
		mysql_query("UPDATE inventory SET inv_qty=inv_qty+1 WHERE inv_userid=".$ir['userid']." AND inv_itmid=".$item); //Plusing the QTY
	}
	else //Else insert the item
	{
		mysql_query("INSERT INTO inventory (inv_itmid,inv_userid,inv_qty) VALUES(".$item.",".$ir['userid'].",1)"); //Inserting the item
	}
	event_add($ir['userid'],"You found ".$soc['itmname']." while walking the street\'s"); //Event user that he got item name blah.
}
}

 

UNTESTED!

Guest Anonymous
Posted

Re: Finding Items in the city

Just what is the point of SELECT * FROM inventory WHERE ... here ?

Totally unnecessary IMO

Guest Anonymous
Posted

Re: Finding Items in the city

 

Nyna, did you read over the code? It check's if the user has the item, and if he does then it add's up if not it inserts.

Did you read my question?

For reference, in case your scroll bar is faulty...

Just what is the point of SELECT * FROM inventory WHERE ... here ?

 

whats IMO?

In My Opinion

I'd use IMHO (In My Humble Opinion) but I'm not feeling humble.

Posted

Re: Finding Items in the city

The use of that query:

 

$while = mysql_query("SELECT * FROM inventory WHERE inv_userid=".$ir['userid']." AND inv_itmid=".$item);

 

It is selecting from inventory where inv_userid is my id & itm id is the item.

If the user has the item, we adjust the inv_qty=inv_qty+1 ELSE it inserts into the inventory.

Does that answer your question?

Guest Anonymous
Posted

Re: Finding Items in the city

Nope...

Why are you running it?

It's redundant.

Posted

Re: Finding Items in the city

Mmmm, i never knew the item_add() function check's if the user has the item or not. My mistake.

 

$rand = rand(1,5); //Rand to check if he get's the item or not.
if($rand == 3) //Now if it is rand 3 continue
{
$data = mysql_query("SELECT itmid,itmname FROM items WHERE itmbuyable=1 ORDER BY itmid rand() LIMIT 1"); //Query the item
$soc = mysql_fetch_assoc($data); //Fetch variable $data
$item = $soc['itmid']; //Item ID
//Check if item exists and etc
if(!$item) //Give false event
{
	event_add($ir['userid'],"You thought you saw something lying on the ground while walking the city, but it was nothing."); //Event false event
}
else //Else give item
{
	//Credit item and event user.
	item_add($ir['userid'],$item,1);
	event_add($ir['userid'],"You found ".$soc['itmname']." item while walking the city"); //Event user that he got item name blah.
}
}
Posted

Re: Finding Items in the city

 

Mmmm, i never knew the item_add() function check's if the user has the item or not. My mistake.

 

$rand = rand(1,5); //Rand to check if he get's the item or not.
if($rand == 3) //Now if it is rand 3 continue
{
$data = mysql_query("SELECT itmid,itmname FROM items WHERE itmbuyable=1 ORDER BY itmid rand() LIMIT 1"); //Query the item
$soc = mysql_fetch_assoc($data); //Fetch variable $data
$item = $soc['itmid']; //Item ID
//Check if item exists and etc
if(!$item) //Give false event
{
	event_add($ir['userid'],"You thought you saw something lying on the ground while walking the city, but it was nothing."); //Event false event
}
else //Else give item
{
	//Credit item and event user.
	item_add($ir['userid'],$item,1);
	event_add($ir['userid'],"You found ".$soc['itmname']." item while walking the city"); //Event user that he got item name blah.
}
}

 

learn something new everyday mate :-D

Posted

Re: Finding Items in the city

 

Well ok, i really do not work on the global_func.php file. I work on my own coding. As i know it should work good.

i only know from recoding some of it so i know how ya feel no biggy really mate

  • 2 weeks later...
  • 4 years later...
Posted
So what is this doing exactly?

Looks like this script gives the user an item, based on 1:5 odds, it runs checks to see if the user has the item and gives it to them whether or not the user has the item already.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...