How to break a column to fix a php table [duplicate] - php

This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed last year.
I had a little problem, i cannot break the table because I'm looking for a 16 columns in these table to create a unicode table...
The aim is to find where i must break the line of the column as like as there:
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
enter image description here

You used the modulus in the first loop, so use it again in the second
print "</tr>";
print "<tr>";
for($i = 1; $i <= 50000; $i++){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>$i</td><td>$unicodeChar</td>\n";
if ( $i % $columnas/2 ) == 0 ) {
print "</tr><tr>\n";
}
}
print "</tr>\n";

Thank you very much for this help, i have been fixed!!
here is the code that i fixed, with your idea :)
´´´´
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
print "<tr>";
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
if (($i % 8 ) == 0 ){
print "</tr><tr>";
}
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
´´´´

Like Riggsfolly said but maybe divide the columns number by 2 before the modulo, because there are 2 TDs / iteration:
if ( $i % ($columnas/2) ) == 0 ) {
print "</tr><tr>\n";
}

Related

PHP Logic to generate bus seat arrangement

I am trying to generate a bus seat arrangement using PHP. What I want to achieve is shown in the image below
What I have so far is given below
I have the following script in place
$divide = 4;
$inside = 2;
for($i = 1; $i <= 45; $i++){
if($i % $divide == 1){
echo "<tr>";
}
echo "<td width='15%' align='center'>".$i."</td>";
if($i % $inside == 0 && $i % $divide != 0){
echo "<td width='40%'> </td>";
}
if($i % $divide == 0){
echo "</tr>";
}
}
As you can see that I have seat number from 1-42 showing correct, but I am stuck in the last row. There should be no gap in the last row. Can any one please help me correct my logic?
You can achieve this in 2 ways :
Way 1:
The hall way is not viewed when we're at the index = 42
A new line is created only if :
$i % $divide == 1 && $i != 45 is true
And closed if $i % $divide == 0 && $i != 44 is true
<?php
$divide = 4;
$inside = 2;
echo "<table border='1'>";
for($i = 1; $i <= 45; $i++){
if($i % $divide == 1 && $i != 45){
echo "<tr>";
}
echo "<td width='15%' align='center'>".$i."</td>";
if($i % $inside == 0 && $i % $divide != 0 && $i != 42){
echo "<td width='40%'> </td>";
}
if($i % $divide == 0 && $i != 44){
echo "</tr>";
}
}
echo "</table>";
Way 2:
This way is more simple:
Rows with and empty hall way are generated in the first for loop, while the last row is generated in another loop starting from position 41
<?php
$divide = 4;
$inside = 2;
echo "<table border='1'>";
for($i = 1; $i <= 40; $i++){
if($i % $divide == 1){
echo "<tr>";
}
echo "<td width='15%' align='center'>".$i."</td>";
if($i % $inside == 0 && $i % $divide != 0){
echo "<td width='40%'> </td>";
}
if($i % $divide == 0){
echo "</tr>";
}
}
echo "<tr>";
for ($i = 41; $i <= 45; $i++) {
echo "<td width='15%' align='center'>".$i."</td>";
}
echo "</tr>";
echo "</table>";
Based on comments, I suspect the picture is faulty and the question is how to "show" the empty seats (and aisle) in the last row.
Suggest you add another variable $maxseats = 45; run the loop an "even 4" times (ie from 1 to 48), and if $i > $maxseats print (echo) the placeholder.

How to do a Diamond Pattern / Shape (asterisks) inside a table? (html + php)

I have to make a diamond-shaped asterisk using for loop, inside a table. It has to have "blank" <td> spaces before and after the asterisks to move and make it look centered, so it looks like a diamond. How do I do that? (I used PHP inside an HTML code.)
Code without the <tr> and <td> tags, it looked like a diamond because it was center aligned:
<center>
<?php
echo "<table border = 1>";
// loop for the pyramid
for($i = 1; $i <= 10; $i += 2) {
for($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br />";
}
// loop for the inverted pyramid, so it looks like a diamond
for($i = 7; $i >= 1; $i -= 2) {
for($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br />";
}
echo "</table>";
?>
</center>
Code with the <tr> and <td> tags, need "spaces" for it to look like it's center aligned:
<?php
echo "<table border = 1>";
// loop for the pyramid
echo "<tr>";
for($i = 1; $i <= 10; $i += 2) {
echo "<tr>";
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
echo "</tr>";
}
echo "</tr>";
// loop for the inverted pyramid, so it looks like a diamond
for($i = 7; $i >= 1; $i -= 2) {
echo "<tr>";
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
echo "<br />";
echo "</tr>";
}
echo "</table>";
?>
Please help!
Here is new Code with your solution. I have added logic to put blank td forward and backward to *
<?php
echo "<table border = 1>";
// loop for the pyramid
echo "<tr>";
$max = $initAmount = 10;
for($i = 1; $i <= $initAmount; $i += 2) {
$max = $max -2;
$halfTD = (int)$max/2;
echo "<tr>";
for($b = 1; $b <= $halfTD; $b++){
echo "<td></td>";
}
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
for($b = 1; $b <= $halfTD; $b++){
echo "<td></td>";
}
echo "</tr>";
}
echo "</tr>";
// loop for the inverted pyramid, so it looks like a diamond
$max = $initAmount = 10;
for($i = 7; $i >= 1; $i -= 2) {
$max = $max -2;
$diff = $initAmount - $max;
$blankTd = $diff/2;
echo "<tr>";
for($b = 1 ; $b <= $blankTd; $b++){
echo "<td></td>";
}
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
for($b = 1 ; $b <= $blankTd; $b++){
echo "<td></td>";
}
echo "</tr>";
}
echo "</table>";
?>
I used the code below without using a table to make a diamond shape.
<div style="text-align: center">
<?php
$n = 8;
if($n === 1){ die("input must be greater than 1"); }
$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;
for($x = 1; $x <= $nn; $x++){
$temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
$total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);
echo nl2br(str_repeat('* ', $total) . "\r\n");
}
?>
I used the code below.
<div style="text-align: center">
<?php
$n = 8;
if($n === 1){ die("input must be greater than 1"); }
$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;
for($x = 1; $x <= $nn; $x++){
$temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
$total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);
echo nl2br(str_repeat('* ', $total) . "\r\n");
}
?>
<?php
for($i=0;$i<=5;$i++){
for($j=5;$j>=$i;$j--){
echo ' ';
}
for($k=0;$k<=$i;$k++){
echo '*';
}
echo '<br />';
}
for($i=0;$i<=4;$i++){
for($k=0;$k<=$i+1;$k++){
echo ' ';
}
for($j=4;$j>=$i;$j--){
echo '*';
}
echo '<br />';
}
?>

