Jump to content

[SOLVED] Making Columns


The Little Guy

Recommended Posts

I have a page that displays items according to values in the database.

 

So... I have a column in the database called 'column'. Column tells what column to place the item in. I am having problems creating the columns on the page.

 

$sql = mysql_query("SELECT * FROM userWidgets u LEFT JOIN widgets w ON (u.widget = w.id) WHERE owner = '$_id' ORDER BY `column`, `row`");
while($row = mysql_fetch_array($sql)){

}

 

I am not sure how to code within the loop to make column one and column two.

 

Both columns can contain 1 or more items, so any help?

Link to comment
https://forums.phpfreaks.com/topic/166684-solved-making-columns/
Share on other sites

It's easier to code this than store it into a database. This is just pseudo code so it may need modifying to work but the basics are there. That and I'm mixing HTML with PHP but that's besides the point, it gets the basics out ;)

 

<?php
$columns = 2;
$total_records = 20;
$column_split = ceil($total_records / $columns);

echo '<table><tr>';
while ($row = mysql_fetch_array($sql)) {
  $count++;

  echo '<td>' . $row['tablerow'] . '</td>';

  if ($count == $column_split) {
    echo '</tr><tr>';
    $count = 0;
  }
}
echo '</tr></table>';
?>

I was able to get the effect that I wanted, by adding this to the loop:

 

if($curCol != $row['column']){
echo '<div style="float:left;width:50%;">'."\n";
}
include 'widget_name.php';
if($curCol != $row['column']){
echo '</div>;
$curCol = $row['column'];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.