Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help: Stacking "items" from inside a "container"
Old 01-03-2009, 10:29 AM Help: Stacking "items" from inside a "container"
Novice Talker

Posts: 10
Name: Tim
Trades: 0
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
Crystalmyst is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-03-2009, 10:46 AM Re: Help: Stacking "items" from inside a "container"
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
I'd say the 1st attempt fails because you're using $i-1 which would be -1 when $i = 0, so it tries to reach an undefined index?

The 2nd attempt fails because you're executing the query over and over again.
You must bear in mind that the while will keep executing $row = mysql_fetch_assoc(mysql_query("SELECT `id` FROM `objects_items`")) over and over again until it returns false.
As long as there is at least one result, it will never return false, though, because you're creating a new query on each call.
You should get the query out of the statement and assign it to a variable.
Then you can use mysql_fetch_assoc on the variable.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-03-2009, 10:55 AM Re: Help: Stacking "items" from inside a "container"
Novice Talker

Posts: 10
Name: Tim
Trades: 0
The first attempt isn't because of that (It'd just return a warning, nothing fatal, although it is bad style)

As for the second attempt:
Oh right, I never really knew much about that syntax for while(), so thanks for telling me that. I'll give it a shot...

~Tim

EDIT: Okay, that solved the problem, and now I'm just trudging through the debugging.
As they say: "1 hour of writing code == 8 days of squashing bugs".

Last edited by Crystalmyst; 01-03-2009 at 11:07 AM..
Crystalmyst is offline
Reply With Quote
View Public Profile
 
Old 01-04-2009, 05:57 AM Re: Help: Stacking "items" from inside a "container"
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
So true...
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help: Stacking "items" from inside a "container"
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.88684 seconds with 12 queries