Someone please help me to create a two triangle patter using PHP. I'm already code but the output didn't as expected below.
expected output
My code:
function generatePattern($num) {
for ($id1 = 0; $id1 <= $num; $id1 = $id1 + 1) {
for ($id2 = $num; $id2 >= $id1; $id2 = $id2 - 1) {
print(' ');
}
for ($id3 = 1; $id3 <= $id1; $id3 = $id3 + 1) {
if ($id3 % 4 == 3) {
echo "o";
} else if ($id3 % 2 == 1) {
echo "x";
} else {
echo " ";
}
}
echo "\n";
}
for ($id1 = 0; $id1 <= $num-1; $id1 = $id1 + 1) {
echo str_repeat(' ', $num - 1);
for($id3 = $num-1; $id3 >= $id1; $id3 = $id3 - 1){
if ($id3 % 4 == 3) {
echo "o";
} else if ($id3 % 2 == 1) {
if ($id1 % 4 == 3) {
echo "o";
} else if ($id1 % 2 == 0) {
echo " ";
} else if ($id1 % 2 == 1) {
echo "x";
} else {
echo "x";
}
} else if ($id3 == $id1){
echo "x";
} else {
echo " ";
}
}
echo "\n";
}
}
generatePattern(4);
And my current output like this (the bottom triangle still messed up)
output
Do the required changes for space between o and x
function generatePattern($num) {
if($num % 2 == 0)
{
$num1 = $num + 1;
}else{
$num1 = $num;
$num = $num - 1;
}
for ($id1 = 1; $id1 <= $num; $id1++) {
for ($id2 = $num; $id2 >= $id1; $id2--) {
print(' ');
}
for ($id3 = 1; $id3 <= $id1; $id3++) {
if ($id3 % 4 == 3) {
echo "o";
} else if ($id3 % 2 == 1) {
echo "x";
} else {
echo " ";
}
}
echo "\n";
}
$str = str_repeat('x o ', ceil(($num1*2)/4));
echo substr($str, 0, $num1*2);
echo "\n";
$j = $num;
for($id1 = $num; $id1 >=1; $id1 = $id1 - 2)
{
for($id2 = 2; $id2 >= 1; $id2--)
{
if($j % 2 == 0)
{
$pattern = [' ', 'x', ' ', 'o',];
}else{
$pattern = [' ', 'o', ' ', 'x',];
}
echo str_repeat(' ', ($id2%2 == 0) ? $num: $num - 1);
$design = implode('', $pattern);
do{
$design .= implode('', $pattern);
}while(strlen($design) < $id1);
echo substr($design, 0, $id1);
echo "\n";
}
$j--;
}
}
generatePattern(14);
Related
When my code is moved from local server to live server it shows an error like this :
Fatal error: Call to undefined method DateTime::diff()
Code:
<?php
date_default_timezone_set('Asia/Calcutta');
$sFinalDate = date('Y-m-d', strtotime($sDate));
$sNow = new DateTime();
$iRemain = new DateTime( $sFinalDate.$sTime);
$iInterval = $iRemain->diff($sNow);
$sTimeCounter = $iInterval->format("%h: %i :%s ");
$sCalculate = $iInterval->format("%a:%h:%i");
?>
Though I found a number of people who ran into the issue of 5.2 and
lower not supporting this function, I was unable to find any solid
examples to get around it. Therefore I hope this can help some others:
<?php
function get_timespan_string($older, $newer) {
$Y1 = $older->format('Y');
$Y2 = $newer->format('Y');
$Y = $Y2 - $Y1;
$m1 = $older->format('m');
$m2 = $newer->format('m');
$m = $m2 - $m1;
$d1 = $older->format('d');
$d2 = $newer->format('d');
$d = $d2 - $d1;
$H1 = $older->format('H');
$H2 = $newer->format('H');
$H = $H2 - $H1;
$i1 = $older->format('i');
$i2 = $newer->format('i');
$i = $i2 - $i1;
$s1 = $older->format('s');
$s2 = $newer->format('s');
$s = $s2 - $s1;
if($s < 0) {
$i = $i -1;
$s = $s + 60;
}
if($i < 0) {
$H = $H - 1;
$i = $i + 60;
}
if($H < 0) {
$d = $d - 1;
$H = $H + 24;
}
if($d < 0) {
$m = $m - 1;
$d = $d + get_days_for_previous_month($m2, $Y2);
}
if($m < 0) {
$Y = $Y - 1;
$m = $m + 12;
}
$timespan_string = create_timespan_string($Y, $m, $d, $H, $i, $s);
return $timespan_string;
}
function get_days_for_previous_month($current_month, $current_year) {
$previous_month = $current_month - 1;
if($current_month == 1) {
$current_year = $current_year - 1; //going from January to previous December
$previous_month = 12;
}
if($previous_month == 11 || $previous_month == 9 || $previous_month == 6 || $previous_month == 4) {
return 30;
}
else if($previous_month == 2) {
if(($current_year % 4) == 0) { //remainder 0 for leap years
return 29;
}
else {
return 28;
}
}
else {
return 31;
}
}
function create_timespan_string($Y, $m, $d, $H, $i, $s)
{
$timespan_string = '';
$found_first_diff = false;
if($Y >= 1) {
$found_first_diff = true;
$timespan_string .= pluralize($Y, 'year').' ';
}
if($m >= 1 || $found_first_diff) {
$found_first_diff = true;
$timespan_string .= pluralize($m, 'month').' ';
}
if($d >= 1 || $found_first_diff) {
$found_first_diff = true;
$timespan_string .= pluralize($d, 'day').' ';
}
if($H >= 1 || $found_first_diff) {
$found_first_diff = true;
$timespan_string .= pluralize($H, 'hour').' ';
}
if($i >= 1 || $found_first_diff) {
$found_first_diff = true;
$timespan_string .= pluralize($i, 'minute').' ';
}
if($found_first_diff) {
$timespan_string .= 'and ';
}
$timespan_string .= pluralize($s, 'second');
return $timespan_string;
}
function pluralize( $count, $text )
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
?>
source http://php.net/manual/en/function.date-diff.php
if you using php 5.3 then there would be another issue
working above example on php>=5.3
I try to implement a pagination to my website in php
This is what I want
If I have 42 Pages and I am on the page 6
[1][2][3][4][5][6][7][8][9][10][20][30][40][42]
or I am on the page 23
[1][10][20][21][22][23][24][25][26][27][28][29][30][40][42]
This what I did
$nbPage = 42;
$actualPage = 23;
$min_part_pagination = floor($actualPage / 10)*10;
$max_part_pagination = ceil($actualPage / 10 )*10;
$last_part_pagination = floor($nbPage /10 )*10;
$first_part_pagination = 10;
$count = 1;
for($j = 1 ; $j <= $nbPage ; $j++){
if($j % 10){
if($pageActuelle == $count){
$max_part_pagination = ($max_part_pagination < $nbPage) ? $max_part_pagination : $nbPage+1;
for($k = $min_part_pagination+1 ; $k < $max_part_pagination ; $k++ ){
if($k == 1){
echo "<a href=http://dev.blablabl.fr/public_html/>".$k."</a> ";
}else{
echo "".$k." ";
}
}
}
elseif($count == $nbPage && $pageActuelle < $last_tranche_pagination ){
echo "".$count." ";
}elseif($count == 1 && $pageActuelle >= $first_tranche_pagination ){
echo "<a href=http://dev.blablabl.fr/public_html/>1</a> ";
}
}else{
if($pageActuelle == $count){
if($max_part_pagination == $max_part_pagination ){
$max_part_pagination += 10;
}else{
$max_part_pagination = $last_tranche_pagination;
}
echo 'min_part_pagination > '.$min_part_pagination.'<br />';
echo 'max_part_pagination > '.$max_part_pagination.'<br />';
for($k = $min_part_pagination ; $k < $max_part_pagination ; $k++){
echo "".$k." ";
}
}else{
echo "".$count." ";
}
}
My problem is If I am on the page 41 It show [40][41][42][43][44][45][46][47][48][49][50]
It doesn't stop at 42
So I'm looking how to make a checkboard but with a spiral in it instead of the default checkerboard made like this:
$checkerboard=array();
for($row=0;$row<10;$row++){
if($row%2==0){
for($col=0;$col<10;$col++){
if($col%2==0){
$checkerboard[$row][$col]="white";
}else{
$checkerboard[$row][$col]="black";
}
}
}else{
for($col=0;$col<10;$col++){
if($col%2==0){
$checkerboard[$row][$col]="black";
}else{
$checkerboard[$row][$col]="white";
}
}
}
}
I also tried it with 2 diagonals like this:
$diagonal=array();
for($row=0;$row<10;$row++){
for($col=0;$col<10;$col++){
if($row==$col){
$diagonal[$row][$col]='black';
}else{
$diagonal[$row][$col]='white';
}
if($row+$col==9){
$diagonal[$row][$col]='black';
}
}
}
And then echo'd simply like this:
echo "<table>";
for($row=0;$row<count($checkerboard);$row++){
echo "<tr>";
for($col=0;$col<count($checkerboard);$col++){
echo "<td width='50px' height='50px' bgcolor='".$checkerboard[$row][$col]."'></td>";
}
echo "</tr>";
}
I'd like to keep the code simple because I've not been coding php for a very long time and it has to work with an array.
I tried this here:
$spiral=array();
for($row=0;$row<10;$row++){
for($col=0;$col<10;$col++){
$spiral[$row][$col]='white';
if($row==0 or $row==9 or $col==0 or $col==9){
$spiral[$row][$col]='black';
}if($row==1 and $col==0){
$spiral[$row][$col]='white';
}if($row==2 and $col<8){
$spiral[$row][$col]='black';
}if($row>1 and $row<8 and $col==7){
$spiral[$row][$col]='black';
}if($row==7 and $col>1 and $col<8){
$spiral[$row][$col]='black';
}if($row>3 and $row<7 and $col==2){
$spiral[$row][$col]='black';
}if($row==4 and $col>2 and $col<6){
$spiral[$row][$col]='black';
}if($row==5 and $col==5){
$spiral[$row][$col]='black';
}
}
}
But if the checkerboard becomes bigger it will be very hard to change. It there a way to make it easier?
Try this out:
I create an empty board then start drawing horizontal & vertical lines, starting from the edges each time. The code might need some tweaking but it's a good start
$checkerboard=array();
$size = 12;
for ($row=0; $row<$size; $row++) {
for ($col=0; $col<$size; $col++) {
$checkerboard[$row][$col]="red";
}
}
//horizontal
$pair = 0 ;
while ($pair < (int) $size / 2) {
//drawing top half rows
$row = 2 * $pair;
$end = min($row, $size - $row);
$start = $end - 2;
for ($col = $start; ($col < $size - $end) && ($row < $size / 2); $col++){
$checkerboard[$row][$col]="black";
}
//drawing bottom half rows
$far_row = $size - 1 - 2 * $pair;
$end = min($far_row, $size - $far_row) + 1 - 2;
$start = $end ;
for ($col = $start; ($col < $size - $end) && ($far_row > $size / 2 ); $col++){
$checkerboard[$far_row][$col]="black";
}
$pair++;
}
$pair = 0;
//vertical
while ($pair < (int) $size / 2) {
//drawing left half columns
$col = 2 * $pair;
$end = min($col, $size - $col);
$start = $end +2 ;
for ($row = $start; ($row < $size - $end) && ($col < $size / 2); $row++){
$checkerboard[$row][$col]="black";
}
//drawing right half columns
$far_columns = $size - 1 - 2 * $pair;
$end = min($far_columns, $size - $far_columns) - 1;
$start = $end ;
for ($row = $start; ($row < $size - $end) && ($far_columns >= ($size / 2 ) ); $row++){
$checkerboard[$row][$far_columns]="black";
}
$pair++;
}
echo "<table>";
for($row=0;$row< $size;$row++){
echo "<tr>";
for($col=0; $col< $size; $col++){
echo "<td width='50px' height='50px' bgcolor='".$checkerboard[$row][$col]."'></td>";
}
echo "</tr>";
}
You can just change the $size variable for different dimensions
Here is my attempt at the problem:
function buildSpiral($gridSize)
{
/**
* Origin is at the top left handcorner
*/
$x = 0;
$y = 0;
$xMin = 0;
$xMax = $gridSize-1;
$yMin = 2;
$yMax = $gridSize-1;
$pattern = [];
$size = $gridSize;
$collision = function($p, $limit) {
return (bool) ($p == $limit);
};
// increment x
$shadeRight = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x++, $xMax));
if ($x >= $xMax) {
$x=$xMax;
}
$xMax-=2;
};
// increment y
$shadeDown = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision) {
while ($y < $yMax && $yMin > $yMax) {
$pattern[++$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($y++, $yMax));
if ($y >= $yMax) {
$y = $yMax;
}
$yMax-=2;
};
// decrement x
$shadeLeft = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision, $gridSize) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x--, $xMin));
if ($x < $xMin) {
$x=$xMin;
}
$xMin+=2;
};
// decrement y
$shadeUp = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision, $gridSize) {
while ($y > $yMin && $yMin > $yMax) {
$pattern[--$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while(!$collision(--$y, $yMin));
if ($y < $yMin) {
$y = $yMin;
}
$yMin+=2;
};
while ($size > 0) {
$shadeRight($x, $y);
$shadeDown($x, $y);
$shadeLeft($x, $y);
$shadeUp($x, $y);
$size-=2;
}
return $pattern;
}
for ($i = 1; $i <= 25; $i++) {
$checkboard = buildSpiral($i);
echo "<h1>$i</h1>";
echo "<table style='margin-bottom: 2em;'>";
for($row=0;$row<count($checkboard);$row++){
echo "<tr>";
for($col=0;$col<count($checkboard);$col++){
if (!isset($checkboard[$row][$col])) {
echo "<td width='50px' height='50px' bgcolor=\"red\"></td>";
} else {
echo "<td width='50px' height='50px' bgcolor=\"black\"></td>";
}
}
echo "</tr>";
}
echo "</table>";
}
I stop each shading direction when a limit is hit.
Update Let's say I want the spirals to start from the top right corner, then we just need to set the new origin and call the shaders in the way we want the spiral to go like so:
function buildSpiral($gridSize)
{
/**
* Origin is at the top left handcorner
*/
$x = $gridSize-1;
$y = 0;
$xMin = 0;
$xMax = $gridSize-1;
$yMin = 2;
$yMax = $gridSize-1;
$pattern = [];
$size = $gridSize;
$collision = function($p, $limit) {
return (bool) ($p == $limit);
};
// increment x
$shadeRight = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x++, $xMax));
if ($x >= $xMax) {
$x=$xMax;
}
$xMax-=2;
};
// increment y
$shadeDown = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision) {
while ($y < $yMax && $yMin > $yMax) {
$pattern[++$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($y++, $yMax));
if ($y >= $yMax) {
$y = $yMax;
}
$yMax-=2;
};
// decrement x
$shadeLeft = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision, $gridSize) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x--, $xMin));
if ($x < $xMin) {
$x=$xMin;
}
$xMin+=2;
};
// decrement y
$shadeUp = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision, $gridSize) {
while ($y > $yMin && $yMin > $yMax) {
$pattern[--$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while(!$collision(--$y, $yMin));
if ($y < $yMin) {
$y = $yMin;
}
$yMin+=2;
};
while ($size > 0) {
$shadeLeft($x, $y);
$shadeDown($x, $y);
$shadeRight($x, $y);
$shadeUp($x, $y);
$size-=2;
}
return $pattern;
}
for ($i = 1; $i <= 25; $i++) {
$checkboard = buildSpiral($i);
echo "<h1>$i</h1>";
echo "<table style='margin-bottom: 2em;'>";
for($row=0;$row<count($checkboard);$row++){
echo "<tr>";
for($col=0;$col<count($checkboard);$col++){
if (!isset($checkboard[$row][$col])) {
echo "<td width='50px' height='50px' bgcolor=\"red\"></td>";
} else {
echo "<td width='50px' height='50px' bgcolor=\"black\"></td>";
}
}
echo "</tr>";
}
echo "</table>";
}
Good day, I was tasked to make a variable counter in php.
Im stuck at the last stage. And that is to display the total number of positive values. I used the count() in php, but it's not giving me the total number. is there a way that i can get the summation of the value that is is count()?
if ($start < $end)
{
for($ctr = $start; $ctr<=$end; $ctr++)
{
if ($ctr == 0)
{
echo " <br/ >there is a zero <br/ >";
}
else if ($ctr <=0)
{
echo count($ctr) . " negative value" ;
}
else if($ctr >=0)
{
echo count($ctr) . " positive value ";
}
}
$zero = $pos = $neg = 0;
for($ctr = $start; $ctr<=$end; $ctr++)
{
switch(true){
case $ctr==0: $zero++; break;
case $ctr<0: $neg++; break;
case $ctr>0: $pos++; break;
}
}
if($zero)
echo "there is a zero<br/>";
if($pos)
echo "$pos positive values<br/>" ;
if($neg)
echo "$neg negative values<br/>" ;
Better way is without any loop:
function test($start, $end) {
echo "Test of [$start, $end]:\n";
if ($isThereZero = $start * $end <= 0) {
echo "There is zero\n";
}
if ($start < 0) {
$negatives = $isThereZero ? -$start : -$start+$end;
echo "$negatives negative values\n";
}
if ($end > 0) {
$positives = $isThereZero ? $end : $end - $start;
echo ($positives)." positive values\n";
}
}
test(-12, 5);
test(-12, -5);
test(5, 12);
test(0, 0);
Try that :
$zer = 0;
$pos = 0;
$neg = 0;
if ($start < $end)
{
for($ctr = $start; $ctr<=$end; $ctr++)
{
if ($ctr == 0)
{
$zer++;
}
else if ($ctr < 0)
{
$neg++;
}
else if($ctr > 0)
{
$pos++;
}
}
echo $zer . " zero value" ;
echo $neg . " negative value" ;
echo $pos . " positive value ";
Just for fun, this could be done without a loop. This is a bit tricky and also a bit far from the initial question :
Demo
$start = -3;
$end = 5;
echo ($start <= 0 && $end >= 0 ? "1" : "0")." zero value\n";
echo ($start < 0 && $start <= $end ? (-$start-($end > -1 ? 0 : -$end-1)) : "0"). " negative value\n";
echo ($end > 0 && $start <= $end ? ($end-($start < 1 ? 0 : $start-1)) : "0"). " positive value\n";
else if($ctr >=0)
{
$cnt++; //or $cnt+=1;
}
Then use $cnt++ for no of count for positive vales
So I'm having this issue where I want to echo only the things that are 1 higher or 1 lower then the current hour. Example if current hour = 14 only echo 13, 14 and 15
Anyone have a clue on how I can do this?
<?php
mysql_connect("*******", "******", "**");
if(!mysql_select_db("deb67423_dj")) {
die("<b>Mislukt!</b><br>Het verbinden met de database is mislukt.");
}
$textweek = date("D");
if($textweek == "Mon") {
$dag = 0;
} else
if($textweek == "Tue") {
$dag = 1;
} else
if($textweek == "Wed") {
$dag = 2;
} else
if($textweek == "Thu") {
$dag = 3;
} else
if($textweek == "Fri") {
$dag = 4;
} else
if($textweek == "Sat") {
$dag = 5;
} else
if($textweek == "Sun") {
$dag = 6;
}
$textuur = date("G");
$SQL = "SELECT * FROM rooster WHERE dag = '$dag'";
$result = mysql_query($SQL);
echo "<table>";
while ($row = mysql_fetch_assoc($result)) {
echo "<td> Hiervoor: DJ ".$row['gebruikersnaam']. "</td>";
}
echo "</table>";
$currHour = date('G');
echo $currHour - 1, ', ', $currHour, ', ', $currHour + 1;
Take note that $currHour can be 0 and you will have to take care of this "special" case... But I think this snippet will get you started. See the PHP date() documentation for more help.