Hey,
Hi guys (First post), how're ya'll?
What I'm trying to do is to stack the 'items' that are inside the 'container' getting opened, so I can then make the user get the right quantity of items.
This is also known as a "loot" function (Web-based games FTW)
Anyways, my logic is become flawed as I've been a little sleep deprived lately, so I was wondering if you guys could help me out.
These are my two attempts, and what was wrong with them:
Attempt #1 - 'For each item in the container...'
PHP Code:
// For each item inside the container...
for($i = 0; $i < count($containersItemIDs); $i++) {
if($containersItemIDs[$i] != "") {
// Check if the selected item is the same as the previous item
if($containersItemIDs[$i] == $containersItemIDs[($i-1)]) {
// It is, so add 1 to the quantity of the previous item inside the new array
$containersItemOccurrences[($i-1)]['quantity']++;
} else {
// It isn't, so set a row in the new array, with the ID and inital quantity.
$containersItemOccurrences[$i] = array('itemID' => $containersItemIDs[$i], 'quantity' => 1);
}
}
}
Attempt #2 - 'For each item, count if it's in the string'
PHP Code:
// While there's a result for the query...
while($row = mysql_fetch_assoc(mysql_query("SELECT `id` FROM `objects_items`"))) {
// Count how many there are of that item inside the container
$subStringCount = substr_count($containerDetails['itemids'], "$row");
// And if that number is higher than zero...
if($subStringCount > 0) {
// Set the number of occurrences (according to item id), to the result of the substr_count()
$containersItemOccurrences[$row] = $subStringCount;
}
}
Problem with #1: It went fine with two items, but that's the highest the quantity value ever went.
Problem with #2: Still trying to debug, the AJAX doesn't go through (which means the codes stuck in a loop?), and the MySQL server hangs (thereby using 90% of my CPU until I reset it).
Any solutions for a 15 year old newbie?
(Some what of a newbie, I've only been doing it for 2 years)
~Tim
P.S - Excuse the indenting of the code, the forum converted my tab-spaces into normal spaces or whatever they're called.
P.P.S - I'm dead tired (It's 3:30am), so excuse my logic, and perhaps newbishness