PHP - Dynamic 3 column result table - php

I have this script which produces a 3 column table of results:
$cols = 3;
$row = 0;
$column = 0;
$count = 0;
echo '<table>';
while($row = mysql_fetch_assoc($result)) {
$count++;
if ($column == 0) {
echo '<tr>';
}
echo "<td>$row['dbField']</td>";
$column++;
if ($column >= $cols) {
$row++;
echo '</tr>';
$column = 0;
}
}
echo '</table>';
This works okay, except if there's only 1 result, it only prints one cell. I would like it to finish off the row of 3, so in this case, two cells would be empty.
I have the total number of records stored in a session variable $_SESSION['r_count'] and thought it would be fairly simple to add the following snippet after the $column++ part of the above:
if ($count == $_SESSION['r_count'] && $column < $cols) {
echo '<td></td>';
$column++;
}
I thought wrong. Could anyone advice me on how to modify this correctly?

If you know the number of rows (i.e. mysql_num_rows()):
for ($i = 0, $n = ceil($nr_of_rows / $cols); $i != $n; ++$i) {
$row = mysql_fetch_assoc($result);
if ($i % $cols == 0) { echo '<tr>'; }
echo '<td>', $row ? htmlspecialchars($row['dbField']) : '', '</td>';
if (($i + 1) % $cols == 0) { echo '</tr>'; }
}

Related

PHP How to display some values of multidimensional array with for loop

I loop through a multidimensional array to echo the first five values of every "row" as a table. As far as that, it works perfectly:
"<table>";
for ($row = 0; $row < $index_number; $row++) {
echo "<tr>";
for ($col = 0; $col < 5; $col++) {
echo "<td>".$tablevalue[$row][$col]."</td>";
}
echo "</tr>";
}
echo"</table>";
Now I want to display numbers 1 to 3 and then 8 and 9 again. Neither one for loop inside another nor two seperate loops work the way I want them to.
Thats what I tried so far:
echo "<table>";
for ($row = 0; $row < $index_number; $row++) {
echo "<tr>";
for ($col = 0; $col < 3; $col++ && $col2 = 8; $col2 < 10; $col2++) {
echo "<td>".$tablevalue[$row][$col]."</td>";
}
echo "</tr>";
}
echo"</table>";
Any ideas on how to make it work?
For your inner loop just use a conditional, if you have integer indexes starting at 0:
foreach($tablevalue[$row] as $col => $val) {
if($col < 3 || ($col > 7 && $col < 10)) {
echo "<td>$val</td>";
}
}
However, as you can see foreach is much easier for the entire thing:
foreach($tablevalue as $row) {
echo "<tr>";
foreach($row as $col => $val) {
if($col < 3 || ($col > 7 && $col < 10)) {
echo "<td>$val</td>";
}
}
echo "</tr>";
}
If you don't have integer indexes starting at 0, then just foreach(array_values($tablevalue)... and foreach(array_values($row)... or you can slice what you want and implode:
foreach($tablevalue as $row) {
echo "<tr><td>";
echo implode("</td><td>", array_slice($row, 0, 3, true) +
array_slice($row, 7, 2, true));
echo "</td></tr>";
}

PHP Skip something a certain amout of times in loop

