2 columns

Discussion in 'PHP' started by promotingspace.net, Aug 28, 2008.

  1. #1
    Hi
    I want to show the table data in 2 columns lik this:
    category1 category2
    category3 category4
    category5 category6
    ...

    but I don't know how to code within the while loop
    can you help? any idea?
    thanks
     
    promotingspace.net, Aug 28, 2008 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    its like your doing odd and even numbers

    for(condition){
    $divstyle = index = odd ? 'odd' : 'even';
    }
     
    bartolay13, Aug 28, 2008 IP
  3. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for your reply. but I didn't understand
    also you know that category1 , 2 etc represents the categories names
     
    promotingspace.net, Aug 29, 2008 IP
  4. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #4
    for($i=0,$n=count(categories)-1;$i<=$n;$i++)
    {
    $div = $i%2 ? "div style to left or table column to left" : "div style to right or table column to right";
    echo $div;
    echo $variable;
    echo "</div>";
    }
     
    bartolay13, Aug 29, 2008 IP
  5. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Another way ... just update $colums with how many colums you want:

    
      $colums = 2;
      $count = 0;
      echo '<table><tr>';
      while($row = mysql_fetch_array($result)) {
       echo '<td>' . $row['category'] . '</td>' ;
       $count += 1;
       if ($count == $colums){
         echo '</tr><tr>';
         $count = 0; 
       }
      }
      while ($count < $colums) {
        echo '<td>&nbsp;</td>';
        $count += 1;
      }
      echo '</tr></table>';
    
    Code (markup):
     
    mallorcahp, Aug 29, 2008 IP