Solved

Working with PHP calculations

Posted on 2007-11-25
30
271 Views
Last Modified: 2008-02-01
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
            $wordcount = split(' ',$ad);
            $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"];

        $ad       = $_REQUEST["ad"];

        $fn       = $_REQUEST["fn"];

        $ln       = $_REQUEST["ln"];

        $company  = $_REQUEST["company"];

        $email    = $_REQUEST["email"];

        $phone    = $_REQUEST["phone"];

        $date     = $_REQUEST["date"];

        $comments = $_REQUEST["comments"];

		

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

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

		

		$wordcount = split(' ',$ad);

		$numwords = count($wordcount);

		

		$total = $numwords * $wordprice;

		

		

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

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

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

		print "Price:" . $total;

        

?>      

<h3>Step 1 - Ad Text</h3>

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

        <label for="weeks">Weeks to run ad:</label>

        <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="ad">Ad Text:</label>

        <textarea name="ad" id="ad" rows="5" cols="30"></textarea><br />

        

        <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 />

        <label for="comments">Comments:</label>

        <textarea name="comments" id="comments" rows="5" cols="30"></textarea><br />

        

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

</form>

Open in new window

0
Comment
  • 18
  • 9
  • 2
  • +1
30 Comments
 
LVL 17

Expert Comment

by:psimation
Comment Utility
have you looked at str_word_count() function yet?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
No...would that be helpful?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
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"];

        $ad       = $_REQUEST["ad"];

        $fn       = $_REQUEST["fn"];

        $ln       = $_REQUEST["ln"];

        $company  = $_REQUEST["company"];

        $email    = $_REQUEST["email"];

        $phone    = $_REQUEST["phone"];

        $date     = $_REQUEST["date"];

        $comments = $_REQUEST["comments"];

		

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

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

		

		$wordcount = split(' ',$ad);

		$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;

        

?>      

<h3>Step 1 - Ad Text</h3>

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

        <label for="weeks">Weeks to run ad:</label>

        <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="ad">Ad Text:</label>

        <textarea name="ad" id="ad" rows="5" cols="30"></textarea><br />

        

        <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 />

        <label for="comments">Comments:</label>

        <textarea name="comments" id="comments" rows="5" cols="30"></textarea><br />

        

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

</form>

Open in new window

0
 
LVL 9

Accepted Solution

by:
blue_hunter earned 350 total points
Comment Utility
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"];

        $ad       = $_REQUEST["ad"];

        $fn       = $_REQUEST["fn"];

        $ln       = $_REQUEST["ln"];

        $company  = $_REQUEST["company"];

        $email    = $_REQUEST["email"];

        $phone    = $_REQUEST["phone"];

        $date     = $_REQUEST["date"];

        $comments = $_REQUEST["comments"];

        

		$mininumGroupPrice = 15;

		$additionalPrice = 0;

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

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

                

                

				$tmp_ad = str_replace(array("\n","\r"), array("",""), $ad);
 

				/*

				*	if count with space, do as follow

				*				$wordcount = explode(" ",$ad);

				*/

//				$wordcount = explode(" ",$ad);
 

				/*

				*	If count with two character, do as follow

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

				*/

				$numwords = ceil(strlen($tmp_ad) / 2);
 
 

         $additionalWords = $numwords - $mininumGroupPrice;
 

		 if($additionalWords > 0){

			 $additionalPrice = $additionalWords * $wordPrice;

		 } else {

			 // the words is not exceeding 15 words

			 $additionalPrice = 0;

		 }
 

		 //	$total = $numwords * $wordprice;

		 $total = $wordPriceGroup + $additionalPrice;

                

                

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

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

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

                print "Price:" . $total;

        

?>      

<h3>Step 1 - Ad Text</h3>

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

        <label for="weeks">Weeks to run ad:</label>

        <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="ad">Ad Text:</label>

        <textarea name="ad" id="ad" rows="5" cols="30"></textarea><br />

        

        <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 />

        <label for="comments">Comments:</label>

        <textarea name="comments" id="comments" rows="5" cols="30"></textarea><br />

        

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

</form>

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
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:

            $tmp_ad = str_replace(array("\n","\r"), array("",""), $ad);
0
 
LVL 9

Expert Comment

by:blue_hunter
Comment Utility
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
$tmp_ad = str_replace(array("\n","\r"), array("",""), $ad);  
0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
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

by:blue_hunter
Comment Utility
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:

$tmp_ad = str_replace(array("\n","\r"," "), array("","", ""), $ad);

0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
Let me give this a go! Thank you!
0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
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

by:blue_hunter
Comment Utility
$numwords = ceil(strlen($tmp_ad) / 2);  <----  function ceil() effect the count of words.

as example you had gave,
"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

$numwords = floor(strlen($tmp_ad) / 2);
0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
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

by:blue_hunter
Comment Utility
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

by:catonthecouchproductions
Comment Utility
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

