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
https://forums.phpfreaks.com/topic/45226-solved-while-loop-help/
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.

$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>"; }

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.

$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.

  • 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.