I am trying to work out how to use the modulus function in PHP with this simple example using a WordPress loop I am wanting to echo out a message every 1, 2 and 3rd iterations of the loop...
if ( $the_query->have_posts() ) {
$counter = 0;
while ( $the_query->have_posts() ) {
$counter++;
$the_query->the_post();
if ($counter % 1 == 0) {
$output .= '1 - ' . $counter . '<br />------------<br />';
} elseif ($counter % 2 == 0) {
$output .= '2 - ' . $counter . '<br />------------<br />';
} elseif ($counter % 3 == 0) {
$output .= '3 - ' . $counter . '<br />------------<br />';
}
}
}
I am seeng this when I run it thoughh..
1 – 1
————
1 – 2
————
1 – 3
————
Can anyone tell me why I am seeing these results? I was expecting to see the echo 1 2 and 3 be output for the 1st second and third row.
Every time you divide an integer by one there is no remainder!
Therefore the line
if ($counter % 1 == 0)
is always true!
Because it always satisfy first condition:
// no matter what value is $counter , it always satisfy that the given value is always divisible by 1 and hence remainder will be 0
if ($counter % 1 == 0) {
$output .= '1 - ' . $counter . '<br />------------<br />';
``
That's why it don't go for other conditions
Related
Hi guys I tried to echo this using php
even odd odd even odd odd
and so on .
My code :
For( $i = 0 ; $i <=11; $i++ ){
If( $i % 2 == 0 ) {
echo " even " ;
}
If( $i % 2 != 0 ) {
echo " odd " ;
$i = $i+1 ;
}
}
And sure the result is :
even odd odd odd ood
and so on .
I wanted to repeat ( even ) one time and ( odd ) twice and so on ..
Can someone help me in that ? Thanks
You can use% operator.
Where any counter that is exactly divisible by 3 is even, other case is odd.
Answer:
for ($i=0 ; $i<=11 ; ++$i) {
if ($i%3==0) {
echo "Even";
}
else {
echo "Odd";
}
}
Hand Run:
$i - $i%3 - Output
--------------------
0 - 0 - Even
1 - 1 - Odd
2 - 2 - Odd
3 - 0 - Even
4 - 1 - Odd
5 - 2 - Odd
6 - 0 - Even
7 - 1 - Odd
8 - 2 - Odd
9 - 0 - Even
10 - 1 - Odd
11 - 2 - Odd
EDIT: Using ternary operator, you can even reduce the lines of code.
Reduced Code:
for ($i=0 ; $i<=11 ; ++$i) {
echo ($i%3==0) ? 'Even' : 'Odd';
}
In this case, you could simply use modulo 3 instead of 2 with a if-else statement
for ($i = 0; $i < 11; $i++) {
if ($i % 3 == 0) {
echo 'even ';
} else {
echo 'odd ';
}
}
I'm not sure if you want the desired behaviour of using % 3 as suggested by the others (even though it gets you the result you want) but i'm not sure you'd get the same 'count'. So I'm just going to simply say, why don't you just write 'odd' in your echo statement twice?
for( $i = 0 ; $i <=11; $i++ ){
if( $i % 2 == 0 ) {
echo " even " ;
}else {
echo " odd odd ";
}
}
If you could help us understand why you want to print odd out twice on the odd numbers that would help? Or whether it doesn't actually matter if i is even or odd itself but just the output you're after (as suggested by using % 3).
I want to show 8 result in single slide and other will be display in next slide. I have done this by following code
$counter = 0;
foreach ($products as $product) {
if(++$counter % 8 === 0) {
$firstSlide = ($counter == 1) ? 'active' : '';
$slideItem = '<div class="item ' . $firstSlide . ' "><div class="fill"></div>';
echo $slideItem;
}
}
Now i want to get count after 8 result. For example
result 1
result 2
result 3
result 4
result 5
result 6
result 7
result 8
Slide 1
result 9
result 10
result 11
result 12
result 13
result 14
result 15
result 16
Slide 2
I have searched so far but couldn't find any solution, please guide me if anyone know how it will be possible.
use following code
$array = array(1,2,3,1,123,123,234,56,67,); // this is your actual array
$arrayChunk = array_chunk($array, 8);
foreach($arrayChunk as $arrayNum=>$chunkArray) {
if(is_array($chunkArray)) {
foreach ($chunkArray as $value) {
print $value.' ';
}
}
print '<br> slide '.$arrayNum.'<br>';
}
Use this code please.
$counter = 1;
foreach ($products as $product) {
if($counter % 8 == 0) {
$firstSlide = ($counter % 8 == 1) ? 'active' : '';
$slideItem = '<div class="item ' . $firstSlide . ' "><div class="fill"></div></div>';
echo $slideItem;
$counter++;
}
}
I have a loop that creates divs with some content from a database. I have a variable $current_count which I'm starting at value '0' which is the first iteration of my loop.
I am using:
if ($current_count == 0 || $current_count % 3 == 0) { echo '<div class="parent">'; }
To create a parent div at the very top of the loop, then again on every iteration divisible by 3. It looks like this (with numbers representing iteration):
0 <div class="parent">
0 <div class="child"></div>
1 <div class="child"></div>
2 <div class="child"></div>
3 <div class="parent">
3 <div class="child"></div>
4 <div class="child"></div>
5 <div class="child"></div>
But the problem is I can't work out how to close those divs, as they would close on different iterations. For example, the parent opened on iteration 0 would need to be closed at the end of iteration 2.
I need to basically say (pseudo-code):
IF $current_count is equal to (division of 3, minus 1) { etc }
I have tried:
if ($current_count % 3 == (0 - 1)) {}
if ($current_count % (3 == 0) - 1) {}
if ($current_count % 3 == 0 - 1) {}
But none of these are returning true. Does anyone know a way I can do this?
Cheers,
Lee.
UPDATE 1: Here's an example of the PHP code currently, to better explain what I'm trying to accomplish:
$current_count = '0';
$ret = '';
foreach ( $brands as $index => $brand ) :
if ($current_count == 0 || $current_count % 3 == 0) {
$ret.= '<div class="parent">'; //Start parent
}
$ret.= '<div class="child"></div>'; //Child
if ($current_count % 3 == (0 - 1)) { // IF LINE 2, 5, 8, 11 etc, NOT WORKING
$ret.= '</div>'; // End the parent
}
$current_count++;
endforeach;
try this,
for($i = 0; $i <= 10; $i++) {
if($i % 3 == 0 && $i > 0)// $i > 0 condition because. 0 % 3 is equal to 0 only.
echo $i - 1;// will echo 2,5,8
echo "</div>";// in your case.
}
if you do it like that it still divided by 3 and not solving the question.
it should be :
if( $key % 3 == 2 ){
</div>
}
I have a pagination script which I have posted below, the problem is I have alot of data so I end with a huge list of pages, I want to make it show only 10 pages at a time and then maybe the last 2 pages like this:
previous 1 2 3 4 5 6 7 8 9...24 25 next
is there anyway I can change the code to do this. Below is the included pagination script I can include the other part of script if needed.
<?php
//source unknown for logic of showPageNumbers()
//modified by drale.com - 1-19-2010
//added query_string reproduction and divs
//added showNext() and showPrev()
class Pagination
{
function getStartRow($page,$limit)
{
$startrow = $page * $limit - ($limit);
return $startrow;
}
function showPageNumbers($totalrows,$page,$limit)
{
$query_string = $this->queryString();
$pagination_links = null;
/*
* PAGINATION SCRIPT
* seperates the list into pages
*/
$numofpages = $totalrows / $limit;
/* We divide our total amount of rows (for example 102) by the limit (25). This
will yield 4.08, which we can round down to 4. In the next few lines, we'll
create 4 pages, and then check to see if we have extra rows remaining for
a 5th page. */
for ($i = 1; $i <= $numofpages; $i++) {
/* This for loop will add 1 to $i at the end of each pass until $i
is greater than $numofpages (4.08). */
if ($i == $page) {
$pagination_links .= '<div class="page-link"><span>' . $i
. '</span></div> ';
} else {
$pagination_links .= '<div class="page-link"><a href="?page=' . $i
. '&' . $query_string . '">' . $i . '</a></div> ';
}
/* This if statement will not make the current page number available
in link form. It will, however, make all other pages available
in link form. */
} // This ends the for loop
if (($totalrows % $limit) != 0) {
/* The above statement is the key to knowing if there are remainders, and it's
all because of the %. In PHP, C++, and other languages, the % is known as a
Modulus. It returns the remainder after dividing two numbers. If there is no
remainder, it returns zero. In our example, it will return 0.8 */
if ($i == $page) {
$pagination_links .= '<div class="page-link"><span>' . $i
. '</span></div> ';
} else {
$pagination_links .= '<div class="page-link"><a href="?page=' . $i
. '&'.$query_string.'">'.$i.'</a></div> ';
}
/* This is the exact statement that turns pages into link
form that is used above */
} // Ends the if statement
return $pagination_links;
}
//added by drale.com - 1-19-2010
function showNext($totalrows,$page,$limit,$text="next »")
{
$next_link = null;
$numofpages = $totalrows / $limit;
if ($page < $numofpages) {
$page++;
$next_link = '<div class="page-link"><a href="?page=' . $page
. '&'.$query_string.'">' . $text . '</a></div>';
}
return $next_link;
}
function showPrev($totalrows,$page,$limit,$text="« prev")
{
$next_link = null;
$numofpages = $totalrows / $limit;
if ($page > 1) {
$page--;
$prev_link = '<div class="page-link"><a href="?page=' . $page
. '&' . $query_string . '">'.$text.'</a></div>';
}
return $prev_link;
}
function queryString()
{
//matches up to 10 digits in page number
$query_string = eregi_replace("page=[0-9]{0,10}&","",$_SERVER['QUERY_STRING']);
return $query_string;
}
}
?>
Untested, but this should always show pages 1 - 3 and the last 3 pages of the list. Otherwise, it will only ever show the previous 3 pages and the next three pages from the current one you're on. (whenever the amount of pages is greater than 10)
$alwaysShowPages = array(1, 2, 3);
// dynamically add last 3 pages
for ($i = 3; $i >= 0; $i--) {
$alwaysShowPages[] = $numofpages - $i;
}
for ($i = 1; $i <= $numofpages; $i++) {
$showPageLink = true;
if ($numofpages > 10 && !in_array($i, $alwaysShowPages)) {
if (($i < $page && ($page - $i) > 3)
|| ($i > $page && ($i - $page) > 3)
) {
$showPageLink = false;
}
}
if ($showPageLink) {
if ($i == $page) {
$pagination_links .= '<div class="page-link"><span>'.$i.'</span></div> ';
} else {
$pagination_links .= '<div class="page-link">'.$i.'</div> ';
}
}
}
I have a version that does this:
1 | 2 | 3 | 4 | 5 | 6 ... 554 | 555 | 556
1 | 2 | 3 | 4 | 5 | 6 ... 554 | 555 | 556
1 | 2 | 3 ... 278 | 279 | 280 ... 554 | 555 | 556
1 | 2 | 3 ... 415 | 416 | 417 ... 554 | 555 | 556
1 | 2 | 3 | 553 | 554 | 555 | 556
The ... are actually links that go halfway between current page and 1st of the next (or last) group of links.
I made it so the 1st 6 pages always appear if current page is less than 5.
But I made all parameters dynamic, so you can change variables like:
$end_links = 3; // set number of links at each end to always show
$center_links; // number of links including current page to show 'floating' in center
It took me hours to do it, which is pretty sad. Oh well.
Just use arrays a lot, and figure out how to add the right values to it. It's simple math and logic, really.
I am trying to get multiple number of 2 and 3 pls help. Thanks
for($i = 0; $i <30; $i++)
if($i % 2)
echo 'number is '. $i . ' is multiple of 2 <br/>';
elseif($i %3)
echo 'number is '. $i . 'is multiple of 3 <br/>';
else
echo 'number is '. $i . 'is multiple of some other number <br/>';
Try this:
if($i % 2 === 0)
...
elseif($i %3 === 0)
...
Basically if the modulo is 0 then that means the number is evenly divisible.
However, another problem with your logic is that a number may be divisible by both 2 and 3. You can fix this by extracting these out into separate if statements:
if($i %2 === 0) {
...
}
if($i %3 === 0) {
...
}
But that sort of breaks your last else since you cannot just fall though to it anymore. You could solve that by setting a variable to false at the top of your loop. Then if any of your if statements is triggered, set the variable to true. Finally, print the "not divisible" message at the end of each iteration if the variable is still false.
You want a NOT if(i%2) because it is a multiple if the remainder is zero.
Additionally if you're trying to find multiples of 30 you only need to loop up to 15. Or number/2.
for($i = 0; $i < 30; $i++)
{
if($i % 2 == 0)
echo 'number is '. $i . ' is multiple of 2 <br/>';
elseif($i % 3 == 0)
echo 'number is '. $i . 'is multiple of 3 <br/>';
else
echo 'number is '. $i . 'is multiple of some other number <br/>';
}
I would add one more condition before your if conditions
if($i%2 === 0 && $i%3 === 0) {
echo 'multiple of 2 and 3\n';
}
It may also be correct to use:
if ($i%6 === 0) {
....
}
for the above if condition.
Even better if you use a switch case block.