PHP Logic to generate bus seat arrangement - php

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.

Related

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

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

I'd like to create a table with PHP, but I don't know how to do it

I have to create a table with the following values:
Table
I guess create this table but not with these values. I have a form that I have to enter a number for example 5 and I have to create a table with 5 rows and 5 columns then I have another value in the form that is used to check if the number is a multiple of the number entered in the box.
This is my code:
<html>
<head>
<meta charset="UTF-8">
<title>Form 1</title>
</head>
<body>
<table border="1">
<?php
if (isset($_POST["length"]) && isset($_POST["multiples"])) {
$multiples = $_POST["multiples"];
$length = $_POST["length"];
for ($i = 1; $i <= $length; $i++) {
echo "<tr>";
for ($j = 1; $j <= $length; $j++) {
if ($j % 2 == 0) {
echo "<td bgcolor=red>$j</td> ";
} else {
echo "<td bgcolor=yellow>$j</td> ";
}
}
}
echo "</tr>";
} else {
echo <<<EOT
<form method="post" action="forms1.php">
<label for="logitud">Side length : </label>
<input type="number" name="length" >
<label for="multiples">Multiples: </label>
<input type="number" name="multiples" >
<input type="submit" value="Send">
</form>
EOT;
}
?>
</table>
</body>
</html>
This is the result:
Code result
If someone can help me I would really appreciate it!
Ultimately what you are looking to do is make a square that is $length by $multiples, so in order to do that you want to take into account $length * $multiples.
There are ways to do this with an inner for loop as you are doing, however since you are already familiar with % (MOD) you can simplify things a bit.
(Also, whenever you are given an assigment/test/whatever like this, ignore the whole form stuff and work on the logic first. Once that is good, you can easily add in the form part.)
<table border="1">
<?php
$multiples = 5;
$length = 5;
// Loop from 1 to 25 (or whatever L * M equals)
for ($i = 1; $i <= ($length * $multiples); $i++) {
// We are using a one-based start, so whenever our MOD works out to one
// here we are at the start of a row
if (1 === $i % $multiples) {
echo "<tr>";
}
// Same logic for coloring
if (0 === $i % 2) {
echo "<td bgcolor=red>$i</td> ";
} else {
echo "<td bgcolor=yellow>$i</td> ";
}
// Being one-based, when our MOD is zero we are at the end of a row
if (0 === $i % $multiples) {
echo "</tr>";
}
}
?>
</table>
(edit)
Here's a version that uses two loops and calculates the cell's value each time. The $k logic is a little complex but hopefully makes sens.
$multiples = 5;
$length = 5;
for ($i = 1; $i <= $length; $i++) {
echo "<tr>";
for ($j = 1; $j <= $multiples; $j++) {
$k = (($i - 1) * $length) + $j;
if (0 === $k % 2) {
echo "<td bgcolor=red>$k</td> ";
} else {
echo "<td bgcolor=yellow>$k</td> ";
}
}
echo "</tr>";
}
More people would probably keep track of a counter independently:
$multiples = 5;
$length = 5;
$k = 0;
for ($i = 1; $i <= $length; $i++) {
echo "<tr>";
for ($j = 1; $j <= $multiples; $j++) {
if (0 === $k % 2) {
echo "<td bgcolor=red>$k</td> ";
} else {
echo "<td bgcolor=yellow>$k</td> ";
}
$k++;
}
echo "</tr>";
}

How can I skip the first if statement occuring in a loop?

I have made a loop to print table <tr> and <td>
Here is my code:
echo "<tr>";
for($i = 0; $i < (int)count($fieldvalues); $i++){
echo "<td>" . $fieldvalues[$i] . "</td>";
if($i % 4 == 0){
echo "<td><input type='text'></td><td><input type='submit'
value='Add to cart'></td></form></tr>";
}
}
I want to skip the first if statement in the loop because the condition $i % 4== 0 is true when $i is 0, that is, 0 % 4 == 0.The value of $fieldvalues is 8. Any other method to overcome this is much appreciated.
Check that it is not 0 in the conditional.
if(!empty($i) && $i % 4 == 0){
or
if($i != 0 && $i % 4 == 0){
I'd also use a foreach rather than for.
A demo: https://3v4l.org/bG2NS
Add this code at starting of for loop
if($i == 0)
continue;
This skip first iteration of you loop
your code will be
echo "<tr>";
for($i = 0; $i < (int)count($fieldvalues); $i++){
if($i == 0)
continue;
echo "<td>" . $fieldvalues[$i] . "</td>";
if($i % 4 == 0){
echo "<td><input type='text'></td><td><input type='submit'
value='Add to cart'></td></form></tr>";
}
}

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 />';
}
?>

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

Categories