Jump to content

Make function equal to string in conditional


dothedew101

Recommended Posts

Hello all. My first post. Like so many, I'm new to PHP, yet have done quite a lot with it - thanks to Larry Ullman's book (my worn Bible) and helpful forums. I can usually figure something out, but I CANNOT figure this one out: This is a classified word ad script to count the words in a text area ($wrdad) and multiply it by the rate for how many times the ad will run in the newspaper.  I know my code is messy and ameture, don't be too critical, but it works - everythign except one part. We have a 15 word minimum. I have this area commented out as //Problem Area/// in the code below.

 

In a nutshell, I want function wordcount($wordad) to be equal to 15 if there are fewer than 15 words in the $wordad textarea. It just seems to ignore the wordcount($wordad) == 15; in my conditional.

 

Oh PHP gods, please help. TIA

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Untitled Document</title>

</head>

 

<body>

 

<?php

if (isset($_POST['submit'])) { //handle Form 1

 

function wordcount($wordad) {

return count(array_filter(explode(" ", $wordad)));

}

 

// Form 1 outputted:

echo '<form action="verify2.php" method="post">';

 

////////////////////////    PROBLEM AREA ///////////////////////////////

 

if  (wordcount($wordad) < 15) {

wordcount($wordad) == 15;

echo "Your ad has " . wordcount($wordad) . " words";

} else {

}

 

////////////////////////    PROBLEM AREA ///////////////////////////////

 

$amount = wordcount($wordad) * $runs;

echo '<h2>Your word ad will cost ' . $amount . '</h2>';

 

echo '<input name="description" type="hidden" id="description" value="News-Telegram classified word ad" />';

echo 'Your word ad:<br />';

echo $_POST['wordad'] . '<p>';

echo "Your word ad has " . wordcount($wordad) . " words, will run under the ";

echo $_POST['Classification'] . " classification, for ";

echo $_POST['runs'] . "<p>";

echo $_POST['More_information'] . "<p>";

echo '<input type="submit" name="submit" id="button" value="Continue" />';

echo '</form>';

} else  { //Display form 1

?>

 

<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post" name="form 1" id="form 1">

<h2>Type your word ad... </h2>

  <p>

    <label>*Type your word ad as you want it to appear in the classifieds. <br />

    <font size="1">Note: the minimum charge is for 15 words. </font><br />

    <textarea name="wordad" id="wordad" cols="45" rows="5"></textarea>

    <br />

    <a href="words.html" target="_blank"><font size="1">Word ad tips.....</font></a><br />

    </label>

  </p>

  <h2>How long would you like it to run? </h2>

  <table width="400">

    <tr>

      <td><b>*Classification:</b></td>

      <td><select name="Classification" user_required_classification="user_required_classification">

          <option selected="selected">Select a classification...</option>

          <option>002 Notices</option>

          <option>003 Card of Thanks</option>

          <option>006 Automobiles</option>

          <option>007 Motorcycles</option>

          <option>008 Trucks</option>

          <option>010 Boats</option>

          <option>012 Business Services</option>

          <option>013 Beauty Aids</option>

          <option>019 Busines Opportunities</option>

          <option>021 Employment</option>

          <option>023 Jobs Wanted</option>

          <option>024 Auctions</option>

          <option>025 Musical Instruments</option>

          <option>026 Clothing</option>

          <option>027 Appliances</option>

          <option>028 Furniture</option>

          <option>029 Hay and Grain</option>

          <option>030 Misc. for Sale</option>

          <option>031 Garage Sale</option>

          <option>032 Farm Equipment</option>

          <option>033 Mobile Homes</option>

          <option>035 Gift Ideas</option>

          <option>036 Campers</option>

          <option>037 Travel Trailer</option>

          <option>038 Motor Homes</option>

          <option>040 Livestock</option>

          <option>041 Pets</option>

          <option>042 Poultry</option>

          <option>044 Rentals</option>

          <option>045 Houses for Rent</option>

          <option>046 Apartments</option>

          <option>047 Duplexes</option>

          <option>048 Business Property</option>

          <option>052 Want to Rent</option>

          <option>053 Mobile Homes</option>

          <option>055 Real Estate</option>

          <option>056 Farms</option>

          <option>057 Homes for Sale</option>

          <option>058 Lots for Sale</option>

          <option>060 Want to Buy</option>

          <option>061 Employment Education</option>

          <option>063 Lost</option>

          <option>066 Public Notices</option>

        </select>

      </td>

    </tr>

    <tr>

      <td><b>*Number of runs: </b></td>

      <td><select name="runs" id="select2">

          <option value="0.39">1 time - .39 cents per word</option>

          <option value="0.69">2 times - .69 cents per word</option>

          <option value="0.92">3 times - .92 cents per word</option>

          <option value="1.10">4 times - $1.10 per word</option>

          <option value="1.25">5 times - $1.25 per word</option>

      </select></td>

    </tr>

    <tr>

      <td><b>More information:<br />

      </b><font size="1">Such as a particular day of the week or schedule.</font> </td>

      <td><textarea name="More_information" wrap="virtual" id="More_information" additional_ad_info="additional_ad_info"></textarea>

      </td>

    </tr>

  </table>

  <p>

    <input type="submit" name="submit" id="button" value="Continue" />

  </p>

  </form>

<?php

} //End of form