display first set of result in row

I want to iterate an array of data using foreach loop.
However, I want the first 4 results to display two items per row.Thereafter I want the rest of the data to display I item per row
i found a possible solution here
$data = range(1, 30);
for($count = 0; $count < count($data);)
{
echo "<tr>\n";
for($i = 0; $count < count($data) && $i < 2; $count++, $i++) {
echo "\t<td>$data[$count]</td>\n";
}
for(; $i < 2; $i++) {
echo "\t<td>-</td>\n";
}
echo "</tr>\n";
}
how the problem with this code is that it displays all the data in rows of two.i however only want the first 4 results to display that way.
You can do something like this:
$data = range(1, 30);
for($count = 0; $count < count($data); ++$count){
if($count < 4){
if($count % 2 == 0){
echo "<tr><td>" . $data[$count] . "</td>";
}else{
echo "<td>" . $data[$count] . "</td></tr>";
}
}else{
echo "<tr><td>" . $data[$count] . "</td></tr>";
}
}
Here's the demo
You're using the already incremented $i variable twice.
$i is never < 2
Add this instead
for($j = 0; $j < 2; $j++) {
echo "\t<td>-</td>\n";
}
Test here
Try with -
$data = range(1, 30);
for($count = 0; $count < count($data); $count++)
{
echo "<tr>";
if($count < 2) {
for($i = 1; $i <= 2; $i++) {
echo "<td>" . $data[$count] . "</td>";
if($i == 1) {
echo "</tr><tr>";
}
}
} else {
echo "<td>" . $data[$count] . "</td>";
}
echo "</tr>";
}
What about something like this?
echo '<table border=1>';
$data = range(1, 30);
for($count = 0; $count < count($data); $count++){
echo "<tr>\n";
if($count < 4) {
echo "\t<td>$data[$count]</td>\n";
$count++;
echo "\t<td>$data[$count]</td>\n";
} else {
echo "\t<td colspan=2>$data[$count]</td>\n";
}
echo "</tr>\n";
}
echo '</table>';

Creating a multiplication "grid"

I am trying to create a simple multiplication grid in PHP
It should be of the format for example for a 2x2 grid:
0 1 2
1 1 2
2 2 4
My issue is getting it to start from 0.
This is my nested for loop so far:
for($i=0;$i<=$_POST['rows'];$i++)
{
echo "<tr>";
for($j=0;$j<=$_POST['columns'];$j++)
{
if($i==0)
{
echo "<td>" . 1*$j . "</td>";
}
else
{
$mult = $i * $j;
echo "<td> $mult </td>";
}
}
echo "</tr>";
}
But it gives the output:
0 1 2
0 1 2
0 2 4
I need the column of 0's to be appropriate.
The way you're getting the top row of 0 1 2 3 is by that special-case on the X-axis. Do a similar special-case for the Y-axis ($j):
if ($i == 0) {
... 1 * $j ...
}
else if ($j == 0) {
... $i * 1 ...
}
else {
... $i * $j ...
}
You not only have $i==0 as special case but also $j==0:
if($i==0)
{
echo "<td>" . 1*$j . "</td>";
}
elseif($j==0)
{
echo "<td>" . $i*1 . "</td>";
}
else
{
$mult = $i * $j;
echo "<td> $mult </td>";
}
I don't understand the way you construct your grid. All you need is a row indicator and the number for the multiplication not a nested loop. Second: Why don't you start with 1 instead of catching the case inside the loop. This would be my variant of the multiplication “grid”
<?php
$rows = $_POST['rows'];
$number = $_POST['columns'];
for( $i=1; $i <= $rows; $i++) {
$mult = $i * $number;
echo "<tr>
<td>" . $i.'*'. $j . "</td>
<td>".$mult."</td>
</tr>";
}
?>
This would to a simple grid (x * y) = result. If you want a complete multiplication table it would be something like this:
<?php
$rows = $_POST['rows'];
$number = $_POST['columns'];
echo "<tr><th></th>";
for( $j=1; $j <= $number; $j++) {
echo "<th>".$j."</th>";
}
echo "</tr>";
for( $i=1; $i <= $rows; $i++) {
echo "<tr>";
echo "<th>".$i."</th>";
for( $j=1; $j <= $number; $j++) {
$mult = $i * $j;
echo "<td>".$mult."</td>";
}
echo "</tr>";
}
?>

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>";
?>

Categories