i need help for this task.
i need a for loop who picks the closest duration
sample we have a duration with 120 seconds the 120 seconds are on every calculation divided by 8
so here is an example
120 Duration
Here are the 8 closest values
15
30
45
60
75
90
105
120
How i can realize this i have all tested
<?php
$count = 1;
$duration = 120;
$temp = 0;
for ($x = 1; $x < 120; $x++) {
#$j = $x / 8;
$temp = $x / 8;
echo '<pre>' . ($temp) . '</pre>';
if ($count == 8) {
break;
}
$count++;
}
?>
Your entire loop is totally redundant. Why not just this:
<?php
for ($i = 0; i < 8; ++$i)
{
echo '<pre>' . (15 * ($i+1)) . '</pre>';
}
?>
You can use $i directly as a counter in the loop.
Your question is quite unclear and the provided code doesn't go from 1->120 since you break it after 8 iterations.
To get the values 15 30 45 60 75 90 105 120 from the base 120 you would need something like this:
$result = array();
$duration = 120; //the duration in seconds as provided in the example
$divider = 8; //the divider 8 as provided by the example
for ($i = 1; $i <= $divider; $i++) {
//This will give 1* 120/8 = 15 for the first run
//2* 120/8 = 30 for the second and so on
$result[] = (int) $i * $duration / $divider;
}
would the following work?
function sample($max, $count) {
$samples = array();
for($i = 1; $i <= $count; ++i) {
$samples[] = (int)($max / $count * $i);
}
return $samples;
}
Do you mean something like
$result = array();
for ($i = 1; $i <= 8; $i++) {
$result[] = (int) ($duration / 8) * $i;
}
?
Related
I have this for loop:
for($i = 1; $i <= 7; $i++) {
echo $i . "<br>";
}
Which outputs:
1
2
3
4
5
6
7
Now what I want is to add all the previous numbers on each loop. So the output should be:
1
2 // add all above to get this number
3 // add all above to get this number
6 // add all above to get this number
12 // add all above to get this number
24 // add all above to get this number
48 // add all above to get this number
96 // add all above to get this number
...etc
The first and second number doesn't necessarily have to be in the loop, that can be defined manually outside.
What I don't want is to add the value of $i on each loop, but to add all the previous numbers on each loop.
I have tried summing up using this code:
$sum = 0;
for($i = 1; $i <= 5; $i++) {
$sum = $sum + $i;
echo $sum . "<br>";
}
But I get this output:
1
3
6
10
15
21
28
How can I achieve my desired output?
Try this
<?php
$results = [];
for ($i = 0; $i <= 7; $i++){
$currentResult = 0;
if ($i < 2){
$currentResult = $i+1;
}
else{
foreach($results as $currenNumber){
$currentResult += $currenNumber;
}
}
echo $currentResult . '<br>';
$results[] = $currentResult;
}
?>
<?php
$value = 0;
for($i = 1; $i <= 8; $i++) {
if($value < 3){
$value = $value + 1;
} else{
$value = $value * 2;
}
echo $value . '<br>';
}
?>
<?php
$numbers = array(1,2,3,4);
$total = count($numbers);
$sum = 0;
$output = "";
$i = 0;
foreach($numbers as $number) {
$i = $i + 1;
if ($i < $total) {
$sum = $sum + $number;
}
}
echo $sum;
?>
I'm going through PHP on TeamTreehouse.com, whilst learning php this was one of the quiz question, the answer is 6. I don't know why the answer is 6, can someone explain?
The variable $i is initialized by 0 (zero).
Before the condition if ($i < $total) is tested $i is incremented by 1. So even the first time it equals 1.
In the third pass $i equals 3, and in the fourth pass it equals 4 which is NOT < $total.
Therefore only 3 of the 4 elements of $numbers are summed up: 1 + 2 + 3, which equals 6.
See the comments in the code below:
<?php
$numbers = array(1,2,3,4);
$total = count($numbers); // Gives 4
$sum = 0;
$output = "";
$i = 0; // $i = 0
foreach($numbers as $number) {
$i = $i + 1; // $i = 1, even at the first time
// after 3 passes $i is equal to $total (=4)
if ($i < $total) { // So, only 3 of the 4 elements of $number are honored
$sum = $sum + $number;
}
}
echo $sum; // Thus $sum = 1 + 2 + 3 = 6
// The last element (=4) is never summed up
?>
This would sum up all 4 elements, giving 10 as the result:
foreach($numbers as $number) {
if ($i < $total) {
$sum = $sum + $number;
}
$i = $i + 1;
}
<?php
$numbers = array(1,2,3,4);
$total = count($numbers); #value of $total is 4 here
$sum = 0;
$output = ""; #intital empty
$i = 0;
foreach($numbers as $number) {
$i = $i + 1;
if ($i < $total) {
$sum = $sum + $number;
}
}
echo $sum;
?>
You increment the $i by 1 so it becomes 1 which is smaller than $total(which is 4). Your program will add till $i become 4. It just add first three number in your array.
1+2+3=6.
That's why you are getting 6.
I hope you get it. :)
The condition limits the iterations. When $i is 4 it is no longer less than $total and so the last number doesn't get added.
I'm new in PHP. How can i achieve continously loop with adding different value?
it's something like this
<?php
$gap1 = 2;
$gap2 = 3;
$lenght = 10;
for( $i=0; $i<$length; $i++ )
{
//the code
}
?>
and the result will be : 0 2 5 7 10 12 15 17
thank you for your help :)
$gap1=2;
$gap2=3;
$lenght = 10;
$p=0;
for($i=0;$i<$lenght;$i++)
{
if($i==0){$p=0;}
elseif($i%2==0)
{
$p+=$gap2;
}
else{
$p+=$gap1;
}
echo $p.'<br>';
}
Try this code:
$gap1 = 2;
$gap2 = 3;
$length = 10;$i=0;
$x = 0;
while($i<$length)
{
echo $x." ";
if($i%2 == 0)
$x+=$gap1;
else
$x+=$gap2;
$i++;
}
Output:
0 2 5 7 10 12 15 17 20 22
$gap = array(2, 3);
$result = array(-1 => 0);
$length = 10;
for($i = 0; $i < $length; $i++) {
$result[] = $result[$i-1] + $gap[($i) % count($gap)];
}
echo implode(' ', $result);
Just started learning. Here's what I have:
<?php
$i = 0;
$num = $i * 12;
for ($i=0; $i<13; $i++) {
echo($i." times 12 = ".$num."<br>");
}
?>
The outcome should be:
1 times 12 = 12
2 times 12 = 24
3 times 12 = 36
etc...
The outcome I actually get is:
1 times 12 = 0
2 times 12 = 0
3 times 12 = 0
any ideas?
It is because you have this declaration before for loop:
$i = 0; $num = $i * 12;
so always $num will be 0. Just place it into for:
for ($i=1; $i<13; $i++) {
$num = $i*12;
echo($i." times 12 = ".$num."<br>");
}
You don't need declare $i variable before for loop. This variable will be overwritten. There is simple test:
$i = 5;
for($i = 1; $i<10; $i++);
echo $i;
OUTPUT:
10
<?php
$i = 1; $num = 1;
for ($i=1; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
If you want that result you should put the calculation inside the for loop and start i with 1
<?php
for ($i=1; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
Something like this
<?php
$num = 12;
for ($i=1; $i<13; $i++) {
echo("$i times 12 = ".$num*$i);
echo "<br>";
}
?>
<?php
for ($i=0; $i<13; $i++) {
echo($i." times 12 = ".$i*12."<br>");
}
?>
Line $num = $i * 12; shift into loop
<?php
$i = 0;
for ($i=0; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
?>
Your $num variable only ever increments on $i when its 0, try putting it in the for loop like this.
$i = 0;
for ($i=0; $i<13; $i++) {
$num = $i * 12;
echo($i." times 12 = ".$num."<br>");
}
<?php
$B = array(
0=>1,
1=>2,
2=>3,
3=>4,
4=>5,
5=>6,
6=>7,
7=>8,
8=>9,
9=>10,
10=>11
);
function pagination_from_array($arr, $show_per_page, $page=1){
$start = $show_per_page * ($page-1);
$end = $show_per_page * $page;
for($i = $start; $i < $end; $i++){
echo ">>>".$arr[$i]."<br>";
}
if($end-1 < count($arr)) {
echo '............';
}
}
pagination_from_array($B , 6, $_GET['page']);
/*
//Dislay in html table
//=> page1
key | value
0 1
1 2
2 3
3 4
4 5
5 6
........
//=> page 2
key | value
6 7
7 8
8 9
9 10
10 11
total 1+2+3+..+11
*/
?>
Could anyone help me to implement this?
Here is your problem: $i is a negative number as $show_per_page * ($page-1); equals -6
So when your referencing $arr[$i] it's not displaying anything because there is nothing at index -6, You could try something like the abs(), Example:
for($i = $start; $i < $end; $i++){
echo ">>>".$arr[abs($i)]."<br>";
}
UPDATE:
Well it's actually this: $_GET['page'] that's causing the negative value for the index in your example.
UPDATE #2:
Well I went along and quickly created this, hope this gets you started:
// Page Count
$page_count = 100;
// Build the array
for($p = 1; $p <= $page_count; $p++) {
$pages[] = $p;
}
// Print the array for testing
//echo print_r($pages, true)."\n";
function pagination_from_array($arr, $show_per_page, $page=1){
$total_pages = count($arr);
$paginate_total_pages = $total_pages / $show_per_page;
$start = $show_per_page * ($page-1);
$end = $show_per_page * $page;
//echo "Start: ".$start."\n";
//echo "End: ".$end."\n";
//echo "Total: ".$total_pages."\n";
//echo "Pageinate: ".$paginate_total_pages."\n";
//echo "Page: ".$page."\n";
if(($paginate_total_pages) + 1 < $page) {
return; // no pages to display
}
if($total_pages < $start) {
return; // no pages to display
}
for($i = $start; $i < $end; $i++){
if(array_key_exists(abs($i),$arr)) {
echo ">>>".$arr[abs($i)]."<br />\n";
}
}
if($end-1 < count($arr)) {
echo "............<br />\n";
}
}
$display_pages = 6;
$pages_to_display = (count($pages) / $display_pages) + 1;
echo "Pages to display: ".$pages_to_display."\n";
for($d = 1; $d <= $pages_to_display; $d++) {
pagination_from_array($pages,$display_pages, $d);
sleep(1);
}