Web Analytics Made Easy -
StatCounter Dynamic new line in table - CodingForum

Announcement

Collapse
No announcement yet.

Dynamic new line in table

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic new line in table

    I have a php script which pulls entries from a MySQL database, I want it to display all the enteries, but only 4 per row in the table, then it automaticaaly adds </TR><TR> and starts again

    any help?
    Matt Davenport - Freelance Web Developer

  • #2
    a simple example:
    PHP Code:
    $result mysql_query('SELECT field FROM table') or exit(mysql_error());
    while (
    $row mysql_fetch_assoc($result)) {$array[] = $row['field'];}
    $chunks array_chunk($array4);

    echo 
    '<table border="1">';
    foreach (
    $chunks as $value)
    {
        echo 
    '<tr>';
        foreach (
    $value as $sub_value)
        {
            echo 
    '<td>' $sub_value '</td>';
        }
        echo 
    '</tr>';
    }
    echo 
    '</table>'

    Comment


    • #3
      or, to save looping unnecessarily:
      PHP Code:
      $i=0;
      while (
      $row mysql_fetch_assoc($result)) {
        if(
      $i%4==0) {
          echo 
      '<tr>';
        }
        echo 
      '<td>blah</td>';
        if(
      $i%4==3) {
          echo 
      '</tr>';
        }
        
      $i++;


      using the modulus operator, %
      My thoughts on some things: http://codemeetsmusic.com
      And my scrapbook of cool things: http://gjones.tumblr.com

      Comment


      • #4
        thanks so much! that's worked a charm!
        Matt Davenport - Freelance Web Developer

        Comment

        Working...
        X