Solved

evenly distribute items

Posted on 2011-02-21
12
404 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:rgb192
  • 6
  • 4
  • 2
12 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34946312
By an item you mean ... ? What is $x ?
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><td>45</td></tr>
6 <tr><td>123</td></tr><tr><td>456</td></tr>
7 <tr><td>1234</td></tr><tr><td>567</td></tr>
8 <tr><td>1234</td></tr><tr><td>5678</td></tr>
9<tr><td>123</td></tr><tr><td>456</td></tr><tr><td>789</td></tr>

right ?
0
 

Author Comment

by:rgb192
ID: 34946438
yes

how to do this in php
0
 

Author Comment

by:rgb192
ID: 34946450
$x is count of items


each item has an image
echo'<img src='.$x.'>';
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34946566
So the $x variable is an array ? If yes, let's get the count of items in it.

<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>

Open in new window


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 ?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34946678
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
<?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>

Open in new window

0
 

Author Comment

by:rgb192
ID: 34948842
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

0
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.

 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 400 total points
ID: 34949284
Try this:

<table border="1">
<?php

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

// count the items in an array 
$num = count($x);
   switch($num){
      case 1: 
         echo"<tr><td>";
         for($i=0;$i<$num;$i++)
            echo"<img src='$x[$i]' />";
         echo"</td></tr>"; break;
      case 2:          
         echo"<tr><td>";
         for($i=0;$i<$num;$i++)
            echo"<img src='$x[$i]' />";
         echo"</td></tr>"; break;
      case 3: 
         echo"<tr><td>";
         for($i=0;$i<$num;$i++)
            echo"<img src='$x[$i]' />";
         echo"</td></tr>"; break;
      case 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
   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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 34950804
@Roads_Roads:  To save you some typing, see http://us2.php.net/manual/en/function.range.php

$x = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14'); can be written $x = range(1,14);

@rgb192: If what you really want is the distribution of 14 items, please tell us that when you post the question.  The question showed 9 items, not 14.  And it is still not clear how you want these distributed.  Which is more important to you - the number of rows or the number of items per row?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34950840
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.
0
 

Author Comment

by:rgb192
ID: 34952440
>>Which is more important to you - the number of rows or the number of items per row?

 the number of items per row
0
 

Author Closing Comment

by:rgb192
ID: 34953118
0
 

Author Comment

by:rgb192
ID: 34953133
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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

912 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

21 Experts available now in Live!

Get 1:1 Help Now