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

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

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

Looping through an array with line breaks every 6th element as a table?

I am trying to print an array so every 6th element is on a new line. I'm trying to do this in a table. I would prefer if it could be answered in PHP, but Javascripts Ok.
$array_control = array(
"1","5","6","2","1",
"2","1","6","4","3",
"3","2","5","6","6",
"4","3","1","5","4",
"6","4","2","3","6"
);
$arrayLength = count($array_control);
$i = 0;
while ($i < $arrayLength)
{
if ($i==0) {
echo "<tr>";
}
if (is_int($i/5)) {
echo '<td style="width:16%;">'.$array_control[$i].'</td>';
echo "</tr>";
echo "<tr>";
}else{
echo '<td style="width:16%;">'.$array_control[$i-1].'</td>';
}
$i++;
}
Any help would be great!
The %-operator (modulus) solves this for you:
echo '<table><tr>';
for($i = 1; $i <= count($array_control); $i++ ){
echo '<td style="width:16%;">';
if( ($i % 6) == 0){
echo $array_control[$i-1] . '</td></tr><tr>';
} else{
echo $array_control[$i-1] . '</td>';
}
}
echo '</tr></table>';
or if you feel sassy:
echo '<table><tr>';
for($i = 1; $i <= count($array_control); $i++ ){
echo '<td style="width:16%;">' . $array_control[$i-1], ( ($i % 6) == 0 ) ? '</td></tr><tr>' : '</td>';
}
echo '</tr></table>';
A for loop seems better, and maybe the mod operator is more clear.
echo '<tr>';
for ($i < = 0; i < $arrayLength; ++ $i) {
echo '<td style="width:16%;">'.$array_control[$i].'</td>';
if ($i % 6 == 5) {
echo '</tr>';
echo '<tr>';
}
}
echo '</tr>';
This isn't quite ideal, because you'll get an empty '' at the end, but the browser should hide that. A little more logic can be added if you need to eliminate that too.
Further, with the array structure you're showing, you could eliminate the $arrayLength variable entirely, and instead use:
foreach ($array_control as $i => $val) {
echo '<td style="width:16%;">'.$val.'</td>';

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.

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

foreach statement forcing while/for to loop too many times

I have a while/for loop with a foreach loop inside. Reading the code will make it self explanatory as to what I am trying to achieve in terms of logic.
In layman's terms, I am trying to create a calendar in PHP. The foreach statement creates 35 cells, for the calendar interface, 4 weeks, 7 days, 5 x 7 = 35.
The while loop has taken the number of days in April (30) and trying to each the range 1-30 in each cell.
However, when implementing the code below, each number gets iterated 30 times.
$i = 1;
while ($i <= 30){
foreach ($this->cells as $cells){
foreach ($cells as $key => $object){
echo "<td>" . $i . "</td>";
if($object % 7 == 0){
echo "</tr><tr>";
}
}
}
$i++;
}
I tried this:
$i = 1;
while ($i <= 30){
foreach ($this->cells as $cells){
foreach ($cells as $key => $object){
echo "<td>" . $i . "</td>"; $i++;
if($object % 7 == 0){
echo "</tr><tr>";
}
}
}
}
Which instead of outputting 1-30 many times each, it outputs 1-35, when I only want 1-30.
I have tried a for loop with the result it prints many of each number. For loop code:
for ($i = 1; $i <= 30; $i++){
foreach ($this->cells as $cells){
foreach ($cells as $key => $object){
echo "<td>" . $i . "</td>";
if($object % 7 == 0){
echo "</tr><tr>";
}
}
}
}
$this->cells is a range 1-35 in an array
$i=0;
foreach ($this->cells as $cells){
foreach ($cells as $key => $object){
if($i<=30)
echo "<td>" . $i . "</td>";
$i++;
if($object % 7 == 0){
echo "</tr><tr>";
}
}
}
Try this code
$i=0;
while ($i++ < 35){
echo '<td>' . ($i <= 30 ? $i : ' ') . '</td>';
if($i % 7 == 0) {
echo '</tr><tr>';
}
}
I'm not really sure why you need to do two loops, you could do it all with one (unless you do need to loop over $this->cells, in which case ignore this answer :p)

Categories