Hello,

I was thinking what would be a best way to create another column if a list is too long?

Say I have a <ul> tag and doing a while loop output <li> tags. Would it be good to just use like a while loop and set the condition to a certain number, and when it hits a number, do another <ul> tag?

Or is there another way of doing something like this?

I'm not exactly sure how to set up the php for this. Cause what if I don't need three columns now, but would need it later? I would code now for only two columns, but later if I need to, I would have to add codes for third column. But I dont want to go that way. I want to be able to just have it so it will automatically set up for the next column without me going into the back end of it.

Thank you

Hi andrewliu,
you will have to set a limit one way or another:
a) either you say "show max $x rows per column
b) or split the output into $y columns

I guess you prefer (a).

There's no (widely supported) HTML tag for columns so either you will have to use a table and TD for each column, or use floating DIVs and style them with CSS.

Either way, when ending one column and beginning another one, some HTML code must be printed: end current, UL, end TD or DIV, begin new TD or DIV, begin new UL. You could do this in the while loop but the code will look better if you choose to preprocess your data first and use foreach.

/*
 * First read your data and group it
 */
$maxRowsLimit = 123; // set your limit here
$groupedRows = array();
$rowsPerGroup = $groupId = 0;
while ($row = ....) {
  if (++$i > $maxRowsLimit) {
    $i = 0;
    $groupId++;
  }
  $groupedRows[$groupId][] = $row;
}

/*
 * Now display the groups as columns
 */
print '<table><tr>';
foreach ($groupedRow as $rows) {
  print '<td><ul>';
  foreach ($rows as $row) {
    print '<li>'.$row.'</li>';
  }
  print '</ul></td>';
}
print '</tr></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.