I have simple table that has about 80 rows, which I populate dynamically using PHP. What I am trying to do is to layout those rows in chunks for each column. So if I have 80 rows, I would like 4 columns of 20 rows or so, maybe the last column has less or more depending on the total number of rows. The total number of rows can change!
I am having trouble coming up with an implementation method that will not get messy! Anyone know of a simple way that I can implement this.
I have tried using a counter as I loop the data to populate the table and when a multiple of of 20 is reached move to the next block but that didn't work for me as I had extra rows left over.
foreach($indexes as $index){
$counter++;
echo '<tr>';
if($counter > 20){
$multiplier = $counter / 20;
$head = '<td></td>';
for($i=1; $i<$multiplier; $i++){
$head .= '<td></td>';
}
}
if($counter < 20){
$head = '';
}
echo "$head<td>$index</td><td><input id='$index' name='$index' type='checkbox' /></td>";
echo '</tr>';
}
Thanks all for any help
I would do :
$nbCols = 4;
$nbRows = count($indexes)/$nbCols;
for($row=0; $row<$nbRows; $row++) {
echo "<tr>";
for($i=0; $i<$nbCols; $i++) {
$index = $indexes[$row + ($i*$nbRows)];
echo "<td>$index</td><td><input id='$index' name='$index' type='checkbox' /></td>";
}
echo "</tr>";
}
Wouldn't you want to see the remainder of your division and deal with that also?
if($counter % 20 == 0){
// You've no remainder
}else{
// Do another loop to output the odd rows
}
Or you could % 2 == 0 to see if it's even, and then just multiply the whole result by 10.
Be sure to look at ceil() and floor() also for ensuring your number of rows is a round number.
if you dont mind to have this kind of cell order:
1 2 3 4
5 6 7 8
you can use <div style='float:left'>$cellValue</div> in the loop without use of table.
Related
I'm stuck trying to use nested loops to make a reflective pattern from numbers.
I've already tried, but the output looks like this:
|0|1|2|
|0|1|2|
|0|1|2|
This is my code:
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 3; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 3; $col++) {
$p = $col-1;
echo "<td>$p</td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
I expected this result:
|0|1|0|
|1|2|1|
|0|1|0|
Each columns' and rows' cell values must increment to a given amount then decrement to form a mirror / palindromic sequence.
First declare the square root of the of the table cell count. In other words, if you want a 5-by-5 celled table (25 cells), declare $size = 5
Since your numbers are starting from zero, the highest integer displayed should be $size - 1 -- I'll call that $max.
I support your nested loop design and variables are appropriately named $row and $col.
Inside of those loops, you merely need to make the distinction between your "counters" as being higher or lower than half of the $max value. If it is higher than $max / 2, you subtract the "counter" (e.g. $row or $col) from $max.
By summing the two potentially adjusted "counters" and printing them within your inner loop, you generate the desired pattern (or at least the pattern I think you desire). This solution will work for $size values from 0 and higher -- have a play with my demo link.
Code: (Demo)
$size = 5;
$max = $size - 1;
echo "<table>\n";
for ($row = 0; $row < $size; ++$row) {
echo "\t<tr>";
for ($col = 0; $col < $size; ++$col) {
echo "<td>" . (($row >= $max / 2 ? $max - $row : $row) + ($col >= $max / 2 ? $max - $col : $col)) . "</td>";
}
echo "</tr>\n";
}
echo "</table>";
Output:
<table>
<tr><td>0</td><td>1</td><td>2</td><td>1</td><td>0</td></tr>
<tr><td>1</td><td>2</td><td>3</td><td>2</td><td>1</td></tr>
<tr><td>2</td><td>3</td><td>4</td><td>3</td><td>2</td></tr>
<tr><td>1</td><td>2</td><td>3</td><td>2</td><td>1</td></tr>
<tr><td>0</td><td>1</td><td>2</td><td>1</td><td>0</td></tr>
</table>
There are a lot of ways to achive that.
An easy way to do that is;
<?php
$baseNumber = 0;
echo "<table border='1' style='border-collapse: collapse'>";
for ($row = 0; $row < 3; $row++) {
echo "<tr>";
if ($row % 2 !== 0) {
$baseNumber++;
} else {
$baseNumber = 0;
}
for ($col = 0; $col < 3; $col++) {
echo "<td>" . ($col % 2 === 0 ? $baseNumber : $baseNumber + 1) . "</td>";
}
echo "</tr>";
}
echo "</table>";
this code will do what you want just call the patternGenerator function with the number of distinct numbers you want for your example these numbers are 3 (0,1,2).
the idea in this code is to use two for loops one that starts from the minimum number to the maximum one and the other one that starts after the maximum number decreasing to the minimum.
for example:
if min = 0 and max = 5
the first loop will print 0,1,2,3,4,5
and the second will print 4,3,2,1,0
and that's it.
at first, I created a function that creates just on row called rowGenerator it takes $min and $max as parameters and prints one row
so if we want to print a row like this: |0|1|0| then we will call this function with min = 0 and max = 1 and
if we want to print a row like this: |1|2|1| then we will call it with min = 1 and max = 2.
function rowGenerator($min, $max)
{
echo '<tr>';
for($i = $min; $i<=$max;$i++)
echo '<td>'.$i.'</td>';
for($i = $max-1; $i>=$min;$i--)
echo '<td>'.$i.'</td>';
echo '</tr>';
}
for now, we can print each row independently. now we want to print whole the table if we look at the calls we do for the rowGenerator function it will looks as follow:
(min = 0, max = 1),
(min = 1, max = 2) and
(min = 0, max = 1).
minimums are (0,1,0).
yes, it's the same pattern again. then we need two loops again one to start from 0 and increase the number until reach 1 and the other one to loop from 0 to 0.
and that's what happened in the patternGenerator function. when you call it with the number of distinct numbers the function just get the min that will always be 0 in your case and the max.
function patternGenerator($numberOfDistinct )
{
echo "<table border =\"1\" style='border-collapse: collapse'>";
$min = 0;
$max = $numberOfDistinct - 2;
for($i = $min;$i<=$max; $i++)
{
rowGenerator($i,$i+1);
}
for($i = $max-1;$i>=$min;$i--)
{
rowGenerator($i,$i+1);
}
echo '</table>';
}
this is the output of calling patternGenerator(3):
the output of calling patternGenerator(5):
I programmed this code but I have a problem that in every row show the same value "1". I would a counter for positioning or number for the row
Position
1
2
3
4
Here the code: what I need to change or what I doing wrong?
case "18":
$html.='<td class="product_tax_name product_list_content product_list_content_'.($j+1)
.' product_list_col_'.($j+1).'">';
for($i=1; $i<=1; $i=$i+1)
{
$html.= $i;
$i++;
}
$html.='</td>';
break;
}
return $html;
You have to increment $j with ++$j
++$j;
$html.='<td class="product_tax_name product_list_content product_list_content_'.$j.' product_list_col_'.$j.'">';
For example purposes
$j = 0;
foreach(range(1,5) as $blah){
echo (++$j)."\n"; //you can ignore the line return "\n"
}
See the example here
Outputs:
1
2
3
4
5
-Note- in order to increment without warning messages you have to first define So like I did in the above example.
Another way to do this is:
$j += 1;
Which is the same as saying
$j = $j+1;
The Crux of your issue is that you are not assigning anything, your just takeing the value of $j adding 1 and outputting it without changing the value of $j. What I showed above are ways to do both the assignment and the modification of the variable.
So if $j was equal to 4 you would get 5 every time. You either set it to 0 or did not define it.
Also we have this mess to contend with
for($i=1; $i<=1; $i=$i+1){
$html.= $i;
$i++;
}
This loops one time, and you wind up with $i=3. Also it's bad form.
for($i=1; $i<=1; ++$i){
$html.= $i;
}
//outputs 1
Now it loops still 1 time but you end with $i=2
UPDATE
If the above for loop is what you mean when you say
$html.= "put here the counter(1,2,3,4,5)"
Then well yea you're only looping 1 time so you will get only one number. Try this,
for($i=1; $i<=10; ++$i){
$html.= $i;
}
//outputs 12345678910 as there is no separator here.
Or even better
$html .= implode(",", range(1,10));
//Outputs: 1,2,3,4,5,6,7,8,10
So to put it all together ( literally )
$html.= "put here the counter(".implode(",", range(1,4)).")";
As for what the "range" is on this, what it counts. Well tell me what this means and I will let you know:
I would a counter for positioning or number for the row
Position 1 2 3 4
I'm trying to build a little program to automatize a little task; in it, one can write two values (as inputs in a form) and then, in another page, numbers will be written in rows and columns with all the numbers within the range given.
The thing is... There will (almost always) be 500 numbers in the range, so I want the result to be displayed in 10 columns of 50 rows each, something like this image:
I wanted the result page (the one with all the numbers) to THEN give the possibility to export this as xml or pdf, but that's another story..
By now, I just would like to get the display right.
I'm using HTML and PHP on XAMPP. I've been able to get the first column... but can't get to the generations of the others, like this (and so on until 50):
So far, this is what I have, I'll put here my PHP code, because I don't think the HTML is relevant (but will post it too if it's needed) - (The values are passed by POST method)
<?php
$cont = 0;
$i = 0;
$arrayValues = range($valueFrom, $valueTo);
if($cont <= 50){
echo "<tr>";
for($i = 0; $i < 50; $i++){
echo "<p>$arrayValues[$i]</p>";
$cont++;
}
echo "</tr>";
}
?>
Also, using something like this --> Use PHP to Generate HTML table with static Cell Count of MySQL Data and worked ok, but I would the numbers displayed in columns... so not in rows (horizontally), so..
YES | NO
1 4 7 | 1 2 3
2 5 8 | 4 5 6
3 6 9 | 7 8 9
That's why I stopped trying with tables and just used a tag for the result.
EDIT:
So, given the "start" value and "end" value for a range, I would like to be able to print all those values in 10 columns of 50 rows each (so when it reaches 50 rows in the first column, it automatically moves to a new column and so on), and vertically (like the little diagram I tried to put above this, in the "YES" side)
Any ideas or guidance will be appreciated! :)
Have a nice rest of the week!
You can do this by splitting your range up into chunks and indexing into the resulting array of columns in a nested loop.
<table>
<?php
$valueFrom = 1;
$valueTo = 500;
$rowCount = 50;
$columns = array_chunk(range($valueFrom, $valueTo), $rowCount);
$columnCount = count($columns);
for ($row = 0; $row < $rowCount; $row++) {
echo '<tr>';
for ($column = 0; $column < $columnCount; $column++) {
$number = $columns[$column][$row] ?? '';
echo "<td>$number</td>";
}
echo '</tr>';
}
?>
</table>
I am having a basic problem, but now it gives pain me lot. I just want a table which have three column in each row. I want to add a extra empty column in a row when it has two columns. code here...
$j=0;
while ($data = mysql_fetch_assoc($q))
{
// when 3 columns fill, it create new row
if (($j%3) == 0)
{
echo "ADD A ROW";
}
$j++;
}
But now I need to know how many columns ($j value) in this loop to add a extra empty column in a row when it has two columns. I know count() is not available in loop. If know $columnNumber, I can handle this look like...
if ($columnNumber == 2)
{
echo "ADD A COLUMN";
}
How I do
As j will be the total number of columns after your while loop has completed, you can calculate how many extra columns you need with:
$remainder = (j % 3);
$columnsLeft = ($remainder == 0 ? 0 : 3 - $remainder);
$j = 1;
while($data=mysql_fetch_assoc($q))
{
if($j == 3)
{
echo "ADD A ROW";
$j = 0;
}
$j++;
}
this will done the things
I'm using explode() to basically take apart paragraphs into individual words. Works great. The looping through with a foreach(). Also works great. Nothing complicated here.
$title_pieces = explode(" ", $title_fixed);
foreach($title_pieces as $tpiece){
echo "<b>$tpiece<br>";
}
Unfortunately this returns just an ugly long list of words. What I'd like to do but can't quite figure out how is to put this all in a nice table. Creating the table is no problem, the part I can't figure out is how to get it to write more than one $tpiece per row. I'd like to have maybe 5 <td>s in each row.
So if I do:
foreach($title_pieces as $tpiece){
echo "<tr><td>$tpiece</td></tr>";
}
I'm still just left with a long list. Can someone point me in the right direction here.
Just a sample-code. Work around with the modulo-operator.
<?php
$i = 1;
echo '<table><tr>';
foreach($title_pieces as $tpiece){
if ($i % 10 == 0)
echo "</tr><tr>";
echo "<td>$tpiece</td>";
$i++;
}
echo '</tr></table>';
?>
Try something like:
$count = 1;
echo "<tr>";
foreach($title_pieces as $tpiece){
if ($count % NUM_COLS == 0)
echo "</tr><tr>";
echo "<td>$tpiece</td>";
$count++;
}
echo "</tr>";
There's probably a clever optimization in there. What you are doing is counting the number of cells and starting a new row when the count divides NUM_COLS evenly. It's important count starts on 1, not 0, or you'll have an empty row.