Solved

evenly distribute items up to 1-14 items

Posted on 2011-02-22
2
231 Views
Last Modified: 2012-06-27



if $x<=4 do one row
if $x<=8 && $x>=5 two rows
if $x>=9 && $x<=14 three rows


I need to do

<table>
<tr>
<td>

and I want the items to evenly distributed
with the number of items per row being important


x can be any value 1-14


these are examples:
if $x==5
3 items on first row
2 items on second row

if $x==7
4 items on first row
3 items on second row


if $x==9
3 items on first row
3 items on second row
3 items on third row

if $x== 14
5
5
4
is what I want

0
Comment
Question by:rgb192
2 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 34953651
Try

<table border="1">
<?php

$x = array('1','2','3','4','5','6');

// count the items in an array 
$num = count($x);
   switch($num){
      case 1: $numrows = 1; break;
      case 2: $numrows = 1; break;
      case 3: $numrows = 1; break;      
      case 4: $numrows = 1; break;                        
      case 5: $numrows = 2; break;                        
      case 6: $numrows = 2; break;                        
      case 7: $numrows = 2; break;                        
      case 8: $numrows = 2; break;                        
   }

   // TO DO
   // handling of more than 9 items
   if($num >= 9) $numrows = 3;
   
   {
   // how much items do we have left
   $itemsleft = $num;
   $firstitem = 0;
   
   for($i=$numrows;$i>0;$i--){
    echo"<tr><td>";
    
    // first row items
    $numitems = ceil($itemsleft/$i);
    // substract the items
    $itemsleft = $itemsleft - $numitems;
    
    for($j=$firstitem;$j<$firstitem+$numitems;$j++)
    echo"<img src=".$x[$j]." />";
    $firstitem = $firstitem + $numitems;
    
    echo"</td></tr>";
   }
   
   }
   
?>
</table>

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 34954294
works great, thanks

I have a similiar question where I want a linebreak if there is only one row

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26839556.html
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question