Solved

# Working with PHP calculations

Posted on 2007-11-25
271 Views
I am trying to create a PHP calculation form for working with ad text, user fills out on a page to submit a block of ad text they want to be run.

I am confused on the calculation part. Some of the prices are set up as:

15 words = \$1110 & every additional word is \$15

That part above is where I am confused to have it set up, right now I have this for my calc:

\$wordPriceGroup = " ";  // price for say 15 words is \$1100 every additional word equals to below (\$wordPrice)
\$wordPrice      = "15"; // price for every additional word
\$numwords = count(\$wordcount);
\$total = \$numwords * \$wordprice;

I am don't know how to group it and the client said that each word is based on TWO characters NOT spaces, I am waiting to hear back from him to double check but I can always change it.

Any suggestions on how to do that? I am stuck.

Thanks!

Ryan
``````<?php

\$weeks    = \$_REQUEST["weeks"];

\$fn       = \$_REQUEST["fn"];

\$ln       = \$_REQUEST["ln"];

\$company  = \$_REQUEST["company"];

\$email    = \$_REQUEST["email"];

\$phone    = \$_REQUEST["phone"];

\$date     = \$_REQUEST["date"];

\$wordPriceGroup = " ";  // price for say 15 words is \$1100 every additonal word equals to below (\$wordPrice)

\$wordPrice      = "15"; // price for every additonal word

\$numwords = count(\$wordcount);

\$total = \$numwords * \$wordprice;

print "Word Count:" . \$numwords ."<br />";

print "Text:" . \$ad ."<br />";

print "Weeks to Run:" . \$weeks ."<br />";

print "Price:" . \$total;

?>

<form name="calculate" action="index.php?p=calc" method="post" />

<select name="weeks" id="weeks">

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

<option value="4">4</option>

</select><br />

<p>\$565 - 15 words</p>

<label for="fn">First Name:</label>

<input type="text" name="fn" id="fn" /><br />

<label for="ln">Last Name:</label>

<input type="text" name="ln" id="ln" /><br />

<label for="company">Company Name:</label>

<input type="text" name="company" id="company" /><br />

<label for="email">E-mail:</label>

<input type="text" name="email" id="email" /><br />

<label for="phone">Phone:</label>

<input type="text" name="phone" id="phone" /><br />

<label for="date">Starting Date:</label>

<input type="text" name="date" id="date" /><br />

<input type="submit" name="submit" value="Calculate!" />

</form>
``````
0
• 18
• 9
• 2
• +1

LVL 17

Expert Comment

have you looked at str_word_count() function yet?
0

LVL 1

Author Comment

0

LVL 1

Author Comment

I took a look at that and I am using that now - http://us.php.net/str_word_count

Below is my new code and it only will output that I have one word when i have numerous
``````<?php

\$weeks    = \$_REQUEST["weeks"];

\$fn       = \$_REQUEST["fn"];

\$ln       = \$_REQUEST["ln"];

\$company  = \$_REQUEST["company"];

\$email    = \$_REQUEST["email"];

\$phone    = \$_REQUEST["phone"];

\$date     = \$_REQUEST["date"];

\$wordPriceGroup = " ";  // price for say 15 words is \$1100 every additonal word equals to below (\$wordPrice)

\$wordPrice      = "15"; // price for every additonal word

\$numwords = str_word_count(\$wordcount);

\$total = \$numwords * \$wordprice;

print "Word Count:" . \$numwords ."<br />";

print "Text:" . \$ad ."<br />";

print "Weeks to Run:" . \$weeks ."<br />";

print "Price:" . \$total;

?>

<form name="calculate" action="index.php?p=calc" method="post" />

<select name="weeks" id="weeks">

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

<option value="4">4</option>

</select><br />

<p>\$565 - 15 words</p>

<label for="fn">First Name:</label>

<input type="text" name="fn" id="fn" size="30" /><br />

<label for="ln">Last Name:</label>

<input type="text" name="ln" id="ln" size="30" /><br />

<label for="company">Company Name:</label>

<input type="text" name="company" id="company" size="30" /><br />

<label for="email">E-mail:</label>

<input type="text" name="email" id="email" size="30" /><br />

<label for="phone">Phone:</label>

