Check if a number completely divides by 4 - php

I need to know if a changing number divides completely by 4. this is the code that I am currently using:
if ($ResultsCounter %4 != 0){
$htmlResult .= "</div><div class='row'>";
}
Problem here is that if the number is greater than 0 it will always divide by 4. I need this to run if the result is a whole number... i.e. 4, 8, 12, 6...

Change from
if ($ResultsCounter %4 != 0){
to
if ($ResultsCounter %4 == 0){

Related

Pagination of PHP echoed Content

PHP is an alien language for me. I am trying to pull some fields from WP's SQL database.. The content comes out okay but is a lot. I want to somehow put it in a HTML carousel or some slider, where data must be formatted like this:
<holder1>
<data1></data1>
<data2></data2>
<data3></data3>
</holder1>
<holder2>
<data4></data4>
<data5></data5>
<data6></data6>
</holder2>
I imagine I would have to put some for loop for i<4, do, then break into holder2.
Current script that echos the data =
while($row = mysql_fetch_array($rs)) {
echo "<div class='testimonials flexslider'><ul class='slides'><li class='testimonial flex-active-slide'><blockquote><p>" . $row['post_content'] . "</p><cite><span>" . $row['post_title'] . "</cite></span></blockquote></li></ul></div>"; }
I would like it to break after 3 <li> items each, into a seperate <div> or <article> whatever I find suitable according to the carousel I use.
You need an index variable, which echoes the beginning of the wrapper when it is divisible by 3, and the end of the wrapper when it leaves a remainder of 2 after division by 3 (and at the very end). It seems like this code would work:
$index = 0;
while($row = mysql_fetch_array($rs)) {
if ($index % 3 == 0) {
echo "<div class='testimonials flexslider'><ul class='slides'>";
}
echo "<li class='testimonial flex-active-slide'><blockquote><p>" . $row['post_content'] . "</p><cite><span>" . $row['post_title'] . "</cite></span></blockquote></li>";
if ($index % 3 == 2) {
echo "</ul></div>";
}
$index++;
}
if ($index % 3 != 2) {
echo "</ul></div>";
}
EDIT: a little bit of explanation to clarify the math. Suppose you have 10 results. Because we usually count from 0 in programming, they can be numbered 0 up to 9. This would make your structure look like this:
<holder>
<data0></data0>
<data1></data1>
<data2></data2>
</holder>
<holder>
<data3></data3>
<data4></data4>
<data5></data5>
</holder>
<holder>
<data6></data6>
<data7></data7>
<data8></data8>
</holder>
<holder>
<data9></data9>
</holder>
You see that we need a <holder> before elements 0, 3, 6 and 9 - all numbers which are divisible by 3. Mathematically, this is expressed with help of the remainder function - a number is divisible by 3 when its remainder after dividing by 3 is zero.
Likewise, we need a </holder> after elements 2, 5 and 8 - numbers which after division by 3 leave a remainder of 2.
We need to take care of the situation where the last block is not complete; that's why there's an extra block of code to take care of the last </holder>.

Every 3rd Iteration of PHP Loop

When I say 'Every 3rd Iteration' I mean 'Every 3rd Iteration... starting from number 4'
If it were just every 3rd, I would target the appropriate iterations like so:
<?php if ($count % 3 == 0) : ?>
Bear in mind I've set $count to 1 beforehand
But the iterations need to start with the 4th, then 7th, 10th etc.
Does anyone know how I can acheive this? Can I do it with a simple if statement like the one above?
Unfortunately I don't think a for loop will be possible as this is a WordPress loop I'm dealing with
Your thoughts about using the modulus operator are sound, you just need to expand the scope of your logic surrounding it:
You want every third iteration after the initial 4 have passed. Begin your logic after the first 4:
if ($count > 4)
Then, you want each 3 after that. Your counter includes the initial 4 iterations you didn't want to include, so remove that from your counter and check if the current iteration is a multiple of 3:
if (($count - 4) % 3 === 0)
This should give you what you're looking for.
if ($count > 4)
{
if (($count - 4) % 3 === 0)
{
...
}
}
You can also put this into one line:
if ($count > 4 && ($count - 4) % 3 === 0)
{
...
}
foreach($array as $key=>$value){
if($key % 3 != 0){
continue;
}
}

Help needed improving a foreach loop

I have this logic written that loops out an array into <li>
and gives #1 and every 5th a class of "alpha".
$count = 0;
foreach($gallery->data as $row){
if ($count==0 || $count%4==0) {
echo '<li class="alpha"></li>'.PHP_EOL;
} else {
echo '<li></li>'.PHP_EOL;
}
$count++;
}
I need to add to this and get the code adding a class of "omega" to every 4th <li>
You do realize that as you describe it, there will be some overlap right? (example - item 30 is both a '5th' and a '6th') Brian gave you an answer for exactly what you described, but I'm not sure if its what you want. You want ALPHA, x, x, x, OMEGA, ALPHA, x, x, x, OMEGA, ALPHA.....
You seem to want Alpha on the 5*k + 1, and Omega on 5*k
conditions:
alpha - ($count + 1) % 5 == 1
omega - ($count + 1) % 5 == 0
I think grouping in the addition makes this easier to understand, since you're count starts at 0 but you seem to be thinking in terms of beginning at 1. If you don't like that, lose the addition and change the equivalences to 0 and 4, respectively - $count % 5 == 0 and $count % 5 == 4
i know this is better suited for a comment under the last answer, but I don't see how. Am i not allowed until my reputation is higher or am i just missing something?>
right now as it stands your code adds the "alpha" class to every fourth item beginning with the first, not every fifth. In other words, items 1, 5, 9, 13, etc. will have a class of "alpha" since your counter begins at 0.
I assume that you want to add the "omega" class then to items 4, 8, 12, etc. Here's what you need to do that:
$count = 0;
foreach($gallery->data as $row){
if ($count%4==0) {
echo '<li class="alpha"></li>'.PHP_EOL;
}
else if ($count%4==3) {
echo '<li class="omega"></li>'.PHP_EOL;
}
else {
echo '<li></li>'.PHP_EOL;
}
$count++;
}

php if iterator divisible by statement for dynamic columns

Im trying to make dynamic column list that will be 4 columns total (PHP). Im echoing an array and after every time 4 array items are echod, I would like to wrap those 4 array items in a div called "column".
So basically, I thought I could do this with a self counting $i++ statement, but first off I'm having trouble starting the count from anything but zero (I tried setting the variable initially outside of the for each loop.)
Anyways, ya, if you could kindly show me how to check to see if the $++ is divisible by 4 in php, so that I can insert a if $i++ is divisible by 4 then echo "" , it would be much appreciated. But first I believe I need to figure out how to start the counting at 1 (so that the if $i++ is divisible by 4 would work... right??)
If you divide by 4 it will be integer division and the quotient will be the factor of 4. What you probably want is the modulus operator %. It will give you the remainder. So when $i is a multiple of 4 it will be 0.
if (($i % 4) == 0) {
// evenly divisible by 4 logic
}
Modulus can be inefficient. Since you are dividing by a multiple of 2, you could shift bits right by 2. It is the same as dividing by 4 and much more efficient. Check out bit shifting.
% is the modulus operator. It returns the remainder after a division, so 7 % 4 == 3.
You really should start from 0. This is because 0 % 4 == 0 and 4 % 4 == 0... AND you want the very first item to be a new row! So you want new rows on 0, 4, 8, etc... If you start at one, then 1, 2, and 3 would not be in a row.
Additionally, we have to remember to close the row on every item one before a new row.
Finally, if we exit our loop without closing the final row, we have to do that after we have exited the loop.
I'll show how to do this with tables, but you can use divs with classes just as easilly.
<table>
<?php
// Start at zero, so we start with a new row
// Since we start at 0, we have to use < number of items, not <=
for ($i = 0; $i < $numberOfItems; ++$i)
{
// if divisible by 0 start new row
if ($i % 4 == 0)
echo '<tr>';
// Always print out the current item
echo '<td>' . $item[$i] . '</td>';
// If the next $i is divisible by 4, we have to close the row
if ($i % 4 == 3)
echo '</tr>';
}
// If we didn't end on a row close, make sure to close the row
if ($i % 4 != 3)
echo '</tr>';
?>
</table>
Modulus!
$a % $b Remainder of $a divided by $b.
http://php.net/manual/en/language.operators.arithmetic.php

Find multiples of a number in PHP

I want to find all muliples of a number in PHP.
I'm using something like this
if($count != 20 )
to work out if $count is not equal to 20.
But I also need this script to check if $count is not equal to 20, 40, 60, 80, 100, 120, 140, 160 etc.
Any ideas? I think i need to use the modulus symbol (%), but I don't know.
if ($count % 20 != 0)
if ($count % 20 != 0)
{
// $count is not a multiple of 20
}
If you don't want zero to be excluded:
if ($count % 20 != 0 || $count == 0)
You can do it like so:
if($count % 20 != 0)

Categories