PHP Modulus Operator – mysql database example

Posted By: Ian on Feb 02, 2012 in Blog, Snippets
Last modified on February 26th, 2014 at 9:56 pm,

OK well recently whilst doing some work for someone on a classifieds site the man wanted a banner from Banner Bar to show every 5 adverts. But he wanted the banners to be shown in ascending order and only 1 at a time. After some playing around and a nights sleep i finally cracked it now i share this with you in the hope it may help you out.

This is the first time i’ve used the PHP Modulus Operator % which to my understanding is like divide.
As if($i % 3 == 0) means if $i divided by 3 is clean (Only whole numbers).
1 ÷ 3 = decimal numbers
2 ÷ 3 = decimal numbers
3 ÷ 3 = whole numbers so carry on with if statement

(Well this is my interruption of it)

Please see the demo which pulls chat up lines from a table and images from another and each one is shown in ascending order.

Try Me

AnonymousIan.J.Gough

Did this help you? Please Let me know by commenting below even if it’s just to say “Thanks”

Discussion - One Comment

  • Karl Karl Nov 28, 2012 

    I was looking for a way to alert a user when their post count increased by 100. if($post % 100 == 0) worked for me.

    Thanks my friend!

    Reply

leave a comment

About This Site

Dreams are built from lines of code well it's true!
If you can dream it you can usually code it.

I have learned so much from other people on the Internet and this site is for me to give back some of what i have learned and hope people can now learn from me.
Have fun and surf safely,
Ian.J.Gough

protected by copyscape duplicate content check

Categories


Powered by Banner Bar 125