<input type="text" name="phone" id="phone" size="30" /><br />

<label for="date">Starting Date:</label>

<input type="text" name="date" id="date" size="30" /><br />

<input type="submit" name="submit" value="Calculate!" />

</form>
``````
0

LVL 9

Accepted Solution

blue_hunter earned 350 total points
i'm using "each word is based on TWO characters", the code below. if the words is count by space, then follow the instruction inside the code, and uncomment the wanted, and uncomment the unwanted.
``````<?php

\$weeks    = \$_REQUEST["weeks"];

\$fn       = \$_REQUEST["fn"];

\$ln       = \$_REQUEST["ln"];

\$company  = \$_REQUEST["company"];

\$email    = \$_REQUEST["email"];

\$phone    = \$_REQUEST["phone"];

\$date     = \$_REQUEST["date"];

\$mininumGroupPrice = 15;

\$wordPriceGroup = 1100;  // price for say 15 words is \$1100 every additonal word equals to below (\$wordPrice)

\$wordPrice      = "15"; // price for every additonal word

/*

*	if count with space, do as follow

*/

/*

*	If count with two character, do as follow

*	\$wordcount = ceil(strlen(\$tmp_ad) / 2);

*/

} else {

// the words is not exceeding 15 words

}

//	\$total = \$numwords * \$wordprice;

print "Word Count:" . \$numwords ."<br />";

print "Text:" . \$ad ."<br />";

print "Weeks to Run:" . \$weeks ."<br />";

print "Price:" . \$total;

?>

<form name="calculate" action="index.php?p=calc" method="post" />

<select name="weeks" id="weeks">

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

<option value="4">4</option>

</select><br />

<p>\$565 - 15 words</p>

<label for="fn">First Name:</label>

<input type="text" name="fn" id="fn" size="30" /><br />

<label for="ln">Last Name:</label>

<input type="text" name="ln" id="ln" size="30" /><br />

<label for="company">Company Name:</label>

<input type="text" name="company" id="company" size="30" /><br />

<label for="email">E-mail:</label>

<input type="text" name="email" id="email" size="30" /><br />

<label for="phone">Phone:</label>

<input type="text" name="phone" id="phone" size="30" /><br />

<label for="date">Starting Date:</label>

<input type="text" name="date" id="date" size="30" /><br />

<input type="submit" name="submit" value="Calculate!" />

</form>
``````
0

LVL 1

Author Comment

Oh nice, thank you! After looking at your code I see how you worked with that. Thanks.

If you go to http://www.newspapersofamerica.com/new/index.php?p=calc - and test it out it comes out to more than 1110 for only 15 words and it says i have 38 words.

I put "test" 15 times in the box to test. I looked at the code to see why it is doing that and am stuck but the closest I came is maybe this:

0

LVL 9

Expert Comment

you put "test" with space in between?

// the following code take away the "enter" in the textarea,
//because PHP would count it as a character if you don't take it away
0

LVL 1

Author Comment

Yeah, I did.
>>you put "test" with space in between?

So you are saying use the code above? I don't want spaces to be counted as a word on two characters or more are letters not including spaces.

Thanks! Getting the grasp of this.
0

LVL 9

Expert Comment

if you don't want space to be counted in, you may just modify the code above to take those space away before you getting the exact WORD.

example as below:

0

LVL 1

Author Comment

Let me give this a go! Thank you!
0

LVL 1

Author Comment

Say I type in "one" with no spaces, I get an output of two words. Any suggestions on that? I dont know why the code is doing that with what you provided.

Ryan
0

LVL 9

Expert Comment

\$numwords = ceil(strlen(\$tmp_ad) / 2);  <----  function ceil() effect the count of words.

"one" =  3 characters, divided by two. it would become 1.5, but ceil() would change it into 2
If you want 3/2 = 1, then, change the ceil() into floor()
as following

0

LVL 1

Author Comment

Oh...that is more clear now I see how that is working. So it cant just say anything over 2 is a word? Is that possible? Instead of rounding.

Thanks for explaining that, I looked up ceil() earlier to get a description on the php site but makes more sense in to play here.
0

LVL 9

Expert Comment

i divided the \$ad total character by two because you said so
--> each word is based on TWO characters NOT spaces
you may ignore the ceil() or floow if you don't have to round up the total
0

