count html table columns inside loop - php

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

Related

php query if statement within while statement

I have the following code which with the following variables set numberofColumns = 2 and numberArticles = 10, will create 2 columns for articles, the article order is from left to right (column1 to column2) going down the page.
Id just like to add some code </div><div class="whatever"> after every 2nd article.
Any help would be much appreciated.
if ($numberColumns >= 1) {
$columnArticles = intval(($numberArticles + $numberK2Articles) / $numberColumns);
}
$columns = array();
for($columnIndex = 0; $columnIndex < $numberColumns; $columnIndex++) {
$columns[$columnIndex] = '<div class="column col-' . ($columnIndex + 1) . '">';
}
$articleIndex = 0;
while($articleIndex < count($articles)) {
foreach ($columns as $columnIndex => $column) {
if (isset($articles[$articleIndex])) {
$columns[$columnIndex] .= modCTRandomArticleHelper::getArticleHtml($params, $articles, $articleIndex);
$articleIndex++;
}
}
}
for($columnIndex = 0; $columnIndex < $numberColumns; $columnIndex++) {
echo $columns[$columnIndex] . '</div>';
}
I've always used the Modulus operator (%) to determine if my current index was one of n rows. $index % 2 returns 0 for every second row. $index % 3 returns 0 for every third row, and so on.
http://php.net/manual/en/internals2.opcodes.mod.php
The first comment on that page suggests that a bitwise operation is more efficient when you just need to determine odd or even. So, $index & 1 (odd) is a faster alternative to !$index % 2 (odd).

Even distribution of PHP arrays across columns

So, I want to distribute evenly lists across 3 columns. The lists cannot be broken up or reordered.
At the moment, I have 5 lists each containing respectively 4, 4, 6, 3 and 3 items.
My initial approach was:
$lists = [4,4,6,3,3];
$columns = 3;
$total_links = 20;
$items_per_column = ceil($total_links/$columns);
$current_column = 1;
$counter = 0;
$lists_by_column = [];
foreach ($lists as $total_items) {
$counter += $total_items;
$lists_by_column[$current_column][] = $total_items;
if ($counter > $current_column*$links_per_column) {
$current_column++;
}
}
Results in:
[
[4],
[4,6],
[3,3]
]
But, I want it to look like this:
[
[4,4],
[6],
[3,3]
]
I want to always have the least possible variation in length between the columns.
Other examples of expected results:
[6,4,4,6] => [[6], [4,4], [6]]
[4,4,4,4,6] => [[4,4], [4,4], [6]]
[10,4,4,3,5] => [[10], [4,4], [3,5]]
[2,2,4,6,4,3,3,3] => [[2,2,4], [6,4], [3,3,3]]
Roughly what you need to do is loop over the number of columns within your foreach(). That will distribute them for you.
$numrows = ceil(count($lists) / $columns);
$thisrow = 1;
foreach ($lists as $total_items) {
if($thisrow < $numrows){
for($i = 1; $i <= $columns; $i++){
$lists_by_column[$i][] = $total_items;
}
}else{
//this is the last row
//find out how many columns need to fit.
//1 column is easy, it goes in the first column
//2 columns is when you'll need to skip the middle one
//3 columns is easy because it's full
}
$thisrow++;
}
This will be an even distribution, from left to right. But you actually want a modified even distribution that will look symmetrical to the eye. So within the foreach loop, you'll need to keep track of 1.) if you're on the last row of three, and 2.) if there are 2 remainders, to have it skip col2 and push to col3 instead. You'll need to set that up to be able to play around with it,...but you're just a couple of logic gates away from the land of milk and honey.
So, I ended up using this code:
$lists = [4,4,6,3,3];
$columns = 3;
$total_links = 20;
$items_per_column = ceil($total_links/$columns);
$current_column = 1;
$lists_by_column = [];
for ($i = 0; $i < count($lists); $i++) {
$total = $lists[$i];
$lists_by_column[$current_column][] = $lists[$i];
//Loop until reaching the end of the column
while ($total < $items_per_column && $i+1 < count($lists)) {
if ($total + $lists[$i+1] > $items_per_column) {
break;
}
$i++;
$total += $lists[$i];
$lists_by_column[$current_column][] = $lists[$i];
}
//When exiting the loop the last time we need another break
if (!isset($lists[$i+1])) {break;}
//If the last item goes onto the next column
if (abs($total - $items_per_column) < abs($total + $lists[$i+1] - $items_per_column)) {
$current_column++;
//If the last item goes onto the current column
} else if ($total + $lists[$i+1] > $items_per_column) {
$i++;
$lists_by_column[$current_column][] = $lists[$i];
$current_column++;
}
}

