rgb192
asked on
evenly distribute items
if $x<=4 do one row
if $x<=8 && $x>=5 two rows
if $x>=9 three rows
I need to do
<table>
<tr>
<td>
and I want the items to evenly distributed
so if $x==5
3 items on first row
2 items on second row
if $x<=8 && $x>=5 two rows
if $x>=9 three rows
I need to do
<table>
<tr>
<td>
and I want the items to evenly distributed
so if $x==5
3 items on first row
2 items on second row
ASKER
yes
how to do this in php
how to do this in php
ASKER
$x is count of items
each item has an image
echo'<img src='.$x.'>';
each item has an image
echo'<img src='.$x.'>';
So the $x variable is an array ? If yes, let's get the count of items in it.
The more than 9 items situation - what if there are 14 items - it means that there should be 3 rows with 5,5,4 items ?
<table>
<?php
// count the items in an array
$num = count($x);
switch($x){
case 1,2,3,4:
echo"<tr><td>";
for($i=0$i<$num;$i++)
echo"<img src='$x[$i]' />";
echo"</td></tr>"; break;
case 5:
echo"<tr><td><img src='$x[0]' /><img src='$x[1]' /><img src='$x[2]' /></td></tr>";
echo"<tr><td><img src='$x[3]' /><img src='$x[4]' /></td></tr>";
break;
case 6:
echo"<tr><td><img src='$x[0]' /><img src='$x[1]' /><img src='$x[2]' /></td></tr>";
echo"<tr><td><img src='$x[3]' /><img src='$x[4]' /><img src='$x[5]' /></td></tr>";
break;
case 7:
echo"<tr><td><img src='$x[0]' /><img src='$x[1]' /><img src='$x[2]' /><img src='$x[3]' /></td></tr>";
echo"<tr><td><img src='$x[4]' /><img src='$x[5]' /><img src='$x[6]' /></td></tr>";
break;
case 8:
echo"<tr><td><img src='$x[0]' /><img src='$x[1]' /><img src='$x[2]' /><img src='$x[3]' /></td></tr>";
echo"<tr><td><img src='$x[4]' /><img src='$x[5]' /><img src='$x[6]' /><img src='$x[7]' /></td></tr>";
break;
}
// TO DO
// handling of more than 9 items
?>
</table>
The more than 9 items situation - what if there are 14 items - it means that there should be 3 rows with 5,5,4 items ?
I may not be getting the concept exactly right, but try this and see if it makes sense to you. On the web at:
http://www.laprbass.com/RAY_temp_rgb192.php
http://www.laprbass.com/RAY_temp_rgb192.php
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo "<pre>";
// FROM THE POST AT EE:
// evenly distribute items
// Question:
// if $x<=4 do one row
// if $x<=8 && $x>=5 two rows
// if $x>=9 three rows
// IF ANYTHING POSTED
if (!empty($_POST["i"]))
{
// GET THE POSTED DATA IN A LOCAL VAR
$i = (int)trim($_POST["i"]);
// MAYBE MAKE SOME SANITY CHECKS ABOUT THE NUMBER HERE
// START THE TABLE
echo "VIEW SOURCE TO SEE THE TABLE" . PHP_EOL;
echo "<table>" . PHP_EOL;
while ($i)
{
// SET THE COUNTER FOR THE ROW
$n = 4;
// START THE ROW
echo "<tr>" . PHP_EOL;
while ($n)
{
// START THE DATA ELEMENTS
echo "<td>$i</td>" . PHP_EOL;
// DECREMENT THE COUNTER
$i--;
// FILLS OUT THE ROW WITH EMPTY CELLS
if ($i == 0) $i = NULL;
// COUNT CELLS PER ROW
$n--;
}
// END OF THIS ROW
echo "</tr>" . PHP_EOL;
}
// END OF THIS TABLE
echo "</table>" . PHP_EOL;
}
// END PHP, PUT UP THE FORM
?>
<form method="post">
ENTER A NUMBER BETWEEN 1 AND 9
<input name="i" />
<input type="submit" />
</form>
ASKER
ray answer was not 'even'
there was a
4 in one row
and
2 in another row
wanted 3 3
>>The more than 9 items situation - what if there are 14 items - it means that there should be 3 rows with 5,5,4 items ?
and roads answer
was correct
but how do to 3 rows
5
5
4
is what I want
there was a
4 in one row
and
2 in another row
wanted 3 3
>>The more than 9 items situation - what if there are 14 items - it means that there should be 3 rows with 5,5,4 items ?
and roads answer
was correct
but how do to 3 rows
5
5
4
is what I want
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks Ray I was not aware of that function.
I assume that the range is closed downwards (of course...) with only one item - the specific case is that when the number of items is >= 9, so what I did was to add some more to that, to see if the function works.
I assume that the range is closed downwards (of course...) with only one item - the specific case is that when the number of items is >= 9, so what I did was to add some more to that, to see if the function works.
ASKER
>>Which is more important to you - the number of rows or the number of items per row?
the number of items per row
the number of items per row
ASKER
thanks
I have a similar question up to 14 items
https://www.experts-exchange.com/questions/26837126/evenly-distribute-items.html?anchorAnswerId=34952440#a34952440
I have a similar question up to 14 items
https://www.experts-exchange.com/questions/26837126/evenly-distribute-items.html?anchorAnswerId=34952440#a34952440
ASKER
sorry
this is the correct link to simliar question
https://www.experts-exchange.com/questions/26839182/evenly-distribute-items-up-to-14-items.html
this is the correct link to simliar question
https://www.experts-exchange.com/questions/26839182/evenly-distribute-items-up-to-14-items.html
With what I understood, you need to distribute let's say numbers from 1to $x. So if the $x is:
1,2,3,4 - <td>1234</td>
5 <tr><td>123</td></tr><tr><
6 <tr><td>123</td></tr><tr><
7 <tr><td>1234</td></tr><tr>
8 <tr><td>1234</td></tr><tr>
9<tr><td>123</td></tr><tr>
right ?