Hey,
This is probably a newbie question, but that is somewhat what I am to PHP. I need to create a two column table within a while statement.

<?php
$letter = $data['letter'];
$getCats = mysql_query("SELECT * FROM categories WHERE catName LIKE '$letter%' ORDER BY catName"); 

while ($cat = mysql_fetch_array($getCats)) {
	$catID = $cat['catID'];
	echo "<li><a href=\"category.php?letID=$letID&catID=$catID\">" . $cat['catName'] . "</a></li>";
}
?>

That is what I have right now, it is just listing them in an unordered list. I have googled how to make the two column table, but I don't understand what the code does, so I was wondering if someone could show me how to do it and what each part does.

I would be forever greatful for anyone's help,
Key

Do you know html? You would have to set up the table before the while loop then do a table row for each item. I don't see your 2nd column data, I just see the catName. But I guess it would be something like:

<?php
$letter = $data['letter'];
$getCats = mysql_query("SELECT * FROM categories WHERE catName LIKE '$letter%' ORDER BY catName"); 
echo "<table>";
while ($cat = mysql_fetch_array($getCats)) {
	$catID = $cat['catID'];
	echo "<tr><td></td><td><a href=\"category.php?letID=$letID&catID=$catID\">" . $cat['catName'] . "</a></td></tr>";
}
echo "</table>";
?>

Yeah, I know HTML, but I want the categories listed in two columns. (I should have mentioned that part) When I went to find out how to do it, it looked really complicated.

Oh okay I see what you're trying to do. Hmm that's a hard one, I can't think of anything right now.

I copied this from a CMS of shorts to see if it could help us with figuring it out.

<?php // The category boxes only take the full width if there are at least $this->categoryColumnsNum boxes:
  $tableWidth = "100%";
  $categories =& $this->get("categories");
  $catNum=count($categories);
  $categoryColumnsNum =& $this->get("categoryColumnsNum");
  $oneCatWidth = 100/$categoryColumnsNum; 
  $alternatingColorsNum = 4;  // one different color for each column
?>

<?php if( $catNum ): /* if there are category boxes at all */ ?>
<h3>Categories</h3>
  <table cellspacing='0' class='catGrid' width='<?php echo $tableWidth ?>' align='center'>
    <?php for( $i=0; $i<$catNum; $i++ ): ?>
      <?php if( ($i % $categoryColumnsNum) == 0 ): /* new row necessary */ ?>
        <tr>
      <?php endif; ?>
          <td width='<?php echo $oneCatWidth ?>%' class='catColor<?php echo $i % $alternatingColorsNum ?>'>
            <div class='catTitle'>
              <a href='<?php echo $categories[$i]->link ?>'><?php echo $categories[$i]->title ?> [<?php echo $categories[$i]->itemNum ?>]</a> 
            </div>   
            <?php if($categories[$i]->picture): ?>
              <div class='catPicture'>
                <a href='<?php echo $categories[$i]->link ?>'><img src='<?php echo $categories[$i]->picture ?>' width='202px'></a> 
              </div> 
            <?php endif; ?>
            <?php if($categories[$i]->description): ?>
              <div class='catDescription' style="">
                <div><?php echo $categories[$i]->description ?></div> 
              </div> 
            <?php endif; ?>
          </td>
      <?php if( (($i+1) % $categoryColumnsNum) == 0): /* row end necessary */ ?>
        </tr>
      <?php endif; ?>  
    <?php endfor; ?>
    <?php if( ($colspNum = $i % $categoryColumnsNum) > 0): /* filling the rest of the row with an empty td */ ?>
          <td colspan='<?php echo $colspNum ?>'></td>
        </tr>  
    <?php endif; ?>
  </table>
<?php endif; ?>

When I googled it, it was very similar to that anyway.

Can anyone help with a two column table in a while statement?

Can anyone help with a three column table ?

Member Avatar for diafol

If I understand you correctly you want to place all your records into two columns. DO they have to be ordred in any way?

...
$num = mysql_num_rows($result);
$x = 1;
$output="";
while($data = mysql_fetch_array($result)){
  if($x%2 == 0){
    $output .= "<td>$data['field']</td></tr>"; //for even records
  }else{
    $output .= "<tr><td>$data['field']</td>"; // for odd records
  }
  $x++;
}
if($num%2 != 0)$output .= "<td>&nbsp;</td></tr>"; //tag on empty cell if odd no. records

echo "<table>$output</table>";
...

This will give a zig-zag order. A two column sort is more fiddly - you probably need to place your record values into an array.

Okay, I got it. I looked up what the % operator meant and I completely get it. It seems to me like it would work. I will test it when I get into the office in the morning. Thanks for your help.

Props on that solution. really creative!

That code works well, the only problem is that for some reason it never likes it when I put $data in, I have to put it in a variable and echo the variable. that's all though, other than that, all is fine!

Member Avatar for diafol

Oops! Sorry my fault - you need to brace the variable - array variables if they contain ' or " need to be braced.

...
$num = mysql_num_rows($result);
$x = 1;
$output="";
while($data = mysql_fetch_array($result)){
  if($x%2 == 0){
    $output .= "<td>{$data['field']}</td></tr>"; //for even records
  }else{
    $output .= "<tr><td>{$data['field']}</td>"; // for odd records
  }
  $x++;
}
if($num%2 != 0)$output .= "<td>&nbsp;</td></tr>"; //tag on empty cell if odd no. records
 
echo "<table>$output</table>";
...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.