Random Numbers, Column and Row. PHP

Please excuse me as this is my first post and I am fairly new to any type of programming. I hope my question is clear, I am using Excel references as I think this explains what I am trying to do best.
I am trying to generate random numbers for a pool. I have 8 rows of numbers and each row contains 10 spots, 0 to 9. I want to have a random number in each row and make sure the the number does not repeat in each row.
Example Grid - 8 columns wide x 10 rows long.
I am repeating this scrip for each column, but I am getting the same number is rows and I want make sure that does not happen.
for ($i=1; $i<=10; $i++) {
while (1) {
$duplicate = 0;
$num=rand(0,9);
for ($x=1; $x<$i; $x++) {
if ($NFC1[$x]==$num) { $duplicate = 1; }
}
if ($duplicate==0) {
$NFC1[$i]=$num;
break;
}
}
}
This is the results, as you can see I have random numbers is each column but not in each row.
"4";"8";"5";"5";"0";"4";"2";"7"
"5";"9";"4";"3";"9";"9";"9";"0"
"9";"5";"1";"1";"5";"8";"6";"1"
"7";"4";"6";"2";"6";"7";"3";"3"
"2";"6";"8";"4";"7";"2";"7";"5"
"0";"1";"0";"7";"2";"1";"4";"6"
"1";"7";"9";"9";"4";"3";"0";"4"
"3";"0";"3";"0";"3";"5";"5";"9"
"8";"2";"7";"8";"1";"6";"8";"2"
"6";"3";"2";"6";"8";"0";"1";"8"
The answer from here addapted to non-square array may looks like below:
$rows = 10; // Number of rows
$columns = 8; // Number of columns
$row = range(0, $columns-1);
$column = range(0, $rows-1);
shuffle($row);
shuffle($column);
// Create an array
foreach ($row as $x => $value)
foreach ($column as $y)
$array[$y][$x] = $value++ % max($rows, $columns);
And if you want to see the result:
foreach($array as $r) {
foreach($r as $number) {
echo $number.' ';
}
echo "<br/>";
}

How to use % (modulus) in PHP to pick out every 1st, 2nd and 3rd result?

I am using a PHP while loop in order to get data from a database. Ignoring the data that I'm retrieving from the database and concentrating on the amount of records I have fetched, which is 4.
I want to be able to take the 1st record and insert it into a div called "div1" and take the 2nd record and insert it into a div called "div2", take the 3rd record and insert it into a div called "div3".
When it comes to the 4th record I would like to insert it in to the div called "div1" and so on.
I've managed to get every 3rd result, so skipping the 1st and 2nd record and being able to do it with the 3rd using a snippet from the below code:
if ($i % 3 < 2) {
I'm having trouble adapting that to do what I want it to do.
What you want is some variation of
$targetDivNumber = ($i % 3) + 1;
See it in action.
Try something like (pseudo code)
if ($i % 3 = 0) {put in div1}
if ($i % 3 = 1) {put in div2}
if ($i % 3 = 2) {put in div3}
With below code you can do this too:
$cont = 1;
while( fetch results from query )
{
if($cont == 1)
put in div 1;
else if($cont == 2)
put in div 2;
else if($cont == 3)
put in div3;
$cont = $cont <= 3 ? $cont += 1 : 1;
}

Dynamic Table Layout using PHP Logic

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.

Categories