LVL 1

Author Comment

I think I am starting to get this, I am kind of confused still. So there is no way to say every word more than two characters is counted as a word? Without dividing, etc?

You are taking all characters and dividing correct? I feel it would be more accurate since we are dealing with peoples money and thousands when they post an ad to do anything more than two is a word?

Thanks! blue,
Ryan
0

LVL 9

Expert Comment

i'm a little confuse on "word", is the length of the word random?
suggestion, please define the "WORD", so that you may working out the feature correctly.
0

LVL 1

Author Comment

For this script and from what the cient told me..a "word" for this is two or more characters. Not including spaces.

Ryan
0

LVL 1

Author Comment

Does that help? I have been trying some stuff here with no luck. Confused about checking to see if a word is more than two characters then it counts as a word.

Thanks for your help! Almost there, haha.

Ryan
0

LVL 1

Author Comment

Below is my newest code, just for we are on the same page, after trying and adding some more stuff to it. I cant get my debug info to show up? Any ideas why not?

Thanks,
Ryan
``````<?php

if(\$_POST){

\$error_message = "<b>Please complete the following fields:</b> <br>";

\$error         = 0;

foreach(\$fields  as \$key=>\$value){

if(!isset(\$_POST[\$key]) || empty(\$_POST[\$key])){

\$error_message .= \$value."<br>";

\$error = 1;

}

}

if(empty(\$error)){

\$weeks    = \$_REQUEST["weeks"];

\$fn       = \$_REQUEST["fn"];

\$ln       = \$_REQUEST["ln"];

\$company  = \$_REQUEST["company"];

\$email    = \$_REQUEST["email"];

\$phone    = \$_REQUEST["phone"];

\$date     = \$_REQUEST["date"];

\$mininumGroupPrice = 15;

\$wordPriceGroup    = 1100; // price for say 15 words is \$1100 every additonal word equals to below (\$wordPrice)

\$wordPrice         = "15"; // price for every additonal word

/*

*       if count with space, do as follow

*/

/*

*       If count with two character, do as follow

*       \$wordcount = ceil(strlen(\$tmp_ad) / 2);

*/

}else{

// the words is not exceeding 15 words

}

//     \$total = \$numwords * \$wordprice;

print "Word Count:" . \$numwords ."<br />";

print "Text:" . \$ad ."<br />";

print "Weeks to Run:" . \$weeks ."<br />";

print "Price:" . \$total;

}

}

if(isset(\$error) && !empty(\$error)){

print \$error_message;

}

?>

<form name="calculate" action="index.php?p=calc" method="post" class="niceform" />

<select name="weeks" id="weeks">

<option value="1">1 week</option>

<option value="2">2 weeks</option>

<option value="3">3 weeks</option>

<option value="4">4 weeks</option>

</select><br />

<p>\$1110 - 15 words</p>

<label for="fn">First Name:</label><br />

<input type="text" name="fn" id="fn" size="30" /><br />

<label for="ln">Last Name:</label><br />

<input type="text" name="ln" id="ln" size="30" /><br />

<label for="company">Company Name:</label><br />

<input type="text" name="company" id="company" size="30" /><br />

<label for="email">E-mail:</label><br />

<input type="text" name="email" id="email" size="30" /><br />

<label for="phone">Phone:</label><br />

<input type="text" name="phone" id="phone" size="30" /><br />

<label for="date">Starting Date:</label><br />

<input type="text" name="date" id="date" size="30" /><br />

<input type="submit" name="submit" value="Calculate!" />

</form>
``````
0

LVL 6

Assisted Solution

baev earned 150 total points

``````<?php

