I need to create the Letter T with PHP code:
This is what I have so far but can't seem to figure how to just have the asterisks on the top two lines in order to extend the top of the T:
<?php
echo "<pre>";
for ($row > 2; $row < 15; $row++) {
for ($column = 2; $column < 12; $column++) {
if (($row < 2 || $row < 2) || ($column < 2 || $column >= 6)) {
echo "*";
}
else echo " ";
}
echo "\n";
}
echo "</pre>";
?>
You have several bugs in your code.
for ($row > 2; $row < 15; $row++) {
for ($column = 2; $column < 12; $column++) {
why do you use $row > 2 and $column = 2 ? Just start from zero.
if (($row < 2 || $row < 2) || ($column < 2 || $column >= 6)) {
Why do you check if $row < 2 is true or $row < 2 is true if they are the same?
Here is an example:
echo "<pre>";
for($i=0; $i <= 10; $i++){
for($j = 0; $j < 10; $j++){
if($i > 2 && ($j < 3 || $j > 6)){
echo " ";
}else{
echo "*";
}
}
echo "\n";
}
for ($row > 2; $row < 15; $row++) {
This condition is wrong, and should be:
for ($row = 0; $row < 15; $row++) {
And:
if (($row < 2 || $row < 2)
is wrong and doesn't do what you probably think it does.
The code in the thread j08691 linked you to, contains the correct solution and you could use that:
<?php
echo "<pre>";
for ($row = 0; $row < 15; $row++) {
for ($column = 0; $column <10; $column++) {
if (($row < 1 || $row > 15) ||( $column == 4)) {
echo "*";
}
else echo " ";
}
echo "\n";
}
echo "</pre>";
?>
See the live demo.
start your for loop with $row = 0
for ($row = 0; $row < 15; $row++) {
you were ignoring the first 2 lines and only drawing the vertical line
Also ($row < 2 || $row < 2) is the same as $row < 2
#j08691 found your exact question if you want more info
Related
<?php
for ($row = 1; $row <= $_POST["number"]; $row++)
{
for ($col = 1; $col <= ($row >= ($_POST["number"]/2) ? ($_POST["number"]+1)- $row : $row); $col++)
{
echo '* ';
}
echo "<br>";
}
print(json_encode(count($row)));
?>
the question is to print the pattern and also the total number of stars in each row.
i tried degugging my self but if i change the condition and only for some inputs the answer is correct.
You can use the following:
<?php
$_POST["number"] = 10;
$row_num = array();
for ($row = 1; $row <= $_POST["number"]; $row++)
{
$count = 0;
for ($col = 1; $col <= ($row >= ($_POST["number"]/2) ? ($_POST["number"]+1)- $row : $row); $col++)
{
$count++;
echo '* ';
}
echo "<br>";
array_push($row_num, $count);
}
print_r ($row_num);
?>
This question already has answers here:
How to get whole and decimal part of a number?
(20 answers)
Closed 1 year ago.
I want to get the breakdown of the charges but im stock on the decimal point, it should be the output i fill.out below. please see and check my code if there is lacking. it really could help me much. Ill show the output of my code.
$charge = 1600.50;
$base = 750;
$difference = $charge - $base;
$installment = $charge / 750;
$remainder = ($charge % 750);
$counter = 1;
if (is_float($charge)) {
if($remainder == 0) {
$count = 0;
for ($i=1; $i <= round($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
} else {
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
}
} else {
if($remainder == 0){
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
} else {
if($difference < 0){
echo $remainder."\n";
} else {
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
}
}
}
This is the output of my code.
750
750
100
0.50
But the correct output would be this.
750
750
100.50
I hope there is anyone could help me to solve this problem. it took weeks but im not able to solve this.
I don't know if i faced your problem correctly, but according to your desired output the following will do the trick.
else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
// echo $remainder."\n";
echo $remainder + $charge-floor($charge); // get fractial part of your $charge and add it to your remainder
}
$counter = $counter + 1;
}
/* unnecessary if-statement, is already checked by surrounding if-statement
if(is_float($charge)){
*/
// echo 'TRUE';
// $exploadedAmount = explode('.', $charge);
// echo "0.".$exploadedAmount[1];
}
}
I really don't understand what are you trying to do but isn't it better to write the code in partly functions?
anyways by editing this part of your code I manage to output the result you want:
$charge = 1600.50;
$base = 750;
$difference = $charge - $base;
$installment = $charge / 750;
$remainder = ($charge % 750);
$counter = 1;
if (is_float($charge)) {
// echo "TRUE";
if($remainder == 0){
$count = 0;
for ($i=1; $i <= round($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
}
else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}
$counter = $counter + 1;
}
if(is_float($charge)){
// if you want to concatinate the number to previuse one you must do it here
// the extra 0.5 is echo here
$exploadedAmount = explode('.', $charge);
echo "$remainder.".$exploadedAmount[1];
}
}
}
else{
if($remainder == 0){
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
}
else{
if($difference < 0){
echo $remainder."\n";
}else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
}
}
}
The output will be :
750 750 100.5
echo ".".$exploadedAmount[1]."0" ;
At line number 63 , if u change that number and 0 adding at the beginning and end means your can get that accurate output and also it is applicable for other phone numbers also.
I am pulling rows from a MySql DB, and I want to enter a line break on the 2nd row.
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$status = $row ['status'];
echo "$status";
}
if ($i % 4 == 0) {
echo '';
}
Is there a way of knowing if it is the 2nd row? Im guessing its something to do with $i?
Sorry if it is a silly question!
You're right. It has something to do with the $i variable.
If you want to enter it the 2nd row only you can evaluate $i==2. Also you could add it every 2nd row by evaluating $i % 2 == 0
This results in the following code:
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$status = $row ['status'];
echo "$status";
if ($i == 2){ // Or replace the evaluation with $i % 2 == 0
echo '<br />'; //HTML linebreak
//echo '\n'; //This is for a newline character.
}
}
if ($i % 4 == 0) {
echo '';
}
This should do it:
for ($i = 1; $i <= mysql_num_rows($result); $i++) {
$row = mysql_fetch_array($result);
$status = $row['status'];
echo "$status";
if ($i == 2) {
echo "\n"; //line break
}
}
if ($i % 4 == 0) {
echo '';
}
If you want an HTML line break, use:
echo "<br/>";
Your indenting might lead you to think the $i mod 4 is inside of the for loop when in fact it is not.
No questions are silly :)
I'm having a little trouble figuring out why there's an extra "0" box on my multiplication table, and here's the code that I have so far:
$cols = 10;
$rows = 10;
$number = 0;
$number2 = 0;
echo "<table border=\"1\">";
for ($r = 0; $r < $rows; $r++){
echo('<tr>');
if ($r == 0) {
for ($i = 0; $i < $rows; $i++) {
echo('<td>' .$number2++.'</td>');
}
}
for ($c = 0; $c < $cols; $c++){
if ($c == 0) {
echo('<td>' .$number++.'</td>');
} else if ($r != 0) {
echo( '<td>' .$c*$r.'</td>');
}
}
echo('</tr>');
}
echo("</table>");
So far it looks good, but that extra 0 on the first row is bothering me. Also I would like to keep the original format of the multiplication table if possible.
Here is:
$cols = 10;
$rows = 10;
$number = 1;
$number2 = 0;
echo "<table border=\"1\">";
for ($r = 0; $r < $rows; $r++){
echo('<tr>');
if ($r == 0) {
for ($i = 0; $i < $rows; $i++) {
echo('<td>' .$number2++.'</td>');
}
}
for ($c = 0; $c < $cols; $c++){
if ($c == 0 && $r != 0) {
echo('<td>' .$number++.'</td>');
} else if ($r != 0) {
echo( '<td>' .$c*$r.'</td>');
}
}
echo('</tr>');
}
echo("</table>");
You have a progression from 0 to 10. But, in the first td of the second for, you should not start from 0, you need to start from 1, or the 0 will be showed at the end of the first row. It's becase you already started the first row using the if, so the second one will repeat it.
You just need to check if the $r is 0 (to avoid repeat the first row) and start the $number from 1 (to follow the same logic, but starting from 1).
How about this:
$cols = 10;
$rows = 10;
$number = 0;
$number2 = 0;
echo "<table border=\"1\">";
for ($r = 0; $r <= $rows; $r++){
echo('<tr>');
if ($r == 0) {
for ($i = 0; $i < $rows; $i++) {
echo('<th>' .$number2++.'</th>');
}
}
for ($c = 0; $c <= $cols; $c++){
if ($c == 0) {
echo('<th>' .$number++.'</th>');
} else if ($r != 0) {
echo( '<td>' .$c*$r.'</td>');
}
}
echo('</tr>');
}
echo "</table>";
I am trying to print number vertically and it must be in group
here is my code
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= 24; $j = $j + $rows) {
$count++;
if($count>$nums){
break;
}
echo "<div class='fleft'>$count</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
out of above
but i want output like for the first column
and next group number will start from where first group number end. in this case next group start from 25
please ask if any doubt
$nums = 105;
$rows = 8;
$colsize = 3;
$col = floor($nums / $rows);
$group = floor($col / $colsize);
$count = 0;
$groupsize = $rows * $colsize;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
$modulo = 0;
$correction = 0;
$rest = $nums - $count;
if ($rest < $groupsize) {
$empty = $groupsize - $rest;
$correction = floor($empty / $colsize);
$modulo = $empty % $colsize;
}
for ($i = 1; $i <= $rows; $i++) {
$colind = 0;
for ($j = $i; $j <= $groupsize; $j = $j + $rows) {
$count++;
if ($count > $nums) {
break;
}
$val = $j + ($g * $groupsize);
$val -= $colind * $correction;
$modcor = $colind - ($colsize - $modulo);
if ( $modcor > 0 ) {
$val -= $modcor;
}
echo "<div class='fleft'>" . $val . "</div>";
$colind++;
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
This works:
Also, you can change number of digits, columns or size of column
for($group = 0; $group < 3; $group++){
for($row =1 ; $row <= 8; $row++){
for($col = 0; $col <= 2; $col++){
echo ($group*24)+ $row + 8 * $col; echo " ";
}
echo "\n";
}
}
This code will print the number in the requested format. You need to modify according to your need.
may be i am mad , made a simple alter .... try this
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$letCounter=0; //added a counter
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
$letCounter=0; //reset counter on each loop
for ($j = $i; $j <= 24; $j = $j + $rows)
{
$count++;
if($count>$nums)
{break;}
//made an alter in the below line , some math :)
echo "<div class='fleft'>".($letCounter++ * $rows +$i)."</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
Thanks !
This May work
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$flag = true;
for($c=1;$c<=$col;$c++)
{
if($c%$group== 1)
{
echo "Group Start";
$flag = false;
}
for ($i = 1; $i <= $rows; $i++) {
$count++;
echo "<div class='fleft'>$count</div>";
echo "<div class='clear'></div>";
}
echo "Line End";
if($c%$group == 2&& $flag)// Check here for your requirement
echo "Group End </br>";
$flag = true;
}