?>d d

</body>

</html>

 

In a nutshell, I want function wordcount($wordad) to be equal to 15 if there are fewer than 15 words in the $wordad textarea. It just seems to ignore the wordcount($wordad) == 15; in my conditional.
di you mean Return

if  (wordcount($wordad) < 15) {

  wordcount($wordad) == 15;

  echo "Your ad has " . wordcount($wordad) . " words";

} else {

}

did you mean

if(wordcount($wordad) <= 15)//Using Less OR Equal
  {
     echo "Your ad has " . wordcount($wordad) . " words";
  }
else
  {
    //Do Something ??
  }

In a nutshell, I want function wordcount($wordad) to be equal to 15 if there are fewer than 15 words in the $wordad textarea. It just seems to ignore the wordcount($wordad) == 15; in my conditional.
di you mean Return

if  (wordcount($wordad) < 15) {

   wordcount($wordad) == 15;

   echo "Your ad has " . wordcount($wordad) . " words";

} else {

}

did you mean

if(wordcount($wordad) <= 15)//Using Less OR Equal
  {
     echo "Your ad has " . wordcount($wordad) . " words";
  }
else
  {
    //Do Something ??
  }

 

Yes, sry I wasn't sure if I needed <= or not since 15 would equal 15.... But, yes, that's what I'm trying to say, but it doesn't obay that statement. It just camculates using the number of words: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ,13, or 14. When I went there to be no word count lower than 15.

OK, the spoonfed version

 

<?php
$ad = "For sale, PHP code on word counting, Fifty dollars";

echo wordcount ($ad);    // --> 15

function wordcount($str)
{
    $wordcount = str_word_count($str);
    return max ($wordcount, 15);
}
?>

 

Thanks for the spoonfed version, but I got it to work with your max() function suggestion. However, I"m not sure why I had to use an elseif instead of an else in the conditional....:

 

function wordcount($wordad) {

return count(array_filter(explode(" ", $wordad)));

}

 

// Form 1 outputted:

echo '<form action="verify2.php" method="post">';

 

if  (wordcount($wordad) <= 15) {

echo "Your ad has " . wordcount($wordad) . " words, but you will be charged for a minimum of 15 words.";

} elseif (wordcount($wordad) > 15) {

echo "Your ad has " . wordcount($wordad) . " words.";

}

$amount = max(wordcount($wordad),15) * $runs;

echo '<h2>Your word ad will cost $' . $amount . '</h2>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.