Counting an Array [Solved]

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
rapid3642
Posts: 45
Joined: Thu Nov 26, 2009 9:38 pm

Counting an Array [Solved]

Post by rapid3642 »

Hey guys I am building a game based off the MMO Tutorial Series and i am currently trying to count an array for my quest system. It's kinda hard to explain so i'll give an example.

when a player accepts a quest, it will then be stored into the players quest log, when reviewing his quest log he will see the requirements needed in order to finish the quest, Like This.

Orc Tooth
0/3

Goblin Mail
0/3

Now the setup i have for my database is in the Image Files i'm really sorry the Table is to big to fit into one picture. 3 is the maximum attachments allowed so i will post a reply with the remaining photos.
i do not know how to really make an array to say how many items they have, this is what i tried and it failed horribly but it displays a 2 which i'm sure is just how many are in the array but its not accurate, because i actually have 4 in my inventory.

This is the Code i have for it to query and put it into an array to be counted, i'm sorry i'm not even sure if i'm on the right track or not. For some reason it will only display if i say $req1 !=='0') its weird and help on that would be appreciated.

Code: Select all

 if($req1 !== '0')
	 {
	  $selectinventory="SELECT name from playerinventory where pid = '$userid' AND name='$selectquest3[req1]'";
      $selectinventory2=mysql_query($selectinventory) or die("Could not retrieve inventory Count.");
	  $selectinventory3=mysql_fetch_array($selectinventory2);
	  $icount1 = count($selectinventory3);
	  
	 print"<br>
	 $selectpquest3[req1]<br>" . count($selectinventory3) . " / $selectpquest3[req1amount]<br><br>";
	 }
If any other information is needed let me know i wouldn't mind at all giving any more information.

thanks everyone who looked at this and tried to help!
Last edited by rapid3642 on Mon Dec 27, 2010 8:24 pm, edited 2 times in total.
rapid3642
Posts: 45
Joined: Thu Nov 26, 2009 9:38 pm

Re: Counting an Array

Post by rapid3642 »

The Remaining Pictures
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Counting an Array

Post by Callan S. »

I'm not really understanding how your storing items? Atleast not from a glance through?
rapid3642
Posts: 45
Joined: Thu Nov 26, 2009 9:38 pm

Re: Counting an Array

Post by rapid3642 »

Sorry for the confusion.

All Items are stored in the playerinventory table, I attached a image showing the table. the id is auto inc, and the pid id matches the players id so that way when i query something by just saying select * from playerinventory where id='$userid'";

The $userid is the player session so thats how i am able to call for it in my query.

If your still confused please reply and tell me and i will go more in depth about it.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Counting an Array

Post by hallsofvallhalla »

why i hate count

insert this instead to to debug whats going on.

Code: Select all

 $iteminfo="SELECT * from playerinventory where pid = '$userid' AND name='$selectquest3[req1]'";
$iteminfo2=mysql_query($iteminfo) or die("Could not get user items");
 while($iteminfo3=mysql_fetch_array($iteminfo2))
 {
 $itemcount = $itemcount + 1;
echo  $itemcount . " " . $iteminfo3['name'] . "<br>";
 }
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Re: Counting an Array

Post by alexander19 »

I've came up with this...let me know if it works.

Code: Select all

if($req1 !== '')
    {
     $selectinventory="SELECT COUNT(name) from playerinventory where pid = '$userid' AND name='$selectquest3[req1]'";
      $selectinventory2=mysql_query($selectinventory) or die("Could not retrieve inventory Count.");
     while($selectinventory3=mysql_fetch_array($selectinventory2))
	 {
	echo "You have". $selectinventory3['COUNT(name)'] ." / ".$selectquest3[req1amount]." ".$selectquest3[req1]."";
	echo "<br />";	 
	 }
    }
rapid3642
Posts: 45
Joined: Thu Nov 26, 2009 9:38 pm

Re: Counting an Array

Post by rapid3642 »

alexander19 wrote:I've came up with this...let me know if it works.

Code: Select all

if($req1 !== '')
    {
     $selectinventory="SELECT COUNT(name) from playerinventory where pid = '$userid' AND name='$selectquest3[req1]'";
      $selectinventory2=mysql_query($selectinventory) or die("Could not retrieve inventory Count.");
     while($selectinventory3=mysql_fetch_array($selectinventory2))
	 {
	echo "You have". $selectinventory3['COUNT(name)'] ." / ".$selectquest3[req1amount]." ".$selectquest3[req1]."";
	echo "<br />";	 
	 }
    }
@ alexander19
Thank you! thats so awesome! lol it works great! thats a really good way to use the count command. Is that what you did in your game? if you don't want to answer its fine. but yea thanks again!

@Halls
Sorry i tried alexander19's method real quick to see if it would work and if it didn't then i was gonna debug with the method you gave me. But it all worked out but hey thank you for the reply and it was a good step in the right direction! :)
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Re: Counting an Array [Solved]

Post by alexander19 »

For the combat quests in my game where you have to kill..for instance 10 bandits,I'm just increasing the needed amount to complete the quest,each time you defeat a bandit.
I have 2 tabs for a combat quest...requiredamount(which would be 10 in our case) and totalamount(which would be the total number of bandits we've defeated).
So I dont have to use the count function...just something like:

Code: Select all

echo "You have killed ".$totalamount." / ".$requiredamount." bandits";
But I've used the count functions for other things...like counting the number of online players,or the total members of a guild etc.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Counting an Array [Solved]

Post by hallsofvallhalla »

glad you got it working.
Post Reply

Return to “Advanced Help and Support”