Creating dynamic table

Hello, I need help to create dynamic table using data from database
like this

 <table border="1">
     <tr>
     <td>Coutry</td>
     <td>Position</td>
     <td>Name</td>
    </tr>
     <tr>
        <td rowspan="9">Usa</td>
         <td rowspan="2">North Countries</td>
         <td>Montana</td>
         
     </tr>
     <tr>
        <td>Minesota</td>
     </tr>
    <tr>
        <td rowspan="3">Middle Countries</td>
        <td>Colorado</td>
       
     </tr>
     <tr>
        <td>Cansas</td>
     </tr>
      <tr>
        <td>Colorado</td>
     </tr>
     
      <td rowspan="4">South Countries</td>
        <td>Arizona</td>
     </tr>
     <tr>
        <td>Texas</td>
     </tr>
    <tr>
        <td>Alabama</td>
     </tr>
    <tr>
        <td>Florida</td>
     </tr>
    
     <tr>
        <td rowspan="4">Canada</td>
         <td rowspan="2">NorthCountry</td>
         <td>Nunavut</td>
         
     </tr>
     <tr>
        <td>Yucon</td>
     </tr>
    <tr>
        <td rowspan="2">South Countries</td>
        <td>Ontario</td>
       
     </tr>
     <tr>
        <td>Alberta</td>
     </tr>
</table>

I’m using

    foreach ($data as $i=> $countries)
    {
       foreach($countries=>$k=>$v)
      {
         //get names from table
          ${$k}=$v;
      }
          echo ' <tr>
            <td rowspan="4">$country</td>
             <td rowspan="2">$position</td>
             <td>$name</td>
             
         </tr>"
    }

problem is i don’t know how to use this rowspan , i can count but how to use for other <tr></tr> to creati table, in my database i have all country not few like in example

you can make easily dynamic table . you follow my link.

http://www.mysamplecode.com/2012/04/generate-html-table-using-javascript.html:

THANKS

If you don`t want to use js then you can create using PHP.

Below is the code you can try and edit according to your needs. Basically this code will create a table with particular number of rows and columns, I hope this will helps you.

**PHP CODE:** 

        <span style="color:red"></span>
<?php
if(isset($_REQUEST['create']))
    { ?>
  <span class="comments">
    <?php $row=$_REQUEST['rows']; ?>
 <span class="comments"></span>
<?php $col=$_REQUEST['columns']; ?>
     <span class="comments"> </span>
        <?php echo "<table border='1' width='350px'>";
        for($i=1;$i<=$row;$i++)
        { ?>
 <span class="comments"></span>
        <?php    echo "<tr>";
            for($j=1;$j<=$col;$j++)
            { ?>
              <span class="comments"></span>
            <?php echo "<td>row".$i."col".$j."</td>";
            } ?>
                       <span class="comments"></span>
            <?php echo "</tr>";
            
        } ?>
       <span class="comments"></span>
        <?php echo "</table>";    
    } ?>
<span style="color:red"></span>

HTML:

<html>
    <head>
 
        <title>Create Table Dynamically</title>
        <style>
            input{width:150px; height:35px}
        </style>
    </head>
 
    <body>
        <form method="post" action ="table.php">
            <table border="1" width="350px">
                <tr>
                    <td>Enter number of Rows</td>
                    <td><input type="text" name="rows"/></td>
                </tr>
                <tr>
                    <td>Enter number of Columns</td>
                    <td><input type="text" name="columns"/></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                    <input type="submit" value="Create Table" name="create"/>
                    </td>
                </tr>
            </table>
        </form>    
    </body>
</html>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.