Skip to content
Advertisement

Need to this table with 100 rows and assign a valor to the code PHP

I need to solve this simple thing: Got this code:

<?php
$xml=simplexml_load_file("d2ladder.xml") or die("Error: Cannot create object");
?>    
<table width="100%" border="1" cellspacing="0" cellpadding="2" align="center" style="text-align:center">
  <tbody>
    <tr class="<?php if ($xml->ladder[12]->char[0]->status == "alive") { echo "alive"; } else { echo "dead"; } ?>">
      <td width="10%">1.</td>
      <td width="50%" style="text-align:left"><?php echo $xml->ladder[12]->char[0]->prefix; ?> <?php echo $xml->ladder[12]->char[0]->name; ?></td>
      <td width="10%"><?php echo $xml->ladder[12]->char[0]->class; ?></td>
      <td width="10%"><?php echo $xml->ladder[12]->char[0]->level; ?></td>
      <td width="20%"><?php echo $xml->ladder[12]->char[0]->experience; ?></td>
    </tr>
        <tr class="<?php if ($xml->ladder[12]->char[1]->status == "alive") { echo "alive"; } else { echo "dead"; } ?>">
      <td width="10%">2.</td>
      <td width="50%" style="text-align:left"><?php echo $xml->ladder[12]->char[1]->prefix; ?> <?php echo $xml->ladder[12]->char[1]->name; ?></td>
      <td width="10%"><?php echo $xml->ladder[12]->char[1]->class; ?></td>
      <td width="10%"><?php echo $xml->ladder[12]->char[1]->level; ?></td>
      <td width="20%"><?php echo $xml->ladder[12]->char[1]->experience; ?></td>
    </tr>

  </tbody>
</table>

Need to create rows on this table till char[99] Maybe I need to use while? Because sometimes it wont get to 99 on the sitemap

Advertisement

Answer

Use for/while cycle:

for ($i = 0; $i < 100; $i++) {
    ?>
  <tr class="<?php if ($xml->ladder[12]->char[$i]->status == "alive") { echo "alive"; } else { echo "dead"; } ?>">
  <td width="10%"><?php echo $i; ?>.</td>
  <td width="50%" style="text-align:left"><?php echo $xml->ladder[12]->char[$i]->prefix; ?> <?php echo $xml->ladder[12]->char[$i]->name; ?></td>
  <td width="10%"><?php echo $xml->ladder[12]->char[$i]->class; ?></td>
  <td width="10%"><?php echo $xml->ladder[12]->char[$i]->level; ?></td>
  <td width="20%"><?php echo $xml->ladder[12]->char[$i]->experience; ?></td>
</tr>
<?php } 

See for cycle

Hope this helps.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement