PHP programming

Posted on 2012-08-28
Last Modified: 2012-12-13
Hi experts.

I have this particular request:
Customer registers and pay via payment gateway.

I need every 5th registrant/payment to be free.

So, 1st, 2nd, 3rd, 4th, pays, 5th free of charge. And then 6th, 7th, 8th, 9th, pays, 10th is free of charge.

I just couldn't figure out the programming logic. Any sample code that I could rely on for this?
Question by:rolandmy
    LVL 25

    Assisted Solution

    I am assuming these registrant's are separate people/separate sessions.  If so - each execution of the PHP code will be different from the others and you will need to keep a counter of the number of registrants elsewhere that can be accessed by the PHP code ... a database table, a file of some form.  Depending on how 'busy' these registrations will be - you may need to consider concurrency and locking.
    But basically you would
    a) determine the number of the current registrant ("select count(*) from ...", read from the file...etc)
    b) determine if 5th by using the % (modulus) operator
    <?php $rem = $count % 5; if ( $rem = 0 ) echo "you are the 5th"; ?>
    LVL 17

    Assisted Solution

    I'd say checking for concurrency/race conditions isn't a "may need to" but necessary even with low volume sites ... doing a simple count will not be safe enough ... also, you'll need to decide upon the logic of assigning the free registrations - at what point is the free registration (or the sale in general) counted and price finalized? What happens if a registrant cancels at some point of the process, how are subsequent registrants handled that started afterwards, but went through with the registration?
    I don't know your experience level in progamming - or rather designing - but maybe taking a piece of paper (or Visio etc.) and plotting out some sort of flow chart of the registration and decision process would help you (and maybe give people on here an idea what to suggest as for the program logic and implementation)
    LVL 107

    Accepted Solution

    Insert each registration into a data base table.  You do not need to lock the table or be concerned about race; just get the AUTO_INCREMENT id back with something like mysql_insert_id()  Then use the remainder (see above) to see if this is one of the 5th registrants.  If the remainder is zero, the price is zero.  All others pay.
    LVL 2

    Author Comment

    Didn't use.
    LVL 107

    Expert Comment

    by:Ray Paseur
    @rolandmy: Withing less than 24 hours of posting this question, you received exactly the correct answers here.  Yet you ignored the question for months, never responded and then gave your fellow experts the worst possible grade anyone can give at EE.  I think we are entitled to an explanation.  Please read the grading guidelines and then tell us why you did that.  Thanks, ~Ray
    LVL 2

    Author Comment

    Ooops. I didn't read the guidelines. My bad. I apologize. As I didn't use the solution, I thought I'd just rate it as a C. And I didn't put it into knowledge base.

    Won't happen again since I already read the guidelines as below.

    An A grade should be given if you receive the solution from the Experts; you should consider the A grade the default unless it is deficient.
    A B grade should be given if the solutions require you to do a good amount of extra work to resolve your problem.
    A C grade is the lowest possible grade you can give. Experts should always be given the opportunity to improve it -- but that does not mean they should rewrite your entire application for you.

    It is customary to explain any grade that is not an A.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now