Jump to content


Photo

Generating tables with php?


  • Please log in to reply
2 replies to this topic

#1 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 11 September 2006 - 07:52 PM

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

$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>';

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:

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

Thanks in advance

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 11 September 2006 - 08:03 PM

<?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>";
    }
}
?>
Try that.

Info: PHP Manual


#3 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 11 September 2006 - 08:08 PM

That works great thank you




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users