views:

60

answers:

4

I currently have a list (<ul>) of people that I have divided up into two columns. But after finishing the code for it I keept wondering if there is a more effective or clean way to do the same thing.

echo "<table class='area_list'><tr>";
// Loop users within areas, divided up in 2 columns
$count = count($areaArray);
for($i=0 ; $i<$count ; $i++) {
  $uid = $areaArray[$i];
  // get the modulus value + ceil for uneven numbers
  $rowCalc = ($i+1) % ceil($count/2);

  if ($rowCalc == 1) echo "<td><ul>";

  // OUTPUT the actual list item
  echo "<li>{$users[$uid]->profile_lastname}</li>";

  if ($rowCalc == 0 && $i!=0) echo "</ul></td>";
}
echo "</tr></table>";

Any ideas of how to make this cleaner or do it in another way?

+1  A: 

You can just loop twice, once for each column. Start at 0 in the first column and at 1 in the second. Increment by two.

Edit: To make it even nicer, put the columns themselves in a loop:

$cols = 3;

echo '<table><tr>';

// column loop
for ($c = 1; $c <= $cols; $c++) {
  echo '<td><ul>';

  // item loop
  for ($i = 0; $i < count($areaArray); $i += $c) {
     echo '<li>...</li>';
  }

  echo '</ul></td>';
}

echo '</tr></table>';
Ilia Jerebtsov
That is the straight forward way of accomplishing it by I want to be able to change the column count.
WmasterJ
You can wrap the whole thing in a double loop - one for writing out the columns, and the inner loop for writing out the items.
Ilia Jerebtsov
+3  A: 

Dont know whether this is what @llia meant, but what about have the for loops like this:

//declare how many columns are needed
$cols=2;

//iterate over each row of entries (down the column)
for ($i=0;i<$count;i+=cols){ 

     echo "<td><ul>";

     //entry loop (across the row)
     for($j=0;j<$cols;j++){ 

           //whose line is it anyway?
           $uid = $areaArray[$i+$j];

           echo "<li>{$users[$uid]->profile_lastname}</li>";
     }
     //end entry loop

     echo "</ul></td>";
}
//end row loop

that way, you can set however many columns you like.

Forgive me if I missed something as I'm waiting for the kettle to boil to get my much needed caffine!

Andrew Bolster
Thanks for that. It doesn't quite work since I have an array `$areaArray` to iterate over (had left that out in the code snippet above, so it's my bad). So I cannot have a for-loop within my for-loop that iterates over the array itself. That is why I do check for each single `$i`
WmasterJ
It gives me a different perspective to work with though, thanks!
WmasterJ
Completely didnt see the '$uid' assignment! but I'll edit it to show where you could put it.
Andrew Bolster
@Andrew: Thanks for the new effort, it didn't really work out either since the counter for the array only does `$i+$j` but it has go go from say 1-5 in the first column and then from 6-10 in the second column. Therefor I need to be able to get it to 6 in the second column. I think I got what I asked though since there are more than one perspective now for me to try out. :)
WmasterJ
+1  A: 

This should do what you want. I pretty much just combined the other two answers so +1 for everyone.

$cols=3;
$user_count = count($users);

echo "<table class='area_list'><tr>";

// Column loop
for($i=0;$i<$cols;$i++){
    echo "<td><ul>";

    // user loop
    for($j=$i;$j<$user_count;$j+=$cols){
        echo "<li>{$users[$j]->profile_lastname}</li>";
    }
    echo "</ul></td>";
}
echo "</tr></table>";
Syntax Error
Actually this wouldn't work either since `$j+=$cols` makes it jump `3` each time. And it still doesn't take into account the entire array of users. It would have to multiplied in the inner for-loop where `$j` is first set to be `= ($count/2)*$cols` to actually work for the counter to start at the right spot. Thanks for the effort though since this actually led me to a new way of doing it as long as I make the changes I just specified. Thanks! +1
WmasterJ
All users are output. It's supposed to jump by 3 each time because I have it set to do 3 columns - you wanted to be able to specify how many columns, right?. Each time the user loop starts it starts at 0, then 1, then 2. So first loop it does users 0, 3, 6, 9, 12, 15. Next loop it does users 1, 4, 7, 10, 13... Next loop it does 2, 5, 8, 11, etc...But either way, I'm glad it was helpful.
Syntax Error
A: 
$cols = 3;
$chunkSize = ceil(count($areaArray) / $cols);
echo $chunkSize * $cols;
foreach (array_chunk($areaArray, $chunkSize) as $items) : ?>
 <td>
  <ul>
<?php foreach ($items as $item) : ?>
   <li><?php echo $item ?></li>
<?php endforeach; ?>
  </ul>
 </td>
<?php endforeach; ?>
chris