Jump to content

Need Help with a PHP Loop


mabsey

Recommended Posts

I'm coding an online RPG and I've got stuck at a final routine which I will explain below:

 

Within the Game you have a commander and the commander can have officers. Now the officers can also have officers so I store these in a MYSQL table in the format:

 

ID - Is the ID of the Player

Commander - Is the commander of the player 0 = No Commander

Status - 1 = Active  2 = Banned  3=Deleted

 

Now the top level commander can have an alliance. Lets say the commander calls his alliance "Alliance1"

 

I need a routine that will set everyone below him to "Alliance1"

 

So every officer and sub officer of the officer needs to get this alliance tag.

 

So I need a PHP Script to loop through everyone under the commander and set an alliance TAG to "Alliance1" and I am completly stumped how to do this.

 

I've looked into for each loops but going down multi levels is quite hard for me.

 

Any help or pointers really appreciated.

 

Thanks

 

Mabs

 

 

Link to comment
Share on other sites

It's all in the same table, isn't it? Shouldn't be too hard to get it working. Create a column in your table and call it "alliance" or whatever you want. When the commander makes his alliance and sets his alliance name "A1" it will be set in the database. Whenever someone joins his alliance, they will get the same set value for their alliance. If they leave, you can update it with a blank alliance or however you want. That's just the basic, what you're needing?

Link to comment
Share on other sites

<?php
$commandID //Set to the ID of the Commander

$query = "SELECT `ID` FROM `table` WHERE `Commander`='$commandID'";
$sql = mysql_query($query) or die("<pre>MySQL Error: ".mysql_error()."\nQuery: ".$query."</pre>");

while($row = mysql_fetch_array($sql) {
     $query = "UPDATE `table` SET `tag`='Alliance1' WHERE `ID`='".$row['ID']."' LIMIT 1"
     mysql_query($query) or die("<pre>MySQL Error: ".mysql_error()."\nQuery: ".$query."</pre>");
}
?>

 

That's all I can gather from what your said.

Link to comment
Share on other sites

Thanks for the replies so far, I will explain a little bit more

 

COMMANDER - Joins the game.

COMMANDER - Gets 5 officers under him called officer1 to officer5

 

OFFICER1 - Gets 2 Officers under him

OFFICER2 - Gets 3 Officers under him

 

So now the chain looks like this:

 

COMMANDER1

  OFFICER1

      OFFICER1

      OFFICER2

      OFFICER3

    OFFICER1

      OFFICER1

      OFFICER2

 

So on the example above COMMAND1 has two officers and the first has 3 officer himself and the second has 2 officers.

 

Now COMMANDER1 decided that he wants to start an alliance TAG called MyAlliance.

 

Somehow I need a routine that gives COMMANDER1 and every officer and sub officer the TAG MyAlliance.

 

Remember there could be multi officers deep underneath the main commander.

 

So the routine has got to say:

 

Get all officers of the Commander and give them the Alliance Tag MyAlliance

Get all the officers of the officers and give them the Tag MyAllliance

and so on and so on until every officer or sub officer has the TAG

 

This is why I'm finding it difficult. Drilling down multi levels stumping me.

 

Mabsey

 

Link to comment
Share on other sites

I have solved my problem so thanks everyone for looking. Basically I need to run a cron job because the routine is quite intense. The guts of it is:

 

Get the user id of every player in the game

check there commander in a loop and see if they are top level, if they are not then loop to next commander

keep looping until you get to the top and then set the alliance name to whatever the top commander has set

 

then move onto player 2 and do all again, do the above for each player in the game. Its effectively a loop withing a loop.

 

Mabsey

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.