I want to draw a pattern like this in php. I could not figure it out.
1 6 11
2 7 12
3 8 13
4 9 14
5 10 15
How can i make it?
I tried
for($i=1;$i <= 5;$i++) {
echo $i;
echo ' ';
echo $i + 5;
echo ' ';
echo $i + 10;
echo "<br/>";
is this the best way to do.
Would be nice to see what you tried, however:
echo '<pre>';
$i=0
do {
echo ($i + 1).' '.($i + 6).' '.($i + 11).'
';
} while(++$i < 5);
echo '</pre>';
I tried your script here and it works:
http://phptester.net/
for($i=1;$i <= 5;$i++) {
echo $i.' '.($i + 5).' '.($i + 10)."<br/>";
}
What error did you have? Maybe you forgot to close the loop with "}"?
for($x=1; x<=5; x++){
echo $x." ".$x+5." ".$x+10.+"\n";
}
You are looking for this... I have made in javascript. You can simply turn that code into php
var m = 1;
var str='';
for(var i=0;i < 5; i++ )
{
for(var j = 0; j < 3; j++)
{
str = str + parseInt(m+(j*5));
str +='\t';
}
m=m+1;
str = str+ '\n';
}
console.log(str)
Related
I would like to return combination of coordinates. Please take a look at the provided script and question below.
This script
<?php
$x = 1;
$y = 8;
$x_c = -200;
$y_c = 100;
$i = 20;
while($x <= 5) {
echo "x is: $x <br>";
echo "x_c is: $x_c <br>";
$x++;
$x_c = $x_c + $i;
}
echo "<hr>";
while($y <= 13) {
echo "y is: $y <br>";
echo "y_c is: $y_c <br>";
$y++;
$y_c = $y_c - $i;
}
?>
will return:
x is: 1
x_c is: -200
x is: 2
x_c is: -180
x is: 3
x_c is: -160
x is: 4
x_c is: -140
x is: 5
x_c is: -120
y is: 8
y_c is: 100
y is: 9
y_c is: 80
y is: 10
y_c is: 60
y is: 11
y_c is: 40
y is: 12
y_c is: 20
y is: 13
y_c is: 0
Question
How can I return combination of this as pattern
x1,y1,x_c1,y_c1
x1,y2,x_c1,y_c2
x1,y3,x_c1,y_c3
...
x2,y1,x_c2,y_c1
x2,y2,x_c2,y_c2
x2,y3,x_c2,y_c3
...
x3,y1,x_c3,y_c1
x3,y2,x_c3,y_c2
x3,y3,x_c3,y_c3
or with numbers
1,8,-200,100
1,9,-200,80
1,10,-200,60
...
2,8,-180,100
2,9,-180,80
2,10,-180,60
...
3,8,-160,100
3,9,-160,80
3,10,-160,60
You need to put the $y loop inside the $x loop. Here I am using $yy and $yy_c for the inner loop so that I can reset them back to the original $y and $y_c before entering the loop.
<?php
$x = 1;
$y = 8;
$x_c = -200;
$y_c = 100;
$i = 20;
while($x <= 5) {
$yy = $y;
$yy_c = $y_c;
while($yy <= 13) {
echo "$x,$yy,$x_c,$yy_c<br>";
$yy++;
$yy_c = $yy_c - $i;
}
$x++;
$x_c = $x_c + $i;
}
?>
I have tried this code and output i am getting is here
<?php
for($i=1;$i<=5;$i++){
for($j=5-$i;$j>=1;$j--){
echo " ";
echo " ";
}
for($k=1;$k<=$i;$k++){
echo $i;
}
echo "<br/>";
}
?>
1
22
333
4444
55555
but i want output like this, please tell where i am wrong
1
12
123
1234
12345
Just change echo $i to echo $k,
for($k=1;$k<=$i;$k++){
echo $k;
}
Since you work with HTML, you may want to wrap this logic into a function that could let you choose the number of lines to display. You can tweak it to fit your needs in term of CSS :
function numberTriangle($endNumber, $align_text = 'right') {
$max = intval($endNumber);
$align = strval($align_text);
$align = ( strlen(trim($align)) > 0 ) ? $align : 'right';
echo "<div style='text-align : $align; display : inline-block'>";
if( $max > 0 ) {
for( $i = 1; $i < $max + 1; $i++ ) {
for( $j = 1; $j < $i + 1; $j++ ) {
echo $j;
}
echo '<br />';
}
}
echo '</div>';
}
numberTriangle(5);
// Will display :
/*
1
12
123
1234
12345
*/
Thanks to this function you do not have to add manual spaces anymore, which is cleaner (and saves you headhaches !).
What I need is putting this array in a loop, but I can't get it to work because week is 1 - 9 and the key is 0 - 8. So i getting a error with a undefined offset i know why the does that but I don't know how to do this properly.
Before people ask me why not just change week1 to week0 and start counting from there. I can't because I did a calculating that is based on 1 - 52 and it will mess up my calculating if I start on 0 - 51
$totaal_vruchtzetting_week[10] = $totaal["week1"][0] + // = 0.46
$totaal["week2"][1] + // = 2.87
$totaal["week3"][2] + // = 4.97
$totaal["week4"][3] + // = 4.35
$totaal["week5"][4] + // = 3.02
$totaal["week6"][5] + // = 2.03
$totaal["week7"][6] + // = 1.41
$totaal["week8"][7] + // = 1.12
$totaal["week9"][8]; // = 1.13
// Should be total 21,36
Edit:
This is my loop I got until now but it gives me the wrong answer plus 2 errors
for($week = 1; $week < 9; $week++)
{
for($sw = 0; $sw <= 8; $sw++)
{
$totaal_vruchtzetting_week[10] += $totaal["week".$week][$sw];
}
}
echo $totaal_vruchtzetting_week[10]; // Outputs 170.89
$i=1;
$totaal_vruchtzetting_week[10]=0;
foreach($totaal as $total)
{
$totaal_vruchtzetting_week[10]+=$total["week$i"][$i-1];
$i++;
}
echo $totaal_vruchtzetting_week[10];
You should sum with this this loop
$i = 1;
$result = 0;
for ($i = 1; $i <= 9; $i++) {
if (isset($totaal['week' . $i]) && isset($totaal['week' . $i][$i - 1])) {
$result += floatval($totaal['week' . $i][$i - 1]);
}
}
$totaal_vruchtzetting_week[10] = $result;
$sum = 0;
foreach(array_values($totaal) as $index=>$item)
$sum += reset($item);
echo $sum; // 21.36
$totaal_vruchtzetting_week[10] = $sum;
Demo
Not sure if I got your question, but did you consider foreach? It iterates over every array regardless of the keys.
My phpcode is
<?php for($i = 1; $i < 5; $i++) { ?>
<div class="upperBoxContainer" id="test1<?php echo $i; ?>">
<img id="test<?php echo $i; ?>" src="images/midimages/<?php echo $i; ?>.jpg">
</div>
<?php } ?>
And my javascript code is
jQuery(document).ready(function() {
for (var i = 1; i < 5; i++) {
alert('images/midimageshover/' + i + '.jpg');
$('#test1' + i).hover(function() {
$('#test' + i).attr('src', 'images/midimageshover/' + i + '.jpg');
});
$('#test1' + i).mouseout(function() {
$('#test' + i).attr('src', 'images/midimages/' + i + '.jpg');
});
}
});
There is some error due to this code is not work. can you please find this.
Thanks in advance
Enclose your events in a Immediately-Invoked Function Expression (IIFE)
jQuery(document).ready(function(){
for (var i = 1; i < 5; i++) {
(function(i){
$('#test1'+i).hover(function(){
$('#test' + i).attr('src', 'images/midimageshover/' + i + '.jpg');
});
$('#test1'+i).mouseout(function(){
$('#test' + i).attr('src', 'images/midimages/' + i + '.jpg');
});
})(i);
}
});
Your code is rewriting the value of $('#test1'+i) until it reaches 4, so at the end of the loop you'll only have $('#test14').
The code above create different scope by using the IIFE hence $('#test1'+i) won't be overwritten, instead you'll have $('#test11'), $('#test12'), $('#test13'), $('#test14')
Here is a demo to demonstrate the difference with your initial code
is that possible to sum variable static values in the while or for loop? i have code and working on it but it sum variables only one time?
Here My Code
session_start();
$length=count($_SESSION['product1']);
$shipping2='280';
$shipping3='680';
$newshipping='0';
$newshipping1='0';
$i='0';
while($i= <$length)
{
$newshipping=$shipping2+$shipping3;
$newshipping1=$newshipping+$shipping2;
}
For Example I want like this
$shipping2='280'; should be sum with every result of $newshipping1
`$newshipping1`= $shipping2='280' + $shipping3='680' = `$newshipping1`=960
+ $shipping2='280'??
'$newshipping1`=960+ $shipping2=280+ $shipping2=280+ $shipping2=280 .....
when ever new product1 enter it should be add $shipping2=280
in the result of `$newshipping1`
I have completed my code here my final code
$length=count($_SESSION['product1']);
$shipping2=280;
$shipping3=680;
$newshipping=0;
for($i=0; $i <$length; $i++) {
if($i == 1) {
$newshipping = $shipping2+$shipping3;
} else if($i <= 100) {
$newshipping = $newshipping+$shipping2;
}
}
Your logic seems a bit confusing, however you can sum several integers. If you are trying to figure out the final amount of several iterations you should:
$shipping_one = 680;
$shipping_two = 260;
$shipping_three = 0;
$finalShipping = array();
while($i= <$length)
{
$finalShipping[] = $shipping_one + $shipping_two + $shipping_three;
}
$finalTotal = array_sum($finalShipping);
If you can clarify your question, I can clarify my answer.
this will be ((680) + (4 * 280)) like you explained in your last comment.
$length = 1;
echo "Test with $length : ".getShippingTotal($length)." <br />";
$length = 2;
echo "Test with $length : ".getShippingTotal($length)." <br />";
$length = 3;
echo "Test with $length : ".getShippingTotal($length)." <br />";
$length = 4;
echo "Test with $length : ".getShippingTotal($length)." <br />";
$length = 5;
echo "Test with $length : ".getShippingTotal($length)." <br />";
function getShippingTotal($length) {
$shipping2=280;
$shipping3=680;
$total = 0;
for($i=0; $i < $length; $i++) {
if($i == 0) {
$total += $shipping2+$shipping3;
} else {
$total += $shipping2;
}
}
return $total;
}
I have tested it and it gave me:
Test with 1 : 960
Test with 2 : 1240
Test with 3 : 1520
Test with 4 : 1800
Test with 5 : 2080