Jump to content

Need help creating a discount table


kakes

Recommended Posts

I need help again. I need to create a discount table. The table should increase the discount rate by 5% and the amount by 50 cents each time. I have to have 20 rows of data. The discount rate starts at 5% and goes up to 95%. Row headings and column headings have to be bold. All dollar values have to be rounded using round().

 

I think I have the table headings correct except I have the amount increasing by 50 not 50 cents. I don't understand what I need to do in order to have the table generate the discount rate. Here's my code so far.

 

<?php

$original_price = 5;

echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Amount</th>";
echo "<th>Less 5%</th>";
echo "<th>Less 10%</th>";
echo "<th>Less 15%</th>";
echo "<th>Less 20%</th>";
echo "<th>Less 25%</th>";
echo "<th>Less 30%</th>";
echo "<th>Less 35%</th>";
echo "<th>Less 40%</th>";
echo "<th>Less 45%</th>";
echo "<th>Less 50%</th>";
echo "<th>Less 55%</th>";
echo "<th>Less 60%</th>";
echo "<th>Less 65%</th>";
echo "<th>Less 70%</th>";
echo "<th>Less 75%</th>";
echo "<th>Less 80%</th>";
echo "<th>Less 85%</th>";
echo "<th>Less 90%</th>";
echo "<th>Less 95%</th></tr>";
for ( $counter = 100; $counter <= 1000; $counter += 50) {
   echo "<tr><td>";
   echo $counter;
   echo "</td><td>";
   echo $original_price * $counter;
   echo "</td></tr>";
}
echo "</table>";
?>

 

I know the multiplication isn't the correct math function but I can't figure out how to get the 5% to generate. I might be way off. Thanks for any suggestions!!!

Link to comment
Share on other sites

You can calculate 5% of something in a number of ways:

 

$five_percent = $original * 0.05;
$five_percent = $original * 5 / 100; # 5% is 5 out of 100
$five_percent = $original / 20; # 5/100 = 1/20, removing the common factor of 5

 

The second equation is probably the best one for you as you can just plug in 10, 15, 20, etc in place of 5.

Link to comment
Share on other sites

Sorry. Wasn't sure where to post the question.  ;)

 

No problem ;)

 

Thanks, I've got a lot of work to do on this so I appreciate you guys steering me in the right direction.

 

If this is resolved please mark as solved, if you need more help just ask or if you have a different question, make a new thread.

Link to comment
Share on other sites

I've taken a different approach to this but I'm not sure it will work. I can't get it to load so I can't view it. Must be something wrong. Help.

 

<?php

   echo "<table style=\"border: 1px solid black;\"> \n";
      for ($discount = 5; $discount<=95; $discount+5)
         {
   echo "<tr>\n";
      for ($amount = 1.00; $amount<=10.50; $amount+.50)
         {
   echo "<td style=\"border: 1px solid #000; width: 25px; padding: 4px;
         text-align: center;\">";
   echo ($discount * $amount);      
   echo "</td>\n";
         }
   echo "</tr>\n";
         }
   echo "</table>";      

?>

 

Link to comment
Share on other sites

Thank you!! That did generate my table and I can view it now. It's not calculating like I need it to and I also need to round to the nearest cent.

 

What I need it to do is this: The table increases the discount rate by 5% up to 95%. I think it is mostly doing that on the left side (need to add a percent sign). I was trying to get the amount to start at a dollar and increase by 50 cents. The table is supposed to calculate the dollar amount with the discount and display it across the rows like a multiplication table. I need 20 rows/columns.

 

Any help is certainly appreciated!

Link to comment
Share on other sites

Thank you!! That did generate my table and I can view it now. It's not calculating like I need it to and I also need to round to the nearest cent.

 

What I need it to do is this: The table increases the discount rate by 5% up to 95%. I think it is mostly doing that on the left side (need to add a percent sign). I was trying to get the amount to start at a dollar and increase by 50 cents. The table is supposed to calculate the dollar amount with the discount and display it across the rows like a multiplication table. I need 20 rows/columns.

 

Any help is certainly appreciated!

 

Like this?

 

  echo "</pre>
