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 ";
}
}
Related
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;
?>
Heres the scenario, I have array with 7 items and I want to separate them in every fourth iteration.. just like this
$counter2 = 0;
$counter3 = 0;
$counter4 = 0;
$sample_array = array('Aso','Pusa','Daga','Kuting','Tuta','Bubwit','Boom');
foreach($sample_array as $sample_array_value)
{
if(++$counter4 % 4 == 0)
{
echo $sample_array_value;
echo "</div>";
}
elseif(++$counter3 % 3 == 0)
{
echo $sample_array_value;
}
elseif(++$counter2 % 2 == 0)
{
echo $sample_array_value;
}
else
{
echo "<div>";
echo $sample_array_value;
}
}
The out put will be div AsoPusaDagaKuting /div div TutaBubwitBoom
The problem is when it ends in iteration that doesn't count 4 it doesn't give the separator ending..
I need it to output div AsoPusaDagaKuting /div div TutaBubwitBoom /div
Thanks in advance...
You can split it with array_chunk, implode the new subarrays of 4 with array_map, then echo it with implode.
$sample_array = array('Aso','Pusa','Daga','Kuting','Tuta','Bubwit','Boom');
echo "<div>", implode("</div><div>", array_map("implode", array_chunk($sample_array, 4))), "</div>";
Result:
<div>AsoPusaDagaKuting</div><div>TutaBubwitBoom</div>
Try this:
$i = 0;
foreach($sample_array as $sample_array_value)
{
if(++$counter4 % 4 == 0)
{
echo $sample_array_value;
echo "</div>";
}
elseif(++$counter3 % 3 == 0 || ++$counter2 % 2 == 0)
{
echo $sample_array_value;
}
else
{
echo "<div>";
echo $sample_array_value;
}
$i++;
}
if ($i % 4 != 0) {
echo "</div>";
}
You are printing the values inside every condition, then why not use echo once outside any condition? Also you want to close and open a div tag for the forth element, then only 1 counter would do the trick. Only this will work -
$sample_array = array('Aso','Pusa','Daga','Kuting','Tuta','Bubwit','Boom');
$i = 0;
echo "<div>";
foreach($sample_array as $sample_array_value)
{
if($i > 0 && $i % 4 == 0)
{
echo "</div><div>";
}
echo $sample_array_value;
$i++;
}
echo "</div>";
Output
<div>AsoPusaDagaKuting</div><div>TutaBubwitBoom</div>
I'm a beginner at PHP, so my code might not be efficient or good.
Why does this code return 1 number and then stops the loop? It's supposed to stop the loop when "the dice" rolled two of every number (1,2,3,4,5,6). But now it stops after randomly generating 1 number..
<?php
$sixCount = 0;
$fiveCount = 0;
$fourCount = 0;
$threeCount = 0;
$twoCount = 0;
$oneCount = 0;
$rollCount = 0;
do{
$roll = rand(1,6);
$rollCount++;
if($roll == 6){
$sixCount++;
echo "6";
} else if($roll == 5){
$fiveCount++;
echo "5";
} else if($roll == 4){
$fourCount++;
echo "4";
} else if($roll == 3){
$threeCount++;
echo "3";
} else if($roll == 2){
$twoCount++;
echo "2";
} else {
$oneCount++;
echo "1";
}
} while($sixCount < 3 && $sixCount > 1 && $fiveCount < 3 && $fiveCount > 1 && $fourCount < 3 && $fourCount > 1 && $threeCount < 3 && $threeCount > 1 && $twoCount < 3 && $twoCount > 1 && $oneCount < 3 && $oneCount > 1);
echo "<br />It took {$rollCount} rolls!";
?>
This is an exercise from Codecademy.com!
Thanks,
Jesper (New at Stackoverflow!)
After first execution of loop, you cannot have $sixCount > 1 && $fiveCount > 1, among other conditions.
After first roll, suppose it's 3, your variables are:
$sixCount = 0;
$fiveCount = 0;
$fourCount = 0;
$threeCount = 1;
$twoCount = 0;
$oneCount = 0;
It doesn't suit while conditions, cuz, for example, $sixCount > 1 is false and other vars too.
The while expression says:
while ($sixCount < 3 && $sixCount > 1 && $fiveCount < 3 && $fiveCount > 1 ...
If $sixCount is less than 3 and more than 1 that implies $sixCount equals 2. Ditto for the others. So it means "keep looping while $sixCount equals 2 and $fiveCount equals 2 and [all the others equal 2]".
You start with those variables at 0:
$sixCount = 0;
$fiveCount = 0;
...
So the loop condition is not initially met. The loop allows at most one of them to be incremented at most once:
$roll = rand(1, 6);
if ($roll == 6) {
$sixCount++;
echo "6";
} else if ($roll == 5) {
$fiveCount++;
echo "5";
} ...
No matter what number is rolled it is impossible to get any of the counts to 2 by the end of a single roll, and certainly not all of them, so the loop condition will not be met, and the loop will inevitably stop.
It's supposed to stop the loop when "the dice" rolled two of every number (1,2,3,4,5,6)
In that case, the correct condition would be:
while ($sixCount < 2 && $fiveCount < 2 && ...
As others have said, your conditional for the while loop will never be true. Instead, you want to make sure the variables aren't all at 2. Try this instead:
while ($sixCount < 2 || $fiveCount < 2 || $fourCount < 2 || $threeCount < 2 || $twoCount < 2 || $oneCount < 2)
I just adapted your script. It can be a funny game.
It rolls 6 dice times 2 (6 x 2) and then if requirements are not met, it rolls the dice again :
$rollCount = 0;
do{
$sixCount = 0;
$fiveCount = 0;
$fourCount = 0;
$threeCount = 0;
$twoCount = 0;
$oneCount = 0;
$rollCount++;
for ($i= 0; $i< 2 * 6; $i++) {
$roll = rand(1,6);
if($roll == 6){
$sixCount++;
echo "6";
} else if($roll == 5){
$fiveCount++;
echo "5";
} else if($roll == 4){
$fourCount++;
echo "4";
} else if($roll == 3){
$threeCount++;
echo "3";
} else if($roll == 2){
$twoCount++;
echo "2";
} else {
$oneCount++;
echo "1";
}
}
echo "_";
} while(!($sixCount < 3 && $sixCount > 1 && $fiveCount < 3 && $fiveCount > 1 && $fourCount < 3 && $fourCount > 1 && $threeCount < 3 && $threeCount > 1 && $twoCount < 3 && $twoCount > 1 && $oneCount < 3 && $oneCount > 1));
echo "<br />It took {$rollCount} rolls!";
$a = array(
$sixCount,
$fiveCount,
$fourCount,
$threeCount,
$twoCount,
$oneCount);
echo '<pre>';
print_r($a);
echo '</pre>';
Giving this king of output :
262225451666_535451252543_666153663214_652652635413_522615315213_412123422526_113553235335_255616351453_124215216465_112544353243_161145351612_522462262355_114331531645_563664155335_455623424146_233336226515_213136514365_646344361534_445325236533_423153546564_324466143565_422464136444_631511342612_516266141216_613556242333_351541131651_554665566244_261433652145_
It took 28 rolls!
Array
(
[0] => 2
[1] => 2
[2] => 2
[3] => 2
[4] => 2
[5] => 2
)
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>";
}
}
?>
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