by:blue_hunter
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
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

by:catonthecouchproductions
Comment Utility
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

by:catonthecouchproductions
Comment Utility
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){

  

  $fields = array("ad"=>"Ad Text", "fn"=>"First Name", "ln"=>"Last Name", "company"=>"Company Name", "email"=>"E-mail","phone"=>"Phone","date"=>"Starting Date","comments"=>"Comments");

 

$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"];

        $ad       = $_REQUEST["ad"];

        $fn       = $_REQUEST["fn"];

        $ln       = $_REQUEST["ln"];

        $company  = $_REQUEST["company"];

        $email    = $_REQUEST["email"];

        $phone    = $_REQUEST["phone"];

        $date     = $_REQUEST["date"];

        $comments = $_REQUEST["comments"];

        

        $mininumGroupPrice = 15;

        $additionalPrice   = 0;

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

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

 

                $tmp_ad = str_replace(array("\n","\r"), array("",""), $ad);

                

                /*

                *       if count with space, do as follow

                *                               $wordcount = explode(" ",$ad);

                */

                //                              $wordcount = explode(" ",$ad);

                

                /*

                *       If count with two character, do as follow

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

                */

                $numwords                = ceil(strlen($tmp_ad) / 2);

                $additionalWords = $numwords - $mininumGroupPrice;

                

                if($additionalWords > 0){

                        $additionalPrice = $additionalWords * $wordPrice;

                }else{

                        // the words is not exceeding 15 words

                        $additionalPrice = 0;

                }

                //     $total = $numwords * $wordprice;

                $total = $wordPriceGroup + $additionalPrice;

               

                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;

}

?>

 

<h3>Ad Text</h3>

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

        <label for="weeks">Weeks to run ad:</label>

        <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="ad">Ad Text:</label><br />

        <textarea name="ad" id="ad" rows="5" cols="30"></textarea><br />

        

        <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 />

        <label for="comments">Comments:</label><br />

        <textarea name="comments" id="comments" rows="5" cols="30"></textarea><br />

        

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

</form>

Open in new window

0
 
LVL 6

Assisted Solution

by:baev
baev earned 150 total points
Comment Utility

<?php

 if($_POST){

  

  $fields = array("ad"=>"Ad Text", "fn"=>"First Name", "ln"=>"Last Name", "company"=>"Company Name", "email"=>"E-mail","phone"=>"Phone","date"=>"Starting Date","comments"=>"Comments");

 

$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"];

        $ad       = $_REQUEST["ad"];

        $fn       = $_REQUEST["fn"];

        $ln       = $_REQUEST["ln"];

        $company  = $_REQUEST["company"];

        $email    = $_REQUEST["email"];

        $phone    = $_REQUEST["phone"];

        $date     = $_REQUEST["date"];

        $comments = $_REQUEST["comments"];

        

        $mininumGroupPrice = 15;

        $additionalPrice   = 0;

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

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

 

                $tmp_ad = str_replace(array("\n","\r","\t"), array(" "," "," "), $ad);

                $tmp_ad = str_replace("  ", " ", $tmp_ad);

                $numwords = 0;

                $words_arr = explode(" ", $tmp_ad);

                foreach ($words_arr as $word){

                  if (strlen($word)>1){

                    $numwords++;

                  }

                }
 

                $additionalWords = $numwords - $mininumGroupPrice;

                

                if($additionalWords > 0){

                        $additionalPrice = $additionalWords * $wordPrice;

                }else{

                        // the words is not exceeding 15 words

                        $additionalPrice = 0;

                }

                //     $total = $numwords * $wordprice;

                $total = $wordPriceGroup + $additionalPrice;

               

                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;

}

?>

 

<h3>Ad Text</h3>

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

        <label for="weeks">Weeks to run ad:</label>

        <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="ad">Ad Text:</label><br />

        <textarea name="ad" id="ad" rows="5" cols="30"></textarea><br />

        

        <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 />

        <label for="comments">Comments:</label><br />

        <textarea name="comments" id="comments" rows="5" cols="30"></textarea><br />

        

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

</form>

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
Comment Utility
@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

by:catonthecouchproductions
Comment Utility
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

by:baev
Comment Utility
> Said 7 words for "This is test"

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

Author Comment

by:catonthecouchproductions
Comment Utility
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

by:blue_hunter
Comment Utility
$words_arr = explode(" ", $tmp_ad);
                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

by:catonthecouchproductions
Comment Utility
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

by:catonthecouchproductions
Comment Utility
I think the code above does that trick? Does it? Double check me code wise?

Ryan
0
 
LVL 9

Expert Comment

by:blue_hunter
Comment Utility
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

by:catonthecouchproductions
Comment Utility
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

by:blue_hunter
Comment Utility
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

by:catonthecouchproductions
Comment Utility
Thanks a ton!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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.

771 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

9 Experts available now in Live!

Get 1:1 Help Now