PHP Triangle of Images - php

I am having trouble figuring out how to create a triangle using 2 sets of images. It is relatively easy to do using *. Its roughly 40 lines of code and its abit ridiculous approaching it this way. I have tried looking online but haven't found a solution
for ($i = 0; $i < 1; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($j = 0; $j < 2; $j++) {
echo '<img src="Penguin.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 3; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 4; $i++) {
echo '<img src="Penguin.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 5; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 6; $i++) {
echo '<img src="Penguin.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 5; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 4; $i++) {
echo '<img src="Penguin.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 3; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 2; $i++) {
echo '<img src="Penguin.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 1; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}

Something like this? https://repl.it/EijH/5
Replace:
* with `<img src="Dinosaur.gif" style="width:50px;height:50px";/>`and
^ with `<img src="Penguin.gif" style="width:50px;height:50px";/>`
EDIT:
<!DOCTYPE html>
<html>
<body>
<?php
$i = 0;
$j = 0;
$rows = 15;
for($i = 1; $i <= $rows; $i++) {
for($j=1; $j <= $i; ++$j) {
if($i % 2 == 0) {
echo ($j % 2 == 0 ? "<img src='http://www.k6-geometric-shapes.com/images/prism-base-square.jpg' style='width:50px;height:50px';/>" : "<img src='http://www.k6-geometric-shapes.com/images/pyramid-base-square.jpg' style='width:50px;height:50px';/>");
} else {
echo ($j % 2 == 0 ? "<img src='http://www.k6-geometric-shapes.com/images/pyramid-base-square.jpg' style='width:50px;height:50px';/>" : "<img src='http://www.k6-geometric-shapes.com/images/prism-base-square.jpg' style='width:50px;height:50px';/>");
}
}
echo "<br>";
}
for($i = $rows; $i >= 1; $i--) {
for($j=$i; $j > 1; --$j) {
echo ($j % 2 == 0 ? "<img src='http://www.k6-geometric-shapes.com/images/prism-base-square.jpg' style='width:50px;height:50px';/>" : "<img src='http://www.k6-geometric-shapes.com/images/pyramid-base-square.jpg' style='width:50px;height:50px';/>");
}
echo "<br>";
}
?>
</body>
</html>

Related

Write a PHP code to print following number pattern

Write a PHP code to print following number pattern:
147
258
369
I am trying like this but its shows me shows below. how to convert column to row pattern.
<?php
$num = "";
for($i=1;$i<=9;$i++) {
$num .= $i;
if($i%3==0){
$num .="<br />";
}
}
echo $num;
?>
please help me
You need to use for loop inside for loop to achieve this. Below is the code
$num = "";
for( $i = 1; $i <= 3; $i++ ) {
for( $j = 0; $j <= 2; $j++) {
$k = $i + ( $j * 3 );
$num .= $k;
}
$num .= "<br />";
}
echo $num;
Here is another way to get this output.
for($i = 1; $i <= 3; $i++) {
$print = $i;
for($j = 1; $j <= 3; $j++) {
echo $print;
$print = $print + 3;
}
echo "<br />";
}

how to write a nested php loop that draws an image with few lines of code?

How best can i solve this with less code as possible below is the problem
*****
****x
***xx
**xxx
*****
**xxx
***xx
****x
*****
this is my code that i want to improve:
<?php
for ($i=0; $i < 5 ; $i++) {
if($i >= 1 & ($i <= 3))
{
for ($t=0; $t < 5-$i ; $t++)
echo "*";
for ($t=0; $t < $i ; $t++)
echo "x";
}
else
for ($j=0; $j < 5 ; $j++)
echo "*";
echo "<br/>";
}
for ($f=1; $f < 5 ; $f++) {
for ($j=0; $j < $f+1; $j++)
echo "*";
for ($v=3; $v>= $f; $v--)
echo "x";
echo "<br/>";
}
?>
To create string with repeated symbols you can use str_repeat. Using this function your code can be simplified to:
$num = 5;
for ($i = $num; $i > 1; $i--) {
echo str_repeat('*', $i) . str_repeat('x', $num - $i) . PHP_EOL;
}
echo str_repeat('*', $num) . PHP_EOL;
for ($i = 2; $i <= $num; $i++) {
echo str_repeat('*', $i) . str_repeat('x', $num - $i) . PHP_EOL;
}
Even if you cannot use php core functions, you can write you own function to create same results as str_repeat:
function createLine($starsCount, $XCount) {
$result = '';
for ($i = 0; $i < $starsCount; $i++) {
$result .= '*';
}
for ($i = 0; $i < $XCount; $i++) {
$result .= 'x';
}
return $result;
}
And rewrite code as:
$num = 5;
for ($i = $num; $i > 1; $i--) {
echo createLine($i, $num - $i) . PHP_EOL;
}
echo createLine($num, 0) . PHP_EOL;
for ($i = 2; $i <= $num; $i++) {
echo createLine($i, $num - $i) . PHP_EOL;
}

PHP Square of Images

I am trying to create a 10 x 10 square of gif images using PHP. This piece of code is one of doing it but it is insufficient code. I have tried using an if statement but has been ineffective. I am looking for another solution to this problem.
<?php
for ($i = 1; $i <= 10; $i++) {
echo '<img src="image1.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 1; $i <= 10; $i++) {
echo '<img src="image2.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 1; $i <= 10; $i++) {
echo '<img src="image1.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 1; $i <= 10; $i++) {
echo '<img src="image2.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 1; $i <= 10; $i++) {
echo '<img src="image1.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 1; $i <= 10; $i++) {
echo '<img src="image2.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 1; $i <= 10; $i++) {
echo '<img src="image1.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 1; $i <= 10; $i++) {
echo '<img src="image2.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 1; $i <= 10; $i++) {
echo '<img src="image1.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 1; $i <= 10; $i++) {
echo '<img src="image2.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
?>
simply you can use only single for loop
$j = 1;
for($i = 1; $i <= 100; $i++) {
if( $j % 2 == 0){
echo '<img src="cows.gif" style="width:50px;height:50px";/>';
}
else{
echo '<img src="chickens.gif" style="width:50px;height:50px";/>';
}
if($i % 10 == 0){
echo "<br>";
$j = $j + 1;
}
}

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

How to do nested loop in descending order?

What I want to get with for loop. Something will be like this.
*
**
***
****
*****
****
***
**
*
This thing I want to create right now I have this code.
<?php
$i = 1;
$end = 5;
$star = "*";
for($i; $i <= $end; $i++){
for($b=1; $b <= $i; $b++){
echo $star;
}
if($i == $end){
for($c=1; $c <= $end; $c++ ){
for($d=i; $d >= 2; $d--){
echo $star;
}
echo "<br>";
}
}
};
?>
And it's working fine with
*
**
***
****
*****
But then I need opposite loop first 4 then 3 then 2 then 1...
Where am I going wrong with this?
Just for fun. Try this:
$stars = 1;
for ($i = 0; $i < 9; $i++) {
for ($s = 1; $s <= $stars; $s++) {
echo "*";
}
echo "\n";
$stars += ($i<4)?1:-1;
}
And, for even more fun, one with just one for loop:
$stars = 0;
$starctr = 0;
for ($i = 0; $i < 25; $i++) {
echo "*";
if ($stars == $starctr) {
echo "\n";
$stars += ($i<14)?1:-1;
$starctr = 0;
} else {
$starctr++;
}
}
Can using nested for loop. Example:
$n = 5;
for($i = 1; $i <= $n; $i++){
for($j = 1; $j <= $i; $j++){
echo '*';
}
echo '<br />';
}
for($i = $n-1; $i >= 1; $i--){
for($j = $i; $j >= 1; $j--){
echo '*';
}
echo '<br />';
}
Another technique using array_fill(), array_map(), array_reverse()
$n = 5; $arr = array();
for($i = 1; $i <= $n; $i++){
$arr[] = array_fill(0, $i, '*');
}
array_map(function($v){echo join('', $v) . '</br>';},$arr);
unset($arr[count($arr) - 1]); //remove last index value
array_map(function($v){echo join('', $v) . '</br>';},array_reverse($arr));
<?php
for($i=0;$i<=6;$i++){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
for($i=5;$i>=1;$i--){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
?>
This can be achieved with only 2 for loops.
$offset = 1;
for ($i = 1; $i > 0; $i += $offset)
{
for($j = 1; $j <= $i; $j++){
echo '*';
}
echo '<br />';
if ($i === 5)
$offset = -1;
}
<?php
$i = 1;
$end = 5;
$star = "*";
for($i; $i <= $end; $i++){
for($b=1; $b <= $i; $b++){
echo $star;
}
};
for($c=$send; $c>=2; $c-- ){
for($d=$end; $d >= 2; $d--){
echo $star;
}
echo "<br>";
};
?>
Using a single for loop:
$end = 5;
$out = array();
for ($i=0;$i<$end;++$i)
{
$out[] = str_repeat('*', $i+1);
}
echo implode(PHP_EOL, $out).PHP_EOL;
array_pop($out);
$out = array_reverse($out);//remove last ****** <-- only one line with 5 stars
echo implode(PHP_EOL, $out).PHP_EOL;
Replace PHP_EOL with <br> if you want to, but this is the least loopy way to write this code I can think of
live demo
Here's a recursive attempt:
function ladder($n, $counter=1, &$acc=array())
{
if ($counter == ($n*2)) {
return implode($acc);
}
if ($counter <= $n) {
$acc[$counter] = str_repeat('*', $counter) . "\n";
$counter++;
}
if ($counter > $n) {
$diff = (int) $n-$counter;
$acc[$counter] = str_repeat('*', $n+$diff) . "\n";
$counter++;
}
return ladder($n, $counter, $acc);
}
print_r(ladder(5));

Categories