How to display each number of a variable? - php

basically what I want is display a count until it reaches a certain number, so if I had the number "5" on the screen it would show "1 2 3 4 5". Or if I had the number "3" it would show "1 2 3".
The reason is because im creating a paging system for my MySQL results. The code I have so far is
$result1 = mysql_query("SELECT * FROM questions WHERE subcat = '$conditions'");
$num_rows = mysql_num_rows($result1);
$results_per_page = "3";
$num_pages = $num_rows / $results_per_page;
So it counts how many results there are, for example we will say 12 results. It then devides this number by how many results I want shown per page. In this example its "3". So the answer is "4".
So I now want to have "1 2 3 4" displayed on the screen, however I want each number to be a link.
How do I do this?
Thanks
Ben

foreach( range( 1, $num_pages) as $i) {
echo '' . $i . '';
}
Or, an approach suggested by knittl:
echo implode( ' | ', array_map( function( $i) {
return sprintf( '%d', $i, $i);
}, range( 1, $num_pages)));
Prints something like this:
1 | 2 | 3 | 4 | 5

Use a for loop.
for($i = 1; $i <= $num_pages; $i++){
echo ''.$i.' ';
}

Use a loop:
for($i = 0; $i < $num_pages; ++$i) {
printf('page %d', $i, $i);
}

Related

Get numbers next and after a certain number with dynamic max results with PHP

I have a pagination which renders some pages. I have a setting where the user can define how many pages are to be displayed as numbers in the pagination nav. The first and last number are always visible. Now if the user wants to have 3 numbers on the nav and assuming that i am now in the page 7, then it should look like this:
1 ... 6 7 8 ... 12
If the user wants four items then it should look like that:
1 ... 6 7 8 9 ... 12
Up until now i have the following which gives me 3 before and after the current page
$maxLinks = 3;
$currentPageNumber = 7;
$pages = [];
$pages[] = 1;
for($i = max(2, $currentPageNumber - $maxLinks); $i <= min($currentPageNumber + $maxLinks, 12 - 1); $i++) {
$pages[] = $i;
}
$pages[] = 12;
foreach ($pages as $key => $page) {
$newPage = '...';
if (($key === 0) && $pages[1] !== $page + 1) {
array_splice( $pages, 1, 0, $newPage );
}
$itemBeforeLast = count($pages)-2;
if (is_numeric($pages[$itemBeforeLast]) && ($key === $itemBeforeLast) && $pages[$itemBeforeLast + 1] !== $pages[$itemBeforeLast] + 1) {
array_splice( $pages, $itemBeforeLast +1, 0, $newPage );
}
}
This gives me back the following:
But i only want to get 3 or 4 numbers between the dots (this changes based on the value that the user gives in the settings ($maxLinks variable))
Any help is deeply appreciated
Best regards
As you add them to both sides, you need to divide by 2. Also, remove 1 to account for the current page. Then you just need to account for the possibility of having a non-even number of links to the left&right by rounding (down for the left, up for the right).
And end up with:
for($i = max(2, $currentPageNumber - floor(($maxLinks-1)/2));
$i <= min($currentPageNumber + ceil(($maxLinks-1)/2), 12 - 1); $i++)

How to calculate the average progressively without know the total items?

