Page 1 of 1

php strings

Posted: Sun Nov 21, 2010 5:03 pm
by alexander19
Hello everyone!

Been working on an inventory system which stores all items in a string:
E.g:

Code: Select all

$inventory = "Sword,Potion,Sword,Dagger";
The thing is that I can manipulate the string by using explode and foreach for adding extra items to it but the problem comes when I want to delete an item, especially one that appears multiple times(like the Sword in the above example).
I tried str_replace,but it replaced all items of the same kind :

Code: Select all

$item = "Sword";
$string = str_replace("$item", "", $inventory);
My question is,how can I replace only one instance of a string that appears multiple times?

Re: php strings

Posted: Sun Nov 21, 2010 9:59 pm
by Chris
If you looked at the str_replace documentation you'd find out:
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

Code: Select all

$string = str_replace("$item", "", $inventory, 1);
 

Re: php strings

Posted: Sun Nov 21, 2010 10:04 pm
by alexander19
damn didnt checked that...thx a lot :D

Re: php strings

Posted: Sun Nov 21, 2010 11:10 pm
by Baseball435
what i did for my inventory is that when someone bought something from the shop it would insert into the table 'inventory' and it the values in the table would be owner, name, strbuff, defbuff, sneakbuff, agilitybuff, consumable, equipable, equiped and then when they bought it i would insert there name as the owner and then have the name as the item name and then i would fill in the rest. Then for the inventory part i just had a query that said SELECT * FROM inventory WHERE owner = '$username' and then i had it as a while loop so that it would post every thing that they had in their inventory. It works really good too

Re: php strings

Posted: Mon Nov 22, 2010 8:40 am
by alexander19
Well I have a drag & drop inventory made in jquery UI,and the inventory database table is actualy made from 2 parts...the tabs with the items you currently have equipped , which stores 1 item each and the normal inventory which stores 18 items,so rather than creating a new table with 18 tabs I've created only a tab that holds a string with all the items.

Here is a screen:
Image