Printing star pattern in alphabetical shape but not working - php

hii i'm trying to improve logic in php where i'm trying to print star pattern in A shape,B shape and K shape but it isnt working can anyone help me with my logic my code is as follows
k.php
<?php
$j = 5;
$i = 0;
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == $column + 1) and $column != 0))
echo "*";
else if ($row == $i and $column == $j)
{
echo "*";
$i=$i+1;
$j=$j-1;
}
else
echo " ";
}
echo "<br>";
}
?>
B.php
<?php
for ($row=0; $row<7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == 0 or $row == 3 or $row == 6) and ($column < 5 and $column > 2)) or ($column == 5 and ($row != 0 and $row != 3 and $row != 6)))
echo "*";
else
echo " ";
}
echo "<br>";
}
?>
A.php
<?php
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ((($column == 1 or $column == 5) and $row != 0) or (($row == 0 or $row == 3) and ($column > 1 and $column < 5)))
echo "*";
else
echo " ";
}
echo "<br>";
}
?>
my code isnt working as expected please anyone help

You can try this code:
<pre><?php echo getK();?></pre>
<pre><?php echo getA();?></pre>
<pre><?php echo getB();?></pre>
<?php
function getK()
{
$j = 5;
$i = 0;
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == $column + 1) and $column != 0))
echo "*";
else if ($row == $i and $column == $j)
{
echo "*";
$i=$i+1;
$j=$j-1;
}
else
echo " ";
}
echo "\n";
}
}
function getA()
{
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ((($column == 1 or $column == 5) and $row != 0) or (($row == 0 or $row == 3) and ($column > 1 and $column < 5)))
echo "*";
else
echo " ";
}
echo "\n";
}
}
function getB()
{
for ($row=0; $row<7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == 0 or $row == 3 or $row == 6) and ($column < 5 and $column > 2)) or ($column == 5 and ($row != 0 and $row != 3 and $row != 6)))
echo "*";
else
echo " ";
}
echo "<br>";
}
}

One more answer:
legend:
n = "new line (\n)"
* = letter (you can use, bettor for you)
space = space)))
for example: "a" => " *** n
* *n
* *n
*****n
* *n
* *n
* *",
Code:
<?php
$letters = [
"a" => " *** n* *n* *n*****n* *n* *n* *",
"b" => "**** n* *n* *n****n* *n* *n**** ",
"k" => "* *n* *n* *n**n* *n* *n* *",
];
function printLetter($pattern)
{
$len = strlen($pattern);
for ($i = 0; $i < $len; $i++) {
if ($pattern[$i] == 'n') {
echo "\n";
continue;
}
echo $pattern[$i];
}
}
?>
<pre><?php printLetter($letters['a']); ?></pre>
<pre><?php printLetter($letters['b']); ?></pre>
<pre><?php printLetter($letters['k']); ?></pre>

Related

Creating two triangle using php

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

Pagination with part in php

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

Displaying retrieve data in different table

