Classified Ad Order Form - Calculate Total by Word Count

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
itsinmyhead
Forum Commoner
Posts: 25
Joined: Wed Aug 13, 2008 11:33 am

Classified Ad Order Form - Calculate Total by Word Count

Post by itsinmyhead »

The company I'm working for is starting to get its web-legs under it, and they want to allow customers to place classified ads through their website. I've not setup an e-commerce site or anything of that nature before, so I thought I'd ask around here for some help. I'm not even sure that what I'm looking for is a PHP function, but it may be. If it isn't, hopefully someone can point me in the right direction.

Here are the basics:

-We charge $39.95 to place a classified ad for up to 20 words. Every word after 20 is an extra twenty cents. I would like to find a way to have the user type their ad into a textbox and have the word count & total cost update as they go. I came across a simple jQuery word count plugin (http://roshanbh.com.np/2008/10/jquery-p ... tarea.html) that counts the words as you go. I'm not sure how to go about the next step, though - displaying the final cost of the ad which will also update with each new word.

-I think the easiest way to set up ordering right now is through a PayPal Business Account. I'm not sure how to take the total we would create in that first step and push it through to PayPal once a user is ready to click the "Pay Now" button. Would it be reaching for the cost of the "item" from a PHP database? And, if so, how do we go about updating that price for those extra words?

Like I said, I'm not sure if this is a PHP function, something to be accomplished through JavaScript or a combination of a few elements. Any help is greatly appreciated.

Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Classified Ad Order Form - Calculate Total by Word Count

Post by requinix »

- A jQuery plugin is great. If that works, use it. But when the thing is submitted calculate the number of words in PHP and use that figure.

- Yes, a business account. The person fills in the form and submits: you then display a confirmation page giving whatever they need to see. They click a button to confirm which sends them to PayPal. They complete the process and return to your site.
I suggest PayPal IPN for the rest.
itsinmyhead
Forum Commoner
Posts: 25
Joined: Wed Aug 13, 2008 11:33 am

Re: Classified Ad Order Form - Calculate Total by Word Count

Post by itsinmyhead »

I've looked at some PHP word count stuff, but what I've seen seems to have issues discerning between words/spaces/punctuation. If I use a jQuery word count plugin, is there a way to tell the PHP to pull the word count from that jQuery script?

At that point, would it be as simple as something like...
if the number of words is less than/equal to 20, total price is $39.95
else if the number of words is greater than 20, total price is $39.95 + .20 per word over 20

I say simple, of course, because it sounds simple to write it out like that. Actually getting it to work would be another story.

What I'm hoping for is this:
First page of ad placement: Form with textbox and jQuery word count
Second page: Total cost of ad processed with PHP after pulling word count
Third page: This would be the PayPal page, I suppose, with page 2 being the exit page with the "Pay Now" link

Does this make sense?

Edit: I'm also wondering, would I need to create a MySQL database for this? Or would that make it easier? A database with two fields, basically - basicprice (which would be a value of $39.95) and extraword (which would be a value of .20) for the PHP to pull from and then give a total?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Classified Ad Order Form - Calculate Total by Word Count

Post by requinix »

Word counting can be difficult because you haven't decided what a word is. As far as you/your business is concerned, what is a word? How does punctuation fit into it?
Come up with a clear policy and the code to count words will be easy.
itsinmyhead wrote:I've looked at some PHP word count stuff, but what I've seen seems to have issues discerning between words/spaces/punctuation. If I use a jQuery word count plugin, is there a way to tell the PHP to pull the word count from that jQuery script?
Yes, but you shouldn't. I, being a smart and sometimes malicious user, would type my message and then set the word count myself. It's possible because the word-counting is done on my machine - which I control. That's why you count words on the server - it's your machine, you control it.

JavaScript should only ever be for the user's convenience. Nothing more.
itsinmyhead wrote:What I'm hoping for is this:
First page of ad placement: Form with textbox and jQuery word count
Second page: Total cost of ad processed with PHP after pulling word count
Third page: This would be the PayPal page, I suppose, with page 2 being the exit page with the "Pay Now" link

Does this make sense?
Yeah, that's about it.
itsinmyhead wrote:Edit: I'm also wondering, would I need to create a MySQL database for this? Or would that make it easier? A database with two fields, basically - basicprice (which would be a value of $39.95) and extraword (which would be a value of .20) for the PHP to pull from and then give a total?
No need. In fact, it would only make things more difficult. Should the price change in the future you can simply edit the script. A couple seconds at most.
itsinmyhead
Forum Commoner
Posts: 25
Joined: Wed Aug 13, 2008 11:33 am

Re: Classified Ad Order Form - Calculate Total by Word Count

Post by itsinmyhead »

tasairis wrote:Word counting can be difficult because you haven't decided what a word is. As far as you/your business is concerned, what is a word? How does punctuation fit into it?
Come up with a clear policy and the code to count words will be easy.
A word, for us, is basically anything that requires a space before or after it. The easiest sense of the definition, I guess. If you're abbreviating "condition" as "cond." then that still counts as a word. Abbreviation or full-length word equals one word.

Now, I'm not shy in saying that I don't have loads of experience with this stuff. I'm familiar with PHP and have used it to build a classifieds website before with a search/sort function, but I've never done anything as far as e-commerce or cost-calculating goes. I assume that I'll have to declare at the beginning of the code telling the PHP that a 20-word ad is $39.95, while each word over 20 is an additional $.20. In my head, this sounds easy. In actual practice, though, I'm wagering that it's a bit more difficult than that.

What would be the best way to begin tackling this?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Classified Ad Order Form - Calculate Total by Word Count

Post by requinix »

With such a simple definition words are easy to count.

Code: Select all

$words = preg_match('/\S\s/', trim($string)) + 1;
That counts the number of times it finds a non-space just before a space, then adds one to count the last word in the string.

The cost is also simple:
- If you have up to 20 words, use $39.95
- If you have more, start with $39.95 and add $0.20 for each word over 20

It really is as easy as it first seems. The right bit of code to start and a bit of math after that.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Classified Ad Order Form - Calculate Total by Word Count

Post by John Cartwright »

There is also str_word_count()
Post Reply