Solved

evenly distribute items up to 14 items

Posted on 2011-02-22
5
348 Views
Last Modified: 2012-05-11
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

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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:EMB01
ID: 34953275
I think this is what you are saying, please see if it works like you want:
<?php

$html = "<table>";

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

if ($x==5) {

$html .= "<tr>";
for ($i = 0; $i < 3; $i++) {
//3 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 2; $i++) {
2 items on second row 
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

}


if ($x==7) {

$html .= "<tr>";
for ($i = 0; $i < 4; $i++) {
//4 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 3; $i++) {
3 items on second row 
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

}

if ($x==9) {

$html .= "<tr>";
for ($i = 0; $i < 3; $i++) {
//3 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 3; $i++) {
//3 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 3; $i++) {
//3 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

}

if ($x==14) {

$html .= "<tr>";
for ($i = 0; $i < 5; $i++) {
//5 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 5; $i++) {
//5 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 4; $i++) {
//4 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

}

$html .= "</table>";

// output
echo $html;

?>

Open in new window

0
 

Author Comment

by:rgb192
ID: 34953331
i want this done for all the values
1 to 14
0
 
LVL 14

Accepted Solution

by:
EMB01 earned 500 total points
ID: 34953349
Here, use this one (don't forget to assign a value to "x"):
<?php

$html = "<table>";

$x = 5;

if ($x==5) {

$html .= "<tr>";
for ($i = 0; $i < 3; $i++) {
//3 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 2; $i++) {
//2 items on second row 
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

}


if ($x==7) {

$html .= "<tr>";
for ($i = 0; $i < 4; $i++) {
//4 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 3; $i++) {
//3 items on second row 
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

}

if ($x==9) {

$html .= "<tr>";
for ($i = 0; $i < 3; $i++) {
//3 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 3; $i++) {
//3 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 3; $i++) {
//3 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

}

if ($x==14) {

$html .= "<tr>";
for ($i = 0; $i < 5; $i++) {
//5 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 5; $i++) {
//5 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

$html .= "<tr>";
for ($i = 0; $i < 4; $i++) {
//4 items on first row
$html .= "<td>Item $i</td>";
}
$html .= "</tr>";

}

$html .= "</table>";

// output
echo $html;

?>

Open in new window

0
 
LVL 14

Expert Comment

by:EMB01
ID: 34953355
Sorry, I don't understand what you're asking.  Do you mean if x is 1-14?  Please try to explain in detail.
0
 

Author Closing Comment

by:rgb192
ID: 34953486
yes your solution is correct
my question did not ask 1-14 items
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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 i…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 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