[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

Working with PHP calculations

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
catonthecouchproductions
Asked:
catonthecouchproductions
  • 18
  • 9
  • 2
  • +1
2 Solutions
 
psimationCommented:
have you looked at str_word_count() function yet?
0
 
catonthecouchproductionsAuthor Commented:
No...would that be helpful?
0
 
catonthecouchproductionsAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
blue_hunterCommented:
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
 
catonthecouchproductionsAuthor Commented:
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
 
blue_hunterCommented:
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
 
catonthecouchproductionsAuthor Commented:
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
 
blue_hunterCommented:
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
 
catonthecouchproductionsAuthor Commented:
Let me give this a go! Thank you!
0
 
catonthecouchproductionsAuthor Commented:
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
 
blue_hunterCommented:
$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
 
catonthecouchproductionsAuthor Commented:
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
 
blue_hunterCommented:
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
 
catonthecouchproductionsAuthor Commented:
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
 
blue_hunterCommented:
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
 
catonthecouchproductionsAuthor Commented:
For this script and from what the cient told me..a "word" for this is two or more characters. Not including spaces.

Ryan
0
 
catonthecouchproductionsAuthor Commented:
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
 
catonthecouchproductionsAuthor Commented:
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
 
DmitriiconsultantCommented:

<?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
 
catonthecouchproductionsAuthor Commented:
@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
 
catonthecouchproductionsAuthor Commented:
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
 
DmitriiconsultantCommented:
> Said 7 words for "This is test"

I just tested your link. Said 3 words for "This is test".
0
 
catonthecouchproductionsAuthor Commented:
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
 
blue_hunterCommented:
$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
 
catonthecouchproductionsAuthor Commented:
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
 
catonthecouchproductionsAuthor Commented:
I think the code above does that trick? Does it? Double check me code wise?

Ryan
0
 
blue_hunterCommented:
oh, i got the code from @baev and explain to you
fyi, that's the part where he added to the code
0
 
catonthecouchproductionsAuthor Commented:
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
 
blue_hunterCommented:
yes, this is how the server side programming help reducing the efforts

1 page for  15- 20 different info, base on request
0
 
catonthecouchproductionsAuthor Commented:
Thanks a ton!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 18
  • 9
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now