I have this php code:
$count = 8;
echo '<div style="background-color:green">';
for($i=1; $i<=$count; $i++) {
echo $i;
if($i%2 == 0) {
echo '</div><div style="background-color:green">';
}
}
echo '</div>';
What this will do is to wrap the numbers in a div with green background color every 2. I wanted to make something like in the snippet
<div style="background-color:green">12</div>
<div style="background-color:yellow">34</div>
<div style="background-color:green">56</div>
<div style="background-color:yellow">78</div>
how can i do this in php? please help. Thanks in advance =)
You can do this using if else condition . suppose if $i is odd then color yellow then other green . i have added some option which give you same result.
Option 1.
<?php
$count = 8;
for($i=1; $i<=$count; $i++) {
$color = $i%2 == 0 ? 'green' : 'yellow';
echo '<div style="background-color:'.$color.'">'.$i.'</div>';
}
Option 2.
<?php
$count = 8;
for($i=1; $i<=$count; $i++) {
if ($i%2 == 0) {
$color = 'green';
} else {
$color = 'yellow';
}
echo '<div style="background-color:'.$color.'">'.$i.'</div>';
}
Option 3.
<?php
$count = 8;
for($i=1; $i<=$count; $i++) {
$color = 'yellow';
if ($i%2 == 0) {
$color = 'green';
}
echo '<div style="background-color:'.$color.'">'.$i.'</div>';
}
You could achieve that effect doing something like this:
<?php
$count = 8;
$colorMatrix = ['odd'=>'green', 'even'=>'yellow'];
for($i=1; $i<=$count; $i++) {
$oddEven = ($i%2) ? "odd" : "even";
echo "<div style='background-color:{$colorMatrix[$oddEven]}'>{$i}</div>";
}
Or even much simpler and straightforward:
$count = 8;
for($i=1; $i<=$count; $i++) {
$oddEven = ($i%2) ? "green" : "yellow";
echo "<div style='background-color:{$oddEven}'>{$i}</div>";
}
To match your snippet, start at 12 and increment 22 on each iteration. changed to DRY for my fans.
<?php
$start = 12;
$end = 78;
for ($i=$start; $i<=$end; $i=$i+22) {
echo '<div style="background-color:'.($i%4 == 0 ? 'green' : 'yellow').'">'.$i.'</div>';
}
Result:
<div style="background-color:green">12</div>
<div style="background-color:yellow">34</div>
<div style="background-color:green">56</div>
<div style="background-color:yellow">78</div>
You could also achieve the same with using a foreach with a range():
foreach (range(12, 78, 22) as $i => $num) {
echo '<div style="background-color:'.($i%2 == 0 ? 'green' : 'yellow').'">'.$num.'</div>';
}
maybe this?
$count = 8;
for($i=1; $i<=$count; $i++) {
if($i%2 == 0) {
echo '</div><div style="background-color:green">'.$i;
}else{
echo '<div style="background-color:yellow">'.$i;
}
echo '</div>'
}
Related
I'm supposed to use nested for loops to create this shape: https://imgur.com/a/prh6zwj
This is what I currently have:
<?php
for ($x = 1; $x <= 10; $x++){
for ($y = 1; $y <= 6; $y++){
echo "Y";
}
}
?>
I have no clue what to do.
Thanks in advance!
<?php
$position = 1;
for ($x = 1; $x <= 11; $x++){
for ($y = 1; $y <= 6; $y++){
if ($y == $position) {
echo "Y";
} else {
echo "0";
}
}
if ($x < 6) {
$position++;
} else {
$position--;
}
echo "\n";
}
<?php
$length = 6; // change this to change height width
$pos = 0;
for ($x = 1; $x <= (($length*2)-1); $x++){
if($x <= $length)
{$pos = $pos+1; }
else
{$pos = $pos-1; }
for ($y = 1; $y <= $length; $y++){
if($y == $pos)
echo "Y";
else
echo "O";
}
echo "\n";
}
There are many possible ways to achieve this when I started programming I never cared about the code quality and just focused on the output. I have added two examples to help you understand it better!
<?php
//We have 6 columns & 11 rows so we need two loops one size of 11 and second size of 6
$counter = 1;
for ($i = 1; $i <= 11; $i++){
for ($j = 1; $j <= 6; $j++){
if ($j == $counter) {
echo "Y";
} else {
echo "O";
}
}
if ($i < 6) {
$counter++;
} else {
$counter--;
}
echo "<br/>";
}
echo "**************************** METHOD TWO ****************************";
//Following is not efficient But its also printing the same results
for ($i = 0 ; $i < 66 ; $i++){
if($i == 65)
{
echo "O";
break;
}
if($i % 6 == 0){
echo "<br/>";
}
if($i <= 36)
{
if ($i % 7 == 0){
echo "Y";
}else{
echo "O";
}
}else{
if ($i % 5 == 0){
echo "Y";
}else{
echo "O";
}
}
}
?>
$k=2; // for calculating position from backside
for($i=1;$i<=11;$i++) //for row
{
for($j=1;$j<=6;$j++) //column
{
if($j==$i && $i<=6) //logic for printing "Y" till the end of row
echo "Y";
else if($i>6 && $j==($i-$k)) //logic for printing "Y" in reversal order from the end of row
{
echo "Y";
$k+=2;
}
else
echo "O"; // filling rest places with "O"
}
echo"\n"; // jumping to new Row;
}
Hope you can understand it easily.
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 />';
}
?>
As you see I use this code to increment photos in a file, one by one :
<?php
for ($i = 1; $i <= $photonumber; $i++) {
if($i < 10){
echo '<div class="photos"><a target="_blank"><img src="../photosfile/' . $nom . '0' . $i . '.jpg"/></a></div>';
}
else
{
echo '<div class="photos"><a target="_blank"><img src="../photosfile/' . $nom . $i . '.jpg"/></a></div>';
}
}
?>
I would like to do the same but with php pages reading them from a specific directory like : ../phppages/
Any idea?
I tried but I missed something I guess and I would like to do it same way but with .php instead of .jpg.
Thank you very much.
Try this:
<?php
for ($i = $photonumber; $i >= 1; $i--) {
if($i < 10){
$i = "0".$i;
}
include("../phppages/page".$i.".php");
}
?>
Update: output by 3
<?php
$photonumber = 12;
for ($i = 0; $i < $photonumber; $i++) {
$j = $photonumber - $i;
if($j < 10){
$j = "0".$j;
}
if ($i % 3 === 0)
{
if ($i !== 0)
{
echo '</div>';
}
echo '<div>';
}
include("../phppages/page". $j.".php");
}
echo '</div>';
Update 2
To find the amount of files, that look like "page".$j.".php" in "../phppages" directory try this:
$files = glob("../phppages/page*.php");
$photonumber = count($files);
That would just be this:
for ($i = 1; $i <= $photonumber; $i++) {
include ('php-file-' . $i . '.php');
}
About having it 3 by 3, to be more precise, i want to create a fore each 3 .php file. Like
<?php
for ($i = 1; $i <= $photonumber; $i++) {
if($i < 10){
echo '<div>';
include("../phppages/page".$i.".php") x3;
echo '</div>';
}
?>
Then it become something like this :
div : 12.php + 11.php + 10.php , div : 09.php + 08.php + 07.php , div
:
06.php + 05.php + 04.php , div : 03.php + 02.php + 01.php
I have a foreach to run
for($i = 0; $i < 7; $i++){
echo "<div class='$_Vr'">" ,$i,"</div>" <p> </p>;
}
and have a test value
if ($_Vr == 'dd'){
echo 'yes';
}else{
echo 'no';
}
I understand I can do this way to get what I want
<?php
for($i = 0; $i < 7; $i++){
echo "<div class='$_Vr'">" ,$i,"</div><p>"?>
if ($_Vr == 'dd'){
echo 'yes';
}else{
echo 'no';
}
<?php </p>"; }?>
but I'd like to know if I can put another echo inside foreach ? like
for($i = 0; $i < 7; $i++){
echo "<div class='$_Vr'">" ,$i,"</div>" <p> " , test value here , "</p>;
}
Use the conditional (AKA ternary) operator:
for($i = 0; $i < 7; $i++){
echo "<div class='$_Vr'>$i</div> <p> " , ($_Vr == 'dd' ? "yes" : "no") , "</p>;
}
for($i = 0; $i < 7; $i++){
echo "<div class='$_Vr'>$i</div><p>".($_Vr == 'dd' ? 'yes': 'no')."</p>";
}
I don't want to display the numbers 3, 5 and 15. I want you to show when my program runs
1 2 three 4 five 6 7 ...
$var1 = 3;
$var2 = 5;
$var3 = 15;
for ($i=1; $i<=100; $i++) {
if($i<=3){
if (($i%$var1)==0)
echo 'Three' .'<br/>';
}
if($i<=5)
if (($i%$var2)==0) {
echo 'Five' .'<br/>';
}
if($i<=15)
if ((($i%$var3)==0)) {
echo 'ThreeFive' .'<br/>';
}
echo $i.'<br/>';
}
A rather crude, but working solution would be this:
$var1 = 3;
$var2 = 5;
$var3 = 15;
for( $i=0; $i<100; $i++ ) {
switch( $i ) {
case $var1: echo 'Three<br>'; break;
case $var2: echo 'Five<br>'; break;
case $var3: echo 'ThreeFive<br>'; break;
default: echo $i.'<br>';
}
}
check with this code
$exclude_array = array(3,5,15);
for($i = 0;$i < 100; $i++){
if(!in_array($i,$exclude_array))
echo $i;
}
A little bit more elegant solution...
$alter = array(3=>"Three<br>", 5=>"Five<br>", 15=>"ThreeFive<br>");
for($i=0; $i<100; $i++) echo isset($alter[$i]) ? $alter[$i] : $i;
I suppose you wanted to print Three and Five every time the $i is divisible by 3 or 5. Then you may alter it this way:
$alter = array(3=>"Three", 5=>"Five");
for($i=0; $i<100; $i++) {
$print_word = false;
foreach($alter as $key=>$val) if($i>0 && $i%$key==0) {
echo $val;
$print_word = true;
}
echo $print_word ? "<br>" : $i;
}
Try this-
$var1 = 3;
$var2 = 5;
$var3 = 15;
for ($i=1; $i<=100; $i++) {
if ($i==$var1)
echo 'Three' .'<br/>';
else if ($i==$var2) {
echo 'Five' .'<br/>';
}
else if ($i==$var3) {
echo 'ThreeFive' .'<br/>';
}
else
echo $i.'<br/>';
}
Try this one :)
<?php
$var1 = 3;
$var2 = 5;
$var3 = 15;
for ($i=1; $i<=100; $i++) {
if($i == $var1){
echo "three <br />";
}elseif($i == $var2){
echo "five <br />";
}elseif($i == $var3){
echo "Fifteen <br />";
}else{
echo $i."<br />";
}
}
?>