<table style='\"border:' solid black> \n";
  for ($discount = 5; $discount  {
     echo "\n";
     for ($amount = 1.00; $amount     {
        echo "";
        echo "$" . (money_format('%i', ($discount/100) * $amount));
        echo "\n";
     }
     echo "\n";
  }
  echo "</table>";           <br><br>?&g

 

(Sorry, I reformatted the code to what I'm used to)

Link to comment
Share on other sites

That's great! Just one thing: how do I get the dollar amount to start at $1.00 and increase 50 cents each column and reflect the discount in the table...sort of like a multiplication table?

 

You've been such a great help and I certainly appreciate it!

Link to comment
Share on other sites

That's great! Just one thing: how do I get the dollar amount to start at $1.00 and increase 50 cents each column and reflect the discount in the table...sort of like a multiplication table?

 

You've been such a great help and I certainly appreciate it!

 

You need to adjust the discount and/or amount values.

Link to comment
Share on other sites

I've taken yet another approach to this table. I think I'm almost there but I can't get the zeros to show up and I can't figure out where to put the round() to make the dollar amounts round to the nearest cent. I would also like the $ and % signs to print but am not sure where to echo them. Thanks for any help you can offer!

 

Here's my new approach..

 

<?php
// variables
$title = "Discount Table";
$maxSize= 20;

echo "<html>
   <head>
      <title>
         $title 
      </title>
      <!-- Embedded style sheet -->
         <style type=\"text/css\">
              th,td {border: 1px solid black;
                     width: 25px; padding: 4px; 
                     text-align: center;}
         </style>
   </head>
   <body>
       <h1>
          $title
       </h1>
       <table style=\"border: 1px solid black;\">
       <tr>
          <th></th>\n";

         for ($columnHeading=1.00; $columnHeading <= $maxSize; $columnHeading++)
   {
      echo "          <th>$columnHeading</th>\n"; 
   }
      echo "       </tr>\n";
         for ($row = .05; $row <= 1.00; $row+=.05)
   {
     echo "       <tr>\n";
     echo "          <th>$row</th>\n";
         for ($column = 1; $column <= $maxSize; $column++)
        {      
          $total = $column * $row;
          echo "          <td>$total</td>\n";
        }
     echo "       </tr>\n";
   }
echo "       </table>
   </body>
</html>\n";

?>


Link to comment
Share on other sites

Pay careful attention to where I added the $ and % signs, and how I used the number_format function. You should try to keep your formatting and indentation more consistent, too.

 

<?php
// variables
$title = "Discount Table";
$maxSize= 20;

echo "<html>
   <head>
      <title>
         $title
      </title>
      <!-- Embedded style sheet -->
         <style type=\"text/css\">
              th,td {border: 1px solid black;
                     width: 25px; padding: 4px;
                     text-align: center;}
         </style>
   </head>
   <body>
       <h1>
          $title
       </h1>
       <table style=\"border: 1px solid black;\">
       <tr>
          <th></th>\n";

         for ($columnHeading=1.00; $columnHeading <= $maxSize; $columnHeading++)
   {
      echo "          <th>$" . number_format($columnHeading, 2, '.', ',') . "</th>\n";
   }
      echo "       </tr>\n";
         for ($row = .05; $row <= 1.00; $row+=.05)
   {
     echo "       <tr>\n";
     echo "          <th>%" . $row * 100 . "</th>\n";
         for ($column = 1; $column <= $maxSize; $column++)
        {
          $total = $column * $row;
          echo "          <td>$" . number_format($total, 2, '.', ',') . "</td>\n";
        }
     echo "       </tr>\n";
   }
echo "       </table>
   </body>
</html>\n";

?>

Link to comment
Share on other sites

Thanks so much for your help! To answer your question, I keep changing code because I'm not sure what I'm doing. I try one approach, then another, just to see which one is going to work the best. The only thing now I need to work on is getting the dollar amounts to increase by 50 cents instead of one dollar and also use "round" to round the dollar amounts to the nearest cent. I'm sure it's a matter of tweaking the code so hopefully I can figure it out. Sure hope so!!

 

Thanks again!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.