I have a ton of for loops for generating a table, with results from my MySQL DB. Some of the more important information are the start and end date. I calculate how many days this absence has besides the start date(For example 02.03.2015 - 04.03.2015, 2 days). I save this result in $difference, however I get this result during a for loop, so I cant simply go to the for loop and say $xyz - difference.
So I thought about skiping the <td>'s in the loop, as many times as the difference.
Here in the picture you see that the <td>'s are off the amount of days the absence lasts.
How can I skip them as many times as the difference, so the tables looks right again?
My code is still a mess so no comments about that:
echo '<table class="table table table-bordered table-striped table-hover">';
echo '<thead>';
for ($l = 0; $l < $days; $l++) {
if ($l == 0) {
echo '<th>', '<b>Day</b>', '</th>';
} else {
$date = "$current-$month-$l";
$date = date('D', strtotime($date));
$date = substr($date, 0, -1);
echo '<th class="center">', $date,'</th>';
}
echo "\n";
}
echo '</thead>';
echo '<tbody>';
for ($l = 0; $l < $days; $l++) {
if ($l == 0) {
echo '<td>', '<b>Onshore</b>', '</td>';
} else {
echo '<th class="center">', $l, '</th>';
}
echo "\n";
}
for ($i = 0; $i < $count_user; $i++) {
echo '<tr>';
$result = mysql_query("select start, end, type_FK, employee_FK FROM absences where employee_FK = {$array_user[$i]['employee_ID']} and MONTH(start) = $month and YEAR(start) = $current");
while ($row = mysql_fetch_assoc($result)) {
$array_absences[] = $row;
}
$count = 0;
if (!empty($array_absences)) {
$count = count($array_absences);
}
for ($j = 0; $j < $days; $j++) {
$true = 0;
if ($j == 0 && $i == $count_on) {
echo '<td>';
echo '<b>Offshore</b>';
echo '</td>';
for($k = 0; $k < $days -1; $k++){
echo '<td>';
echo '</td>';
}
echo '</tr>';
}
if ($j == 0) {
echo '<td>';
echo $array_user[$i]['name'], ' ', $array_user[$i]['surname'];
echo '</td>';
}
for ($k = 0; $k < $count; $k++) {
$array_absences[$k]['start'] = substr($array_absences[$k]['start'], -2);
$array_absences[$k]['end'] = substr($array_absences[$k]['end'], -2);
$array_absences[$k]['start'] = ereg_replace("^0", "", $array_absences[$k]['start']);
$array_absences[$k]['end'] = ereg_replace("^0", "", $array_absences[$k]['end']);
$difference = $array_absences[$k]['end'] - $array_absences[$k]['start'];
if ($j == $array_absences[$k]['start'] && $array_absences[$k]['employee_FK'] == $array_user[$i]['employee_ID']) {
$true = 1;
$result = mysql_query("select approved from absences where DAY(start) = $j and MONTH(start) = $month");
while ($row = mysql_fetch_assoc($result)) {
$approved[] = $row;
$n++;
}
$now = date('Y-m-d');
$absence = strtotime("$current/$month/$j");
$absence = date('Y-m-d',$absence);
for ($q = 0; $q < $difference+1; $q++) {
if ($approved[$n]['approved'] == 1) {
echo '<td class="center green">';
} elseif ($approved[$n]['approved'] == 0 && $now <= $absence) {
echo '<td class="center orange">';
} elseif ($approved[$n]['approved'] == 0 && $now > $absence) {
echo '<td class="center red">';
}
for ($l = 0; $l < $count_types; $l++) {
if ($array_absences[$k]['type_FK'] == $types[$l]['type_ID']) {
echo $types[$l]['short'];
}
}
echo '</td>';
}
}
}
//Days that are not absences
//Skip this the amounts of $difference
echo '<td ';
//If weekend special coloring
$date = "$current-$month-$j+1";
$date = new DateTime($date);
$day = $date->format("w");
if ($day == 6 || $day == 0) {
echo 'class = "weekend"';
}
echo '>';
echo '</td>';
echo "\n";
}
echo '</tr>';
}
I'm afraid I did not really get the sense of your explanations, however, to skip something in a loop, you can use the continue keyword for this.
For example :
for($i = 0; $i++; i<1000){
if($i < 200 && $i > 100)
continue;
// Computations are here....
}

Splitting MySql data in 3 columns if i have less than eight values

I want to display some data split into 3 columns. If I have more than eight values​​, then everything is working properly, but if I have less than eight values I get: Notice: Undefined offset:
How can I fix that problem?
echo '<table><tr>';
for ($i=0;$i < count($audio_fileexts) / 3; $i++) {
for ($j = 0; $j < 3; $j++){
echo $audio_fileexts[ $i + $j * 3];
}
echo '</tr><tr>';
}
echo '</tr></table>';
echo '<table>';
for ($i = 0; $i < count($audio_fileexts); $i++) {
if ($i % 3 == 0) {
if ($i > 0) {
echo '</tr>';
}
echo '<tr>';
}
echo '<td>' . $audio_fileexts[$i] . '</td>';
}
if ($i % 3 > 0) {
while ($i++ % 3 > 0) {
echo '<td></td>';
}
echo '</tr>';
}
echo '</table>';