(Language PHP - This question is for any language, particularly I'm using PHP)
For example you have an array of numbers like:
$q = array( 1, 2, 3, 4, 5, ... ); // ... mean you can give more numbers
$i = 0;
$currentAverage = 0;
while ($i < count( $q )) {
$currentAverage = ($currentAverage + $q[$i]) / 2; // this is my try
$i++;
}
echo "The final average is: " . $currentAverage . "<br/>";
Obviusly, you can divide by count( $q ) the sum, but that's not the idea.
I hope you can help me! thanks.
You can't calculate an "incremental" mean average without knowing the total number of items make up that average.
For example, if you have 10 items that average 5 and you want to add the next item, X, you have to give the appropriate "weight" to the newly added item.
For example, to get the next average, you would do
(currentAverage * currentNumberOfItems + X) / (currentNumberOfItems + 1)
If we say X is 7, the new average would be
(5 * 10 + 7) / (10 + 1)
= (50 + 7) / 11
= 57 / 11
= 5.181818182
It is impossible to do this calculation without knowing the current number of items that make up the average (10) beforehand
To show you this working in an incremental fashion, here is a for loop that keeps track of the average as the loop is running
$xs = [1,2,3,4,5];
$average = $xs[0];
for ($count = 1; $count < count($xs); $count++) {
echo sprintf("average: %0.3f, count: %d" . PHP_EOL, $average, $count);
$average = ($average * $count + $xs[$count]) / ($count + 1);
}
average: 1.000, count: 1
average: 1.500, count: 2
average: 2.000, count: 3
average: 2.500, count: 4
Could use this:
$q = array( 1, 2, 3, 4, 5, ... ); // ... mean you can give more numbers
$i = 0;
$currentAverage = 0;
while ($i < count( $q )) {
$sliceArr = array_slice($q, 0, $i+1);
$currentAverage = array_sum($sliceArr) / count($sliceArr);
$i++;
}
echo "The final average is: " . $currentAverage . "<br/>";

PHP.. How to loop by some selected number?

I need a output like this
Number 5
Number 4
Number 9
Number 3
Number 8
Number 10
And so on (There are more like this)
I used this code
<?php
for ($i = 0; $i <= 10 ; $i++) {
if ($i == 5 || $i == 4 || $i == 9) { //And so on Like this
echo "$i<br>";
}
}
?>
But the main problem is output is showing the number serially.
//It shows
Number 3
Number 4
Number 5
Number 8
Number 9
Number 10
//But I need
Number 5
Number 4
Number 9
Number 3
Number 8
Number 10
And this takes much time to code. And it not looks so good. Sure there is a easy way out!
I am expecting something like this -
//Surely this is not right. It's just an idea.
<?php
$x = 5,4,9,3,8,10;
for ($i = 0; $i = $x; $i++) {
echo "$i<br>";
}
?>
Take this
$x = array(5,4,9,3,8,10);
foreach ($x as $i) {
echo "Number $i<br>";
}
but please, learn the basics of PHP if you really want to code in php.
JustOnUnderMillions's answer is corrent - You can even have access to key & value like this
$x = array(
"num1" => 1,
"num2" => 2,
...
...
);
foreach($x as $key => $value){
echo $key . " : " . $value . "<br>";
}

Counter inside while loop every X times increase

I'm not really sure how to work this question, but I currently have a while loop outputting <li></li>.
Let's say that there are 35 rows and I want the counter to increase every five times.
So the output would be something like this.
- 1 Name
- 1 Name
- 1 Name
- 1 Name
- 1 Name
- 2 Name
- 2 Name
- 2 Name
- 2 Name
- 2 Name
- 3 Name
- 3 Name
- 3 Name
- 3 Name
- 3 Name
- 4 Name and so on...
I've tried counting throughout the loop and comparing the number to see if it was less than five and if not then increasing it, but I know that's not correct. Just can't seem to figure out the best solution.
while ($stmt->fetch()) {
$HTML .= "<li data-id='$id' data-name='$name'>$count Name</li>";
}
To try to make this clearer...basically I would like to have a counter variable running. Starting at 1, but every fifth time through the while loop, I would like to increase this count variable by one.
$count = $rows = 0;
while ($stmt->fetch()) {
if ($rows % 5 == 0)
$count++;
$rows++;
$HTML .= "<li data-id='$id' data-name='$name'>$count Name</li>";
}
You could use array_fill() (psuedo code):
<?php
$li = "<li>Item</li>";
$row = array_fill(0, 5, $li);
$list = array_fill(0, 35, $row);
print_r($list);
?>
http://codepad.org/ETCv3GBK
As in:
$count = 0;
while ($stmt->fetch() && $count++) {
$HTML .= implode('', array_fill(0, 5, "<li data-id='$id' data-name='$name'>$count Name</li>"));
}
Another demo (ignite.io may not be working on save, though):
https://ignite.io/code/514a9bf5ec221ee821000005
for($i=1; $i <= 20; $i++){
$array[] = "Name " . $i;
}
$count = 1;
$output = 1;
for($i=0; $i < 20; $i++){
if($count++ < 5)
echo $output . ". " . $array[$i] . "<BR>";
else{
$count = 1;
echo $output++ . ". " . $array[$i] . "<BR>";
}
}
returns
Name 1
Name 2
Name 3
Name 4
Name 5
Name 6
Name 7
Name 8
Name 9
Name 10
Name 11
Name 12
Name 13
Name 14
Name 15
Name 16
Name 17
Name 18
Name 19
Name 20
$i=1
while ($stmt->fetch()) {
if($i%5==0){
$HTML .= "<li data-id='$id' data-name='$name'>$count Name</li>";
}
$i++;
}

PHP Simple Loop breaking at a certain number

I have a loop I want to break after a certain number. So I want display link every 24 records.
for ($i=0; $i<=$TotalProductsString; $i+24)
{
echo "Page link" . $i . "";
}
If $TotalProductsString=52 then it would display the loop 3 times as 24 can only go into 52 around 3 times.
Your loop should look like this:
for ($i=0; $i<=$TotalProductsString; $i+=24)
{
echo "Page link " . $i;
}
try
for ($i=0; $i<=$TotalProductsString; $i =$i+24)
{
echo "Page link" . $i . "";
}
Use the modulo operator for that:
$i = 1;
foreach($rows as $row) {
// do your row stuff
if($i % 24 == 0)
echo "Page link" . $i . "";
$i++;
}
Not sure what the question is.
Instead of increasing $i by 24 you can also increase by 1 and check if $i meets the number you are looking for. For example,
for ($i=0; $i<=$TotalProductsString; $i++)
{
// if the remainder is 0 when dividing $i by 24
if ($i % 24 == 0) {
echo "Page link" . $i . "";
}
}
have a loop I want to break after a certain number. So I want display
link every 24 records.
You wanna break or you wanna display the link for every 24 records or you wanna to make the two things?
If $TotalProductsString=52 then it would display the loop 3 times as
24 can only go into 52 around 3 times.
24 x 3 = 72, so, 24 can only go 2 times if $TotalProductsString is 52.
If you want to display the page for every 24 records, you could simple put this inside of your "for":
if ($i % 24 == 0) {
echo "Page link" . $i . "";
}
and use $i++ instead of use this: $i+24;

Categories