Jump to content

Generating tables with php?


php_b34st

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
Link to comment
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.
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.