Transform html table

Hi I have an array of movie names in alphabetical order, of which I want to create a html table of, I want to do this on the fly so I did the following:
echo "<div align=\"center\"><table>";
$i=0;
foreach ($results as $entry){
//If first in row of 4, open row
if($i == 0) {
echo "<tr>\n";
}
//print a cell
echo "\t<td>" . $entry . "</td>\n";
i++;
//if last cell in row of 4, close row
if($i == 4) {
echo "</tr>\n";
$i=0;
}
}
if($i < 4) {
while($i < 4) {
echo "\t<td></td>\n";
$i++;
}
echo "</tr>\n";
}
echo "</table></div>";
However this builds a table which looks like:
entry0 | entry1 | entry2 | entry3
entry4 | entry5 | entry6 | entry7
How can I go about building a table like:
entry0 | entry3 | entry6
entry1 | entry4 | entry7
entry2 | entry5 | entry8
?
I'm guessing I would have to reorganise my $results array and still build the table the same way?
I'm very new to php ( a week!) so I'm not really sure how to go about this
Thanks for your help
$results = array( 'e1', 'e2', 'e3', 'e4', 'e5', 'e6','e7' );
$NUM_COLUMNS = 3;
$numRows = count($results) / $NUM_COLUMNS;
if (count($results) % $NUM_COLUMNS > 0) {
$numRows += 1;
}
echo "<div align=\"center\"><table>";
$i=0;
for ($i = 0; $i < $numRows; $i++) {
echo "<tr>\n";
$index = $i;
for ($j = 0; $j < $NUM_COLUMNS; $j++) {
//print a cell
$entry = '';
if ($index < count($results)) {
$entry = $results[$index];
}
echo "\t<td>" . $entry . "</td>\n";
$index += $numRows;
}
echo "</tr>\n";
}
echo "</table></div>";
This is tested and includes sorting items vertically. I would write a description, but I just got a phone call and have to go. I will answer questions if you have any in ~1hr. (sorry!)
How about this: (I did not test, but should be OK)
<?php
$i = 1;
$max = 3; // this is the number of columns to display
echo "<div align=\"center\"><table><tr>";
foreach ($results as $entry) {
echo "<td style=\"text-align: center;\">";
echo $entry;
echo "</td>";
$i++;
if ($i == ($max)) {
echo '</tr><tr>';
$i = 1;
}
}
echo "</tr>\n";
echo "</table></div>";
?>

Insert tr after every third loop

I'm making a forum in PHP. I have to display all forum categories in a table, and to do so, I have used a while loop. However, I want to have only 3 td's in every table row. To loop through the categories, I'm using a while loop with the query, so I don't think I can use modulus here.
Why can't you use modulus? Just add a counter somewhere, and if it hits % 3 == 0 reset the counter and do your stuff.
You might need to do some extra if's for first and last and stuff like that, but there is no reason not to use a modulo with a while.
$i=0;
while(guard()){
if($i % 3 == 0){
//ploing
}
$i++
}
This code will close any extra rows:
<table>
<?php
$columns = 3;
$i = 0;
while($row = mysql_fetch_array($result)){
$i++;
//if this is first value in row, create new row
if ($i % $columns == 1) {
echo "<tr>";
}
echo "<td>".$row[0]."</td>";
//if this is last value in row, end row
if ($i % $columns == 0) {
echo "</tr>";
}
}
//if the counter is not divisible by the number of columns, we have an open row
$spacercells = $columns - ($i % $columns);
if ($spacercells < $columns) {
for ($j=1; $j<=$spacercells; $j++) {
echo "<td></td>";
}
echo "</tr>";
}
?>
</table>
I haven't tested the code, but the logic should work:
<Table>
<?php
$i = 0;
while($row = mysql_fetch_array($result)){
if($i == 0){
echo"<TR>";
}
echo"<td>".$row[0]."<TD>";
$i++;
if($i == 3)
{
$i = 0;
echo"</tr>"
}
}
if($i ==1){
echo "<td></td><td></td></tr>";
}
if($i ==2)
{
echo "<td></td></tr>";
}
?>
<table>

Categories