if(\$_POST){

\$error_message = "<b>Please complete the following fields:</b> <br>";

\$error         = 0;

foreach(\$fields  as \$key=>\$value){

if(!isset(\$_POST[\$key]) || empty(\$_POST[\$key])){

\$error_message .= \$value."<br>";

\$error = 1;

}

}

if(empty(\$error)){

\$weeks    = \$_REQUEST["weeks"];

\$fn       = \$_REQUEST["fn"];

\$ln       = \$_REQUEST["ln"];

\$company  = \$_REQUEST["company"];

\$email    = \$_REQUEST["email"];

\$phone    = \$_REQUEST["phone"];

\$date     = \$_REQUEST["date"];

\$mininumGroupPrice = 15;

\$wordPriceGroup    = 1100; // price for say 15 words is \$1100 every additonal word equals to below (\$wordPrice)

\$wordPrice         = "15"; // price for every additonal word

\$numwords = 0;

foreach (\$words_arr as \$word){

if (strlen(\$word)>1){

\$numwords++;

}

}

}else{

// the words is not exceeding 15 words

}

//     \$total = \$numwords * \$wordprice;

print "Word Count:" . \$numwords ."<br />";

print "Text:" . \$ad ."<br />";

print "Weeks to Run:" . \$weeks ."<br />";

print "Price:" . \$total;

}

}

if(isset(\$error) && !empty(\$error)){

print \$error_message;

}

?>

<form name="calculate" action="index.php?p=calc" method="post" class="niceform" />

<select name="weeks" id="weeks">

<option value="1">1 week</option>

<option value="2">2 weeks</option>

<option value="3">3 weeks</option>

<option value="4">4 weeks</option>

</select><br />

<p>\$1110 - 15 words</p>

<label for="fn">First Name:</label><br />

<input type="text" name="fn" id="fn" size="30" /><br />

<label for="ln">Last Name:</label><br />

<input type="text" name="ln" id="ln" size="30" /><br />

<label for="company">Company Name:</label><br />

<input type="text" name="company" id="company" size="30" /><br />

<label for="email">E-mail:</label><br />

<input type="text" name="email" id="email" size="30" /><br />

<label for="phone">Phone:</label><br />

<input type="text" name="phone" id="phone" size="30" /><br />

<label for="date">Starting Date:</label><br />

<input type="text" name="date" id="date" size="30" /><br />

<input type="submit" name="submit" value="Calculate!" />

</form>
``````
0

LVL 1

Author Comment

@baev: Thank you! I am trying to go through that code...what did you add? And could you help me try to display the output? I have it in there, but it doesnt display after submission? Any ideas?

Thanks a ton!
0

LVL 1

Author Comment

UPDATE - I got it - http://www.newspapersofamerica.com/new/index.php?p=calc

It still says the wrong amount of words..any idea to fix it? Said 7 words for "This is test"
0

LVL 6

Expert Comment

> Said 7 words for "This is test"

I just tested your link. Said 3 words for "This is test".
0

LVL 1

Author Comment

You're right..I just tested it! Thanks baev, so you added in that anything over 15 words added extra \$? Correct? I am guessing from when I tested it?

Could you tell me briefly what you did to change that?

And after I get this concrete if I post another question could you help me with hooking it up to a database a nd then pulling the prices from a table because i am going to have say 10 of these I dont want 10 files, just use different ID's to pull prices from the DB.

Thanks for your help! Really appreciate it .

Ryan
0

LVL 9

Expert Comment

foreach (\$words_arr as \$word){   // looping through
if (strlen(\$word)>1){                   // compare the characters of the word, if it's more than 1
\$numwords++;                         // accumulate the total words here
}
}

Looping through each of the words, and compare the characters of the words. you will get the result
0

LVL 1

Author Comment

So that is for 2 or more characters and like you said it will accumulate? Correct? I think above did the trick already..but let me give that a shot, your version can't hurt.

Thanks!
Ryan
0

LVL 1

Author Comment

I think the code above does that trick? Does it? Double check me code wise?

Ryan
0

LVL 9

Expert Comment

oh, i got the code from @baev and explain to you
fyi, that's the part where he added to the code
0

LVL 1

Author Comment

Thanks a ton for explaining that, just for I would get a hang of it!

If I posted a new question would you help me on this same page. I want to set up a DB and pull the prices and word grouping because there are about 15-20 of them, instead of having that many pages, just use say

calc.php?id=4 to call of the pricing, etc. Doesnt seem that bad? Correct?

Let me set up the table for it.
0

LVL 9

Expert Comment

yes, this is how the server side programming help reducing the efforts

1 page for  15- 20 different info, base on request
0

LVL 1

Author Comment

Thanks a ton!
0

## Featured Post

### Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But whatâ€¦
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creatâ€¦
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.