To print 1 to 25 - php

Expected output:
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
Tried following code:
for($i=1;$i<=25;$i++)
{
if($i%5 ==0)
{
echo $i;
echo "<br>";
for($j=($i+5);$j>$i;$j--)
{
echo $j;
}
echo "<br>";
}
else if ($i%5!=0)
{
echo $i;
}
}

for($i=1;$i<=25;$i++)
{
if($i%5 ==0)
{
echo $i;
echo "<br>";
for($j=($i+5);$j>$i;$j--)
{
echo $j;
}
$i = $i+5;
echo "<br>";
}
else if ($i%5!=0)
{
echo $i;
}
}

Related

for loop if else Break php

I'd like to add page like this :
foreach($list->result() as $row){
if($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if($no % 6 == 0 ) {
//// $pdf->Ln();
echo "<br><br><br>";
}
echo $no;
echo "<br>";
$no++;
}
I want if this data > 12 page break , if this data < 12 not add page but if data > 6 add more space
My problem is, it's always like this :
1
2
3
4
5
space
space
space
6
7
8
9
10
11
break
space
space
space
12
This can be easily fixed by:
foreach ($list->result() as $row)
{
if ($row['SOMETHING??'] >= 12)
{
/// Add Page Break Here
echo "<p>break</p>";
}
if ($row['SOMETHING??'] >= 6 && $row['SOMETHING??'] < 12)
{
//// $pdf->Ln();
echo "<br><br><br>";
}
echo $no;
echo "<br>";
$no++;
}
Look carefully at the comparison operators which are being used.
Documentation:
http://php.net/manual/en/language.operators.comparison.php
You just need to re-arrange the order of what you are doing, you are outputting the breaks Before you output the line they relate to
foreach($list->result() as $row){
echo $no;
echo "<br>";
if ($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if ($no % 6 == 0 ) {
//// $pdf->Ln();
echo "<br><br><br>";
}
$no++;
}
You may also need to stop the %6 test doing something when the %12 line will have done something like so
foreach($list->result() as $row){
echo $no;
echo "<br>";
if ($no % 12 == 0 ) {
// Add Page Break Here
echo "<p>break</p>";
}
if ($no % 6 == 0 && $no % 12 != 0) {
//// $pdf->Ln();
echo "<br><br><br>";
}
$no++;
}

output numbers bold except some

How to produce the following output? All numbers should be bold except 10, 20, 30 and 40.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
My current code is:
<?php
$i = 1;
while($i <= 40) {
$m = ($i % 1);
if($m == 0) {
echo '<b><u>' . $i . '</b></u>';
}
$i++;
}
?>
Simple one:
<?php
for ($i=1;$i<=40;$i++){
if ($i % 10 == 0){
$result .= $i;
}
else{
$result .= "<b>".$i."</b>";
}
}
echo $result;
?>
Update 1:
If your logic is to be corrected then,
<?php
$i = 1;
while($i <= 40) {
$m = ($i % 10); // have to replace 1 by 10
if($m == 0) {
echo $i;
}
else{
echo '<b><u>' . $i . '</b></u>';
}
$i++;
}
?>
You can merge the if ($i%10 == 0) into single statement as well.
<?php
$i=1;
while($i<=40)
{
if ($i%10 == 0){
echo $i;
}
else{
echo '<b><u>'.$i.'</b></u>';
}
$i++;
}
?>
one small Correction From 1st answer #Fakhruddin Ujjainwala
Undefined variable: result
<?php
$result = "";
for ($i=1;$i<=40;$i++){
if ($i % 10 == 0){
$result .= $i;
}
else{
$result .= "<b>".$i."</b>";
}
}
echo $result;
?>

I want the following output using for loop in php

I want to display the output as like this
1 1
12 21
123 321
1234 4321
1234554321
Here is my php code
for($i=1;$i <= 5;$i++)
{
for($j=1;$j<=$i;$j++)
{
// print the result
echo "$j";
}
for($y=$i;$y<=$i;$y++)
{
echo ' ';
}
for($k=$i;$k>=1;$k--)
{
// print the result
echo " $k";}
echo "<br/>";
}
But I got the output like this
1 1
12 2 1
123 3 2 1
1234 4 3 2 1
12345 5 4 3 2 1
Please help me get an output like above.
try
for($i=1;$i <= 5;$i++) {
for($j=1;$j<=$i;$j++) {
echo "$j";
}
for($y=0;$y<(5-$i)*4;$y++) {
echo ' ';
}
for($l=$i;$l>0;$l--) {
echo "$l";
}
echo "<br/>";
}
output:-
1 1
12 21
123 321
1234 4321
1234554321
For browser view :- for($y=0;$y<(5-$i)*4;$y++) else correct way to going is for($y=0;$y<(5-$i)*2;$y++)
Did you ever here about the PHP functions str_repeat and range?
http://php.net/manual/en/function.str-repeat.php.
http://php.net/manual/en/function.range.php
With it you can print your character like this:
echo str_repeat(' ', (5 -$i) *4);
Complete code
$count = 5; // count oft rows and oft iterated numbers
for ($i = 1; $i <= $count; $i++) {
echo implode('', range(1, $i));
echo str_repeat(' ', ($count -$i) *4);
echo implode('', array_reverse(range(1, $i)));
echo '<br />';
}
How about this?
for($i=1;$i<=5;$i++){
echo substr("12345", 0, $i);
echo str_repeat(5-$i, ' ');
echo str_reverse(substr("12345", 0, $i));
echo '<br>';
}
Try this:
for($i=1;$i <= 5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo "$j";
}
for($y=1;$y<=10-(2*$i);$y++)
{
echo ' ';
}
for($k=$i;$k>=1;$k--)
{
// print the result
echo "$k";
}
echo "<br/>";
}
OUTPUT:
1 1
12 21
123 321
1234 4321
1234554321
How about this with $y<= 2 * (5 - i),
for($i=1;$i <= 5;$i++) {
for($j=1;$j<=$i;$j++) {
echo "$j";
}
for($y=1;$y<= 2 * (5 - i); $y++) {
echo ' ';
}
for($k=$i;$k>=1;$k--) {
echo "$k";
}
echo "<br/>";
}
for ($counter=1;$counter<= 5;$conter++)
{
for ($j=1;$j<=$counter;$j++)
{
echo "$j";
}
for ($y=1;$y<=10-(2*$counter);$y++)
{
echo ' ';
}
for ($k=$counter;$k>=1;$k--)
{
echo "$k";
}
echo "<br/>";
}

Matrix formation, there is something wrong in the loops

I am trying to get this kind of matrix:
1 0 0 0 5
0 2 0 4 0
0 0 3 0 0
0 2 0 4 0
1 0 0 0 5
and here is the code:
$n = 5;
$flag = 0;
for($i=1; $i<=$n; $i++){
for($j=1; $j<=$n; $j++){
if($i == $j){
echo "$i ";
}else{
echo "0 ";
}
if($j == $n - $flag){
echo $n - $flag." ";
$flag++;
}
}
echo "</br>";
}
the output is:
1 0 0 0 0 5
0 2 0 0 4 0
0 0 3 3 0 0
0 0 2 0 4 0
0 1 0 0 0 5
there is something wrong in the middle. I think it is because two for loops overlap there.
How to fix thiis?
Try this code to output the array you want:
$n = 6;
for($i=1; $i<$n; $i++){
for($j=1; $j<$n; $j++){
if($i == $j){
echo "$i ";
} else if ($j == $n - $i) {
echo $n - $i ." ";
} else {
echo "0 ";
}
}
echo "</br>";
}
The reason isn't an overlap in the loops. Its an overlap of the two if statements.
In the inner loop, you always print 6 things.
for($j=1; $j<=$n; $j++){
if($i == $j){ // <--- This will always print in each iteraton (i.e 5 times)
echo "$i ";
}else{
echo "0 ";
}
if($j == $n - $flag){ // <--- This will print once in the loop (the 6th extra)
echo $n - $flag." ";
$flag++;
}
}
Since $j is equal to $flag, you don't need to keep track of it.
Change it to this (Check it on ideone):
for($j=1; $j<=$n; $j++){
if($i == $j){
echo "$i ";
} elseif($j == $n - $i + 1){
echo $n - ($i-1)." ";
} else {
echo "0 ";
}
}

increment counter when value occurs more than once

An example output of the following code may be this:
39 48 39 12 17 39 12
code:
if (mysql_num_rows($filterResult)) {
while ($filterrow = mysql_fetch_array($filterResult)) {
$vidID = $filterrow['routineID'];
...
...
echo "<video id='video$vidID'></video>";
}
}
I need it to output like this:
39 48 39_1 12 17 39_2 12_1
Notice how value 39 occurs three times and value 12 occurs twice.
After the first occurence of a value I need it
formatted like ##_#.
Can you help me code this up?
I appreciate your time and assistnace -
Derek
Use array_count_values like this:-
$array = array(1, 38, 1, 38,35);
print_r(array_count_values($array));
Output:-
Array
(
[1] => 2
[38] => 2
[35] => 1
)
Apply some logic to achieve what you want.
if (mysql_num_rows($filterResult)) {
$tmp = array();
while ($filterrow = mysql_fetch_array($filterResult)) {
$vidID = $filterrow['routineID'];
if(isset($tmp[$vidID]) {
$tmp[$vidID] = $tmp[$vidID] + 1;
$vidID = $vidID . '_' . $tmp[$vidID];
} else {
$tmp[$vidID] = 0;
}
echo $vidID;
}
}
if (mysql_num_rows($filterResult)) {
while ($filterrow = mysql_fetch_array($filterResult)) {
$j=-1;
$vidID = $filterrow['routineID'];
$arr[]=$vidID;
for($i=0;$i<count($arr);$i++){
if($arr[$i]==$vidID)
$j++;
}
if($j==0)
$j="";
else $j="_".$j;
echo $vidID.$j;
}
}
Here is the code:
$array1 = array(39,48,39,12,17,39,12);
$array2 = array(39,48,39,12,17,39,12);
foreach($array1 as $value){
$counts1 = array_count_values($array1);
$counts2 = array_count_values($array2);
if(!empty($counts2[$value])){
if ($counts1[$value] > 1){
$count = $counts1[$value] - $counts2[$value] + 1;
if($count > 1) {
echo $value.'_'.($count - 1).' ';
} else {
echo $value.' ';
}
} else {
echo $value.' ';
}
array_shift($array2);
}
}
Here is the PHP fiddle

Categories