Jump to content

Archived

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

php_b34st

Generating tables with php?

Recommended Posts

Hi I use the following code to retrieve data from mysql an insert it into a table:

[code]$query = "SELECT * FROM stats WHERE userID='$user'";
$result = mysql_query($query) or die("Could not query: " . mysql_error());

$info = array();
$i = 0;

while ($row = mysql_fetch_assoc($result)) {
   
    $info[$i]['cat'] = $row['cat'];
    $info[$i]['statB'] = $row['statB'];
    $i++;
   
}

echo '<table align="center" border="0" cellpadding="2" cellspacing="2" class="forumline">
        <tr>
  <th class="cat" height="28" colspan="4"><span class="cattitle">Personal Scores For ' . $user . '</th>
</tr>
<tr>
            <th class="titlemedium">Skill</th>
            <th class="titlemedium">Level</th>
        </tr>';
$i = 0;
foreach ($info as $r => $value) {

   
    echo '
        <tr>
          <td class="row1"><span class="genmed">' . $info[$r]['cat'] . '</span></td>
          <td class="row1"><span class="genmed">' . $info[$r]['statB'] . '</td>
        </tr>';
 
    $i++;
}
echo '</table></div></div>';[/code]

The script works fine but it produces a long narrow table, is there a way that i can limit the rows to 5 and have a table that is more like this:

[code]<table>
  <tr>
    <td><img src="images/icons/attack.png"></td>
    <td><img src="images/icons/prayer.png"></td>
    <td><img src="images/icons/runecraft.png"></td>
    <td><img src="images/icons/fishing.png"></td>
    <td><img src="images/icons/farming.png"></td>
  </tr>
  <tr>
    <td><img src="images/icons/strength.png"></td>
    <td><img src="images/icons/magic.png"></td>
    <td><img src="images/icons/craft.png"></td>
    <td><img src="images/icons/cooking.png"></td>
    <td><img src="images/icons/construction.png"></td>
  </tr>
  <tr>
    <td><img src="images/icons/defense.png"></td>
    <td><img src="images/icons/agility.png"></td>
    <td><img src="images/icons/fletching.png"></td>
    <td><img src="images/icons/firemaking.png"></td>
  </tr>
  <tr>
    <td><img src="images/icons/hp.png"></td>
    <td><img src="images/icons/herblore.png"></td>
    <td><img src="images/icons/mining.png"></td>
    <td><img src="images/icons/woodcutting.png"></td>
  </tr>
  <tr>
    <td><img src="images/icons/range.png"></td>
    <td><img src="images/icons/thieving.png"></td>
    <td><img src="images/icons/smithing.png"></td>
    <td><img src="images/icons/slayer.png"></td>
  </tr>
</table>[/code]

Thanks in advance

Share this post


Link to post
Share on other sites
[code]<?php
$i = 0;
foreach ($info as $r => $value) {
   if ($i == 0)
       echo "<tr>";
  
    echo '<td class="row1"><span class="genmed">' . $info[$r]['cat'] . '</span></td>
          <td class="row1"><span class="genmed">' . $info[$r]['statB'] . '</td>';
   
    if (++$i == 5)
    {
        $i = 0;
        echo "</tr>";
    }
}
?>[/code]
Try that.

Share this post


Link to post
Share on other sites

×

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.