Here is some code, which I modestly suggest is superior for the following
reasons:
1.  Includes simply scaffolding (ie a sample form).
2.  Code is separated into functions, rather than one monolithic block.
3.  Somewhat improved word-counting.

==============================
<html>
<head>
 <style>
  body {
   font-family: Verdana,Arial,sans-serif;
  }
 </style>
</head>
<body>
 <h2>Price-An-Ad</h2>
 <form>
  New ad text:<br />
  <textarea name='ad' style='width:400px; height:300px;'></textarea><br />

  <input type='checkbox' name='deduct' value='true'>Make deduction<br />

  Run length <select name='weeks'>
   <option value='1' selected>One week
   <option value='2'>Two weeks
   <option value='4'>One month
  </select><br />

  <input type='submit' value='Calculate'>
 </form>

<?php
 if (isset($ad)) {
  $ad = stripslashes($ad);

  $words = count_words($ad);
  $price = calc_price($words, $deduct, $weeks);

  echo
   "Priced text:"
   ."<div style='width:400px;'><pre>$ad</pre></div>"
   ."$words words, ".(isset($deduct) ? "with" : "no")." deduction, for
$weeks weeks comes to ".sprintf("\$%01.2f",$price);

 }


function count_words($text) {
 $count = 0;
 $tok = strtok($text, " ");

 while (is_string($tok)) {
  if (strlen($tok) > 0)
   $count++;

  $tok = strtok(" ");
 }

 return $count;
}


function calc_price($words, $deduct, $weeks) {
 // base price
 $price = 5.00;

 // additional cost for more than ten words
 if ($words > 10)
  $price += ($word - 10) * 0.20;

 // favored-customer deduction
 if ($deduct)
  $price -= 1.50;

 // by number of weeks to appear
 $price *= $weeks;

 return $price;
}

?>
</body></html>



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to