Solved

if there is only one row, I want it to be on the second row

Posted on 2011-02-22
4
157 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



the code is correct
but if there is only one row, I want it to be on the second row
so I want either a linebreak or an empty first row
<table border="1">
<?php

$x = array('1','2','3','4','5','6','7','8','9','10','11','12');

// 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
Comment
Question by:rgb192
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 250 total points
ID: 34954832
Like this ?

<table border="1">
<?php

$x = array('1','2','3','4','5','6','7','8','9','10','11','12');

// 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>";
    
   if($numrows == 1) echo"<br />";

    // 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
 
LVL 11

Expert Comment

by:Ovid Burke
ID: 34956392
Try this:

<table border="1">
<?php
$x = array('1','2','3','4');

// 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>";
	
	if($numrows == 1) {
		// insert space, close first row, and start second row
		echo "&nbsp;</td></tr><tr><td>";
	}
	
	// first row of actual 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
 
LVL 11

Assisted Solution

by:Ovid Burke
Ovid Burke earned 250 total points
ID: 34960048
Hey,

I had shortened the array $x to test for a small number of items. Here it is with all 12:
<table border="1">
<?php
$x = array('1','2','3','4','5','6','7','8','9','10','11','12');

// 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>";
	
	if($numrows == 1) {
		// insert space, close first row, and start second row
		echo "&nbsp;</td></tr><tr><td>";
	}
	
	// first row of actual 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: 34966053
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Php Email function not working with Template 8 38
WordPress Themes 10 34
Should I use subdomains or addon domains? 3 39
mysql between clause 2 8
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 count occurrences of each item in an array.

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now