Jump to content

[SOLVED] while loop help


mike1313

Recommended Posts

Okay so for my MMORPG, there are player-owned guilds that have an armory where they can store their weps/armor but when it shows what the armory holds if it is more than one of the same item they stack instead of showing

 

Sword

Sword

 

I want it to display 2 Sword. How can I do this?

 

while ($armo = mysql_fetch_array($arm)) {
       if($armo[type] == "Weapon"){ $t = "W";}
       if($armo[type] == "Armor"){ $t = "A";}
      echo "<tr><td><li type=square></td><td>[<b>$t</b>]</td><td>$armo[item]</td><td> [<b>E</b>]:  $armo[effect]</td></tr>"; }

Link to comment
Share on other sites

Need more code man, what is the query you are running? Does it return a quantity if you know the quantity you want to do something like this:

 

while ($armo = mysql_fetch_array($arm)) {
       $i=0;
       while ($armo['quantity'] < $i) {
           if($armo[type] == "Weapon"){ $t = "W";}
           if($armo[type] == "Armor"){ $t = "A";}
           echo "<tr><td><li type=square></td><td>[<b>$t</b>]</td><td>$armo[item]</td><td> [<b>E</b>]:  $armo[effect]</td></tr>"; 
            $i++;
        }
}

 

Note that will only work if quantity is actually in the DB, post more code if you want a more accurate response.

Link to comment
Share on other sites

$arm = mysql_query("SELECT * FROM order_arm WHERE `order`='$order[id]' ORDER by effect asc");
      while ($armo = mysql_fetch_array($arm)) {
       if($armo[type] == "Weapon"){ $t = "W";}
       if($armo[type] == "Armor"){ $t = "A";}
      echo "<tr><td><li type=square></td><td>[<b>$t</b>]</td><td>$armo[item]</td><td> [<b>E</b>]:  $armo[effect]</td></tr>"; }

Link to comment
Share on other sites

Can you post your DB structure dude. It is hard to diagnose the problem when you are not giving us enough information. Its like me telling you to find out google's search engine secret, impossible without some type of access to the code.

Link to comment
Share on other sites

If you want the fields they are as such for the db table

 

id - auto_increment

order (int)

item (varchar)

type(varchar)

effect(int)

equipped(varchar)

req_lvl (int)

 

Each time someone puts something into the armory it makes a new row.

Link to comment
Share on other sites

So you only want it to echo sword once, that is simple my friend.

 

$arm = mysql_query("SELECT * FROM order_arm WHERE `order`='$order[id]' GROUP BY type ORDER by effect asc");
      while ($armo = mysql_fetch_array($arm)) {
       if($armo[type] == "Weapon"){ $t = "W";}
       if($armo[type] == "Armor"){ $t = "A";}
      echo "<tr><td><li type=square></td><td>[<b>$t</b>]</td><td>$armo[item]</td><td> [<b>E</b>]:  $armo[effect]</td></tr>"; }

 

Try that out. Added a GROUP BY statement.

Link to comment
Share on other sites

$arm = mysql_query("SELECT * FROM order_arm WHERE `order`='$order[id]' ORDER by effect asc");
      while ($armo = mysql_fetch_array($arm)) {
          if (in_array($armo['item'], $armArr)) {
                $armArr[$armo['item']]['quantity']++;
          }else {
                $armArr[$armo['item']] = $armo; 
                 $armArr[$armo['item']]['quantity'] = 1; 
          }
}

foreach ($armArr as $armo) {
         if($armo[type] == "Weapon"){ $t = "W";}
         if($armo[type] == "Armor"){ $t = "A";}
         echo "<tr><td><li type=square></td><td>".$armo['quantity']."[<b>$t</b>]</td><td>$armo[item]</td><td> [<b>E</b>]:  $armo[effect]</td></tr>"; 
}

 

something along those lines.

Link to comment
Share on other sites

  • 8 months later...

I've tried using that over to count the number but it's not working it's just running the

else {
                $armArr[$armo['item']] = $armo; 
                 $armArr[$armo['item']]['quantity'] = 1; 
          }

 

the code again

 

$arm = mysql_query("SELECT * FROM order_arm WHERE `order`='$order[id]' ORDER by effect asc");
      while ($armo = mysql_fetch_array($arm)) {
          if (in_array($armo['item'], $armArr)) {
                $armArr[$armo['item']]['quantity']++;
          }else {
                $armArr[$armo['item']] = $armo; 
                 $armArr[$armo['item']]['quantity'] = 1; 
          }
}

foreach ($armArr as $armo) {
         if($armo[type] == "Weapon"){ $t = "W";}
         if($armo[type] == "Armor"){ $t = "A";}
         echo "<tr><td><li type=square></td><td>".$armo['quantity']."[<b>$t</b>]</td><td>$armo[item]</td><td> [<b>E</b>]:  $armo[effect]</td></tr>"; 
}

I've tested it using 2 items of the same thing and it only displays 1 instead of 2.

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.