Dynamic create rows and colum with the help of PHP and HTML

前端 未结 5 1678
面向向阳花
面向向阳花 2021-01-01 02:17

I want to create dynamic rows and column with the help of PHP and HTML but I am little confused about this code so some help is definitely appreciated.

&l         


        
相关标签:
5条回答
  • 2021-01-01 02:21

    Use % php operator (division remainder) to break rows where you need

    0 讨论(0)
  • 2021-01-01 02:23

    Here's a better way. This one uses Bootstrap syntax, but you can easily change it to a table format or anything else. Logic is the same. Remember to update the $items array name with your own where appropriate.

    <div class="container">
        <div class="row">
            <?php
            // Sample array
            $items = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    
            // Define how many columns
            $cols = 4;
    
            $tdCount = 1; // Don't change
            foreach ($items as $key => $item)
            {
                ?>
    
                <div class="col"><?php echo $key; ?></div>
    
                <?php
                // Close and open new rows
                if( (($key + 1) % $cols) === 0  && ($key + 1) !== count($items) )
                {
                    ?>
                    </div>
                    <div class="row">
                    <?php
                }
    
                // Increment column count
                $tdCount++;
    
                // Fill with empty columns at the end
                if( ($key + 1) === count($items) && $tdCount <= $cols )
                {
                    $spacers = $cols - $tdCount;
                    for ($i = $spacers; $i >= 0 ; $i--)
                    {
                        ?>
                        <div class="col spacer">&nbsp;</div>
                        <?php
                    }
                }
    
                // Reset columns count
                if( $tdCount > $cols )
                {
                    $tdCount = 1;
                }
            }
        ?>
        </div>
    </div>
    

    It will output:

    <div class="container">
        <div class="row">
            <div class="col">0</div>
            <div class="col">1</div>
            <div class="col">2</div>
            <div class="col">3</div>
        </div>
        <div class="row">
            <div class="col">4</div>
            <div class="col">5</div>
            <div class="col">6</div>
            <div class="col">7</div>
        </div>
        <div class="row">
            <div class="col">8</div>
            <div class="col">9</div>
            <div class="col spacer">&nbsp;</div>
            <div class="col spacer">&nbsp;</div>
        </div>
    </div>
    
    0 讨论(0)
  • 2021-01-01 02:27

    Something like this, maybe

    function create_table()

    function create_table($data) {
      $res = '<table width="200" border="1">';
      $max_data = sizeof($data);
      $ctr = 1;
      foreach ($data as $db_data) {
        if ($ctr % 2 == 0) $res .= '<td align="center">' . $db_data['id']. '</td></tr>';
        else {
          if ($ctr < $max_data) $res .= '<tr><td align="center">' . $db_data['id']. '</td>';
          else $res .= '<tr><td colspan="2" align="center">' . $db_data['id']. '</td></tr>';
          }
        $ctr++;
        }
      return $res . '</table>';
      }
    

    Course, you can modify style of table to fit your needs.

    Call it like this:

    echo create_table($data);
    

    Output

    (example for 7, 4, 3 and 8 id's) enter image description here

    It returns table with same number of rowsin each column if you pass even number of id's or table where last row is merged if you pass odd number of id's into function.

    0 讨论(0)
  • 2021-01-01 02:30

    i think you should try doing it in a while loop . for example

    $sql = your.sql.query;
    $row = mysql_query($sql);
    $result = mysql_fetch_arrey($row)
    <table>
    while($row!=0) {
    echo "
    <tr>
    // number of <td> you need according to your data return in the query:
    <td>$result['column']</td>
    <td>$result['column']</td>
    </tr>
    ";
    }
    

    if i understand what you're trying to do.

    0 讨论(0)
  • 2021-01-01 02:39

    Do something like this:

      foreach ($data as $ord => $db_data)
      {
          if (($ord == count($data) - 1) && (count($data) % 2))
          {
              // Do a colspan of 2, as it is the last item (first clause)
              // and there are an odd number of items (second clause)
          }
      }
    
    0 讨论(0)
提交回复
热议问题