Paging from Array and display ....? - php

<?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);
}

Related

Add all the previous numbers in a for loop to get the current number

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>';
}
?>

How to show the next interest rate like image expected below

Expectation
My Code
<?php
$value = 100;
$years = 5;
for ($i = 1; $i <= $years; $i++) {
$income = round($value * (pow(1+6/100, $i)), 2);
echo $i, " ", $income, "<br>";
}
?>
Output
1 106
2 112.36
3 119.1
4 126.25
5 133.82
How can I get result like my expectation above ?
Just add one more loop
$value = 100;
$years = 5;
for ($i = 1; $i <= $years; $i++) {
$income = array();
for ($j = 6; $j <= 10; $j++) {
$income[] = round($value * (pow(1+$j/100, $i)), 2);
}
echo $i, " ", implode(' ', $income), "<br>";
}

continously loop with adding different value

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);

Calculate a specific number PHP

I have a for loop, where $i is 0, and it will run until $i reaches 4. I am trying to make a code that would output numbers in an order like this: 01, 11, 02, 12, 03, 13... etc... Now, the thing is next: when $i is 1, the script should make an order of those number in the boundaries of 1 and 20. When $i is 2, it would be 21 to 40, etc.
I've tried many things (mostly deleted), could not come up with anything that would work the right way.
The inner loop:
for ($j = 0; $j != 10; ++$j)
{
echo $j + 1 + 10 * ($i - 1);
echo $j + 1 + 10 * $i;
}
Try this piece of code;
<?php
$num = 4;
for($i=1;$i<($num + 1);$i++){
$string = "0" . $i . ", 1" . $i;
if($i<$num){
$string .= ", ";
}
echo $string;
}
?>
printf will format your numbers with a leading zero, as specified:
<?php
$format = "%02d ";
for ($i = 1; $i <= 4; $i++) {
$k = 2 * $i - 1;
for ($j = 1; $j <= 10; $j++) {
printf($format, ($k - 1) * 10 + $j);
printf($format, $k * 10 + $j);
}
echo "<br />";
}
?>
You can try:
<?php
$ten = 10;
for ($i = 0; $i<=4; ++$i)
{
echo "0".$i." , ";
echo $ten + $i."<br/>";
}
?>
Only change the range of $i
Thanks

For Loop Custom Fields

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;
}
?

Categories