Stacking Items [Resolved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Stacking Items [Resolved]

Post by Epiales »

Heya all! Another question here...

Instead of the inventory listing one thing 10 times, how can you make it where it shows one thing, but stacks the other 9?

Thx
Last edited by Epiales on Sun Sep 08, 2013 10:28 am, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Stacking Items

Post by MikuzA »

I would suggest adding quantity to database and alter code to play with them.

If that's not ok,
When querying the inventory, sort it. Then in the fetch add just a row calculations, like

Code: Select all

//Define your preset variables which are used in the while.
$cur_item = ""; //item name // or ID.
$cur_item_cnt = 1; //Quantity counter
$first_row = 1; //Avoids empty qty print

while (row = mysql_fetch_assoc($mysql_query)) { //loop starts here
  IF($cur_item == $row['itemname']) { // If the current item variable is the same as the current looped row, if yes, it adds +1 to the calc.
      $cur_item_cnt++; //+1 to calc.
   } else {
      if($first_row == 1) { // checks if it's the first row of the query, ignores printout.
            $first_row = 0; //change variable to show to next loop that this is not first_row anymore.
      } else {
            print $cur_item . " - Qty: " . $cur_item_cnt . "</br>";  //when result shows item change, prints out the item name and quantity.
      }
      $cur_item = $row['itemname']; //defines the current item with the row_result itemname.
      $cur_item_cnt = 1; // resets the item quantity counter.
   }
} //loop ends here
  print $cur_item . " - Qty: " . $cur_item_cnt . "</br>"; // prints out the last line which was stored in the variables during item change. due while-loop stops there.
 

NOTE: I did NOT test the above code, should be only used as example of logic that could be done.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stacking Items

Post by Epiales »

Okay, this is what I've written. I used partial of your explanation above. Thank you. Now it's not there yet, but close. I have it where it shows the items that have more than one of them, but it doesn't show how many of them.

EX: If you have 4 guns and 3 knives, and 1 crutch, it will show the guns and knives, but not the crutch, as the crutch only has one. But it's not giving the item count of the knives or the guns. Make sense? Here's the code:

Code: Select all

$invinfo = ("SELECT count(*), name FROM inventory GROUP BY name having count(*) > 1 ");
$invinfo2 = mysql_query($invinfo) or die ("Could not select inventory");
$row = mysql_fetch_array($invinfo2);
$cur_item = $row['name'];
$cur_item_cnt = 1;

while ($row = mysql_fetch_assoc($invinfo2)) {
if ($cur_item = $row['name']) {
    $cur_item_cnt++;    
}
}
 print $cur_item . " - Qty: " . $cur_item_cnt . "</br>";
?>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

Re: Stacking Items

Post by a_bertrand »

Just be careful with stacks if you add more attributes to the item, for example how worn it is, or whatever else. Make sure only items which are 100% the same stack!
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stacking Items

Post by Epiales »

a_bertrand wrote:Just be careful with stacks if you add more attributes to the item, for example how worn it is, or whatever else. Make sure only items which are 100% the same stack!
Yeah, that's what I'm trying to do. I'm using the name of the item, as there will only be one name for each item. But this is driving me mad yet again LOL. Thx for pointing it out though. I'm sure it's so simple, but I make it difficult grrrr.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stacking Items

Post by Epiales »

Okay, slowly getting there. Now I have it listing one item and how many that item has if it's over 1. Just need to figure out the loop. Code Below:

Code: Select all

<?php
$invinfo = ("SELECT count(*) as total, name FROM inventory GROUP BY name having count(*) > 1 ");
$invinfo2 = mysql_query($invinfo) or die ("Could not select inventory");
$row = mysql_fetch_array($invinfo2);
$num_rows = mysql_num_rows($invinfo2);
$cur_item = $row['name'];
$cur_item_cnt = 1;

 print $cur_item . " - Qty: " . $row['total'] . "</br>";

}
?>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stacking Items

Post by Epiales »

And here's the final result YAY

Code: Select all

<?php
$invinfo = ("SELECT count(*) as total, name FROM inventory GROUP BY name having count(*) > 1 ");
$invinfo2 = mysql_query($invinfo) or die ("Could not select inventory");
while ($row = mysql_fetch_assoc($invinfo2)) {
$cur_item = $row['name'];

print $cur_item . " - Qty: " . $row['total'] . "</br>";
    }
}
?>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stacking Items

Post by Epiales »

GRRR, now I can't get them inside a table in order have them viewed and used. :lol: :lol:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Stacking Items

Post by Epiales »

Okay, for those that wanted to stack their items, here's the code that you can use in the script. It's not inside a table or anything, but you can work that out :D

Code: Select all

<?php
$id = $user['id']; 
$counter = 0;
$invinfo = ("SELECT count(*) as total, name, stats, price, statadd, randid, type FROM inventory GROUP BY name having count(*) > 0 ");
$invinfo2 = mysql_query($invinfo) or die ("Could not select inventory");
while ($row = mysql_fetch_assoc($invinfo2)) {
$cur_item = $row['name'];

 if($row['type'] == 'healing' || $row['type'] == 'revive' )
      {
print $cur_item . " - Qty: " . $row['total'] . " <A href='battle1.php?randid=$row[randid]'><font color='lime'>Use Item</a></font></td></br><br><br>";

       $counter = 1;

    }
}

 if ($counter == 0)
      {
            echo "<center>You have Nothing in your inventory!<br>";
            echo "<a href='index.php'><font color='lime'>Go Back</font></center>";
            exit;
      }

                echo "<a href='battle1.php'><font color='lime'>Go Back To Battle</font></center></a>";

 }   
    ?>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Stacking Items [Resolved]

Post by MikuzA »

Good job! Never thought about actually enhancing the mysql-statement there, good pointer!
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
Post Reply

Return to “Beginner Help and Support”