Jump to content

PHP Counting Columns


codeline

Recommended Posts

I have a page that displays a list of retail locations (est. 100-150 locations).

 

I have my layout setup to fit 3 columns side by side. Ex:

 

<div class="col"></div>

<div class="col"></div>

<div class="col"></div>

 

.. with the "col" class having a specific width and float: left.

 

Basically, I want to print out the locations in the column and when a column reaches a certain number of locations, it closes that div and starts a new one and continues printing the rest of the locations.

 

How would I go about making sure that once a column has reached a certain number of locations, it closes the div and starts the new one?

Link to comment
Share on other sites

Hmmm... wait, I read wrong. You want to put it into columns vertically, not horizontally. However, it's also a simple mathematics. You must count the total number of elements you want to display, divide it by three to estimate, how many you will get within a single column. Then you apply the modulo division every time you display an element:

 

$counter = 0;
foreach($elements as $element)
{
   if($counter % $elementNumInAColumn == 0)
   {
      echo '</div><div>';
   }
   // display the element here.

   $counter++;
}

 

Boundary cases are boundary cases: the first and the last element. Run the script above, and see what's happening with the tags before the first and after the last element. This is because the script does not handle the boundary cases correctly. I leave it to you as an exercise. You have all the necessary information you need: you know when you reach the first element, and when you reach the last. Make use of this knowledge.

 

 

PS. Do you really have no clue what modulo division is and how it works? :)

Link to comment
Share on other sites

Zyx, I took your approach and played around with it a bit and got this:

 

<?php

$q = "SELECT * FROM locations ORDER BY id";
$r = mysql_query($q) or die(mysql_error());

$counter = 0;
$elementNumInCol = 5;

print '<div class="stockistCol">';

while($row = mysql_fetch_object($r)){

	if($counter % $elementNumInCol == 0)
	{
		print '</div><div class="stockistCol">';
	}

	print '
	<ul class="stockistItem">
		<li>' . $row->store . '</li>
		<li>' . $row->city . ', ' . $row->state . '</li>
		<li>' . $row->ph . '</li>
	</ul>
	';

	$counter++;

}

print '</div>';

?>

 

You'll notice that the div column's class is "stockistCol".. So, trying this out, the page did print out 5 locations within the div column and when it reached 5, it closed the div and made a new one, continuing on with printing out the rest of the locations.

 

The only problem I do have right now is that it prints out an empty <div class="stockistCol"></div> before it prints out any of the locations.. so I have an empty column followed by columns filled with the locations.

Link to comment
Share on other sites

Yes, these are the boundary cases. In the if that splits the columns, you must put each tag in one more if:

 

if(! its_the_first_element)
{
   echo '</div>';
}
if(! its_the_last_element)
{
   echo '<div ... >';
}

 

You can use the counter to test, whether it is the first or the last element. In addition, to recognize the last element, you must take the total number of results with mysql_num_rows().

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.