Okay, what I want is to display retrieved data in a table form database, I want my table to be limit in 5 data per table. I need the tables to be display horizontally.
Like this:
while($row1 = sqlFetchArray($row))
{ $ctr = 0;
$ctr+1;
if($ctr=1 || $ctr<=5)
{
$html .='<table width="100px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=6 || $ctr<=10)
{
$html .='<table width="110px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=11 || $ctr<=15)
{
$html .='<table width="120px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
}
Output:
1 6
2 7
3 8
4 9
5 10
This should work for you:
(But the function sqlFetchArray as to work like mysql_fetch_array)
<?php
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = mysql_num_rows($row); $numberCounter++) {
$count = 0;
if($numberCounter >= mysql_num_rows($row))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($row1 = sqlFetchArray($row) && $count < $limit) {
if($numberCounter >= mysql_num_rows($row))
break;
echo "<tr><td>" . $row1[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>
As an example:
<?php
$test = range(1, 43);
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = count($test); $numberCounter++) {
$count = 0;
if($numberCounter >= count($test))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($count < $limit) {
if($numberCounter >= count($test))
break;
echo "<tr><td>" . $test[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>

PHP - If number is divisible by 3 and 5 then echo

I'm new to PHP and trying to create the following whilst minimizing the amount of code needed. PHP should show a list of 100 then display if the number is / by 3, 5 or 3 and 5. If not by any then show nothing.
This is what I've got so far, but any help would be great since not sure about the / by 3 and 5 bit as you can see below.
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number) {
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 and 5 == 0) {
echo "BY3 AND 5";
}
echo "</td></tr>";
}
?>
</table>
Thanks
Nope... you should check first if it's divisble for 15 (3x5) (or 3 and 5) and after you can do other checks:
if($number % 15 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
}
echo "</td></tr>";
?>
Because every number divisble for 15 is also divisble for 3 and 5. So your last check could never hit
if I'm reading your question correct then you are looking for :
if ($number % 3 == 0 && $number %5 == 0) {
echo "BY3 AND 5";
} elseif ($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
}
Alternative version :
echo ($number % 3 ? ($number % 5 ? "BY3 and 5" : "BY 3") : ($number % 5 ? "BY 5" : ""));
$num_count = 100;
$div_3 = "Divisible by 3";
$div_5 = "Divisible by 5";
$div_both = "Divisible by 3 and 5";
$not_div = "Not Divisible by 3 or 5";
for($i=0;$i<=$num_count;$i++)
{
switch($i)
{
case ($i%15==0):
echo $i." (".$div_both.")</br>";
break;
case ($i%3==0):
echo $i." (".$div_3.")</br>";
break;
case ($i%5==0):
echo $i." (".$div_5.")</br>";
break;
default:
echo $i."</br>";
break;
}
}
No need to do three if statements:
echo "<table border='1'>";
for ($i = 1; $i <= 100; $i++) {
echo "<tr><td>{$i}</td><td>";
if ($i % 3 == 0) echo "BY3 ";
if ($i % 5 == 0) echo "BY5";
echo "</td></tr>\n";
}
echo "</table>";
Update the code as given below
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number)
{
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0 && $number % 5 == 0)
{
echo "BY3 AND 5";
}
elseif ($number % 5 == 0)
{
echo "BY5";
}
elseif ($number % 3 == 0)
{
echo "BY3";
}
echo "</td></tr>";
}
?>
<?php
if($number % 5 == 0 && $number % 3 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
} else{
echo "NOT BY3 OR 5";
}
?>
if($number % 15 == 0)
{
echo "Divisible by 3 and 5";
}
elseif ($number % 5 == 0)
{
echo "Divisible by 5";
}
elseif ($number % 3 == 0)
{
echo "Divisible by 3";
}
This is neater and completed to be run:
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0)
{
echo"Divisible by 3 and 5</br>";
}
elseif ($i%3==0)
{
echo"Divisible by 3</br>";
}
elseif ($i%5==0)
{
echo"Divisible by 5</br>";
}
else
{
echo $i,"</br>";
}
}
?>
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0) echo "This Number is Divisible by 3 and 5<br>";
else if ($i % 3 == 0) echo "This Number is Divisible by 3 only<br>";
else if ($i % 5 == 0) echo "This number is Divisible by 5 only<br>";
else{
echo "$i<br>";
}
}
?>

Break mysql_num_rows into 6x4

Pretty much I have a code like below that I'm trying to add </tr><tr> to after every 6 results.
echo "<table><tr>";
$query="SELECT * WHERE id='$id' ORDER BY date ASC";
$result=mysql_query($query);
if (mysql_num_rows($result) > 0) {
while($rts = mysql_fetch_array($result)){
$cdata1 = $rts['cdata1'];
$cdata2 = $rts['cdata2'];
echo "<td>$cdata1 and $cdata2</td>";
}
}else{
echo "<td>no results</td>";
}
echo "</tr></table>";
echo "<table><tr>";
$query="SELECT * WHERE id='$id' ORDER BY date ASC";
$result=mysql_query($query);
$i = 0;
if (mysql_num_rows($result) > 0) {
while($rts = mysql_fetch_array($result)){
$cdata1 = $rts['cdata1'];
$cdata2 = $rts['cdata2'];
echo "<td>$cdata1 and $cdata2</td>";
if(++$i % 6 == 0) {
echo '</tr><tr>';
}
}
}else{
echo "<td>no results</td>";
}
echo "</tr></table>";
UPD:
Whats means if(++$i % 6 == 0) code:
++$i equals $i = $i + 1;
$i % 6 means $i modulo 6
If $i modulo 6 equals 0 then echo </tr><tr>
So we can write it as:
$i = $i + 1;
if($i % 6 == 0) {
echo '</tr><tr>';
}
http://php.net/manual/en/internals2.opcodes.mod.php
http://php.net/manual/en/language.operators.increment.php

Categories