CodeIgniter Forums

Full Version: Confusing PHP Arithmetic Formula
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys,

I am writing today for some help is deciphering some PHP calculations that were done in Codeigniter.

First of all, I should preface this by saying, I did not write most of this code originally. It was written by a friend of mine who has become very difficult to get in touch with lately due to work schedule.

So here is what I do know:
  • Most of the values for the PHP calculations are held within the database, but not all.
  • Some of the values are dependent on calculations in other places in the HTML table BUT don't appear to be calculated that way.

I have the folllowing code:

Priceinfo.php (Controller)
Code:
<?php
class PriceInfo extends MY_Controller {

function __construct() {
 parent::__construct();
  $this->session->set_flashdata('uri', $this->uri->uri_string());
  $this->load->model('calculator_model');
}

function index() {
 $name = $this->uri->rsegment(3);
 $priceinfo = $this->calculator_model->read_record_by_name($name);
 $data = array(
      'title' => "Shirley's Recipes: ".$priceinfo->title,
    'columns' => array('toc', 'calculator'),
  'priceinfo' => $priceinfo
 );
 $this->load->view('includes/template', $data);
}

function admin() {
 $this->require_login();
 $name = $this->uri->rsegment(3);
 $priceinfo = $this->calculator_model->read_record_by_name($name);
 $data = array(
      'title' => "Shirley's Recipes: ".$priceinfo->title,
    'columns' => array('toc', 'calculator'),
  'priceinfo' => $priceinfo
 );
 $this->load->view('includes/template', $data);
}
}
//End - priceinfo.php
?>

Calculator_model (Model)
Code:
<?php
class Calculator_model extends CI_Model {

function create_record($data){
 $this->db->insert('calculator', $data);
}
 
function read_record_by_id($id){
 $this->db->where('id', $id);
 $q = $this->db->get('calculator');
 return $q->row();
}
 
function read_record_by_name($name){
 $this->db->where('name', $name);
 $q = $this->db->get('calculator');
 return $q->row();
}

function update_record($id, $data){
 $this->db->where('id', $id);
 $this->db->update('calculator', $data);
}
 
function delete_record($id){
 $this->db->where('id', $id);
 $this->db->delete('calculator');
}
}
// End - calculator_model.php
?>

calculator.php (View)
Code:
<div id="calculator" class="content">
<h1>Shirley's Recipe Book Price Info</h1>
<p>
<table id="printinfo">
<tr>
 <th colspan="6" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:2px solid; border-left:1px solid;"><?=$priceinfo->header ?></th>
</tr>    
<tr>
 <th colspan="6" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid; border-left:1px solid;">Cost to Print Single-Sided</th>
</tr>      
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Type of Printing:</td>        
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;"># of Pages:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Cost per Page:</td>
 <td style="background:#6890CB; font-weight:bold; border-bottom:1px solid;">Cost to Print:</td>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:2px solid; border-bottom:1px solid;">Color</td>
 <td colspan="2" style="border-right:1px solid; border-bottom:1px solid;"><?php echo $priceinfo->numSinglePages ?></td>
 <td colspan="2" style="border-right:1px solid; border-bottom:1px solid;"><?php printf('$%01.2f', $priceinfo->colorSingleCost) ?></td>
 <td style="border-bottom:1px solid;"><?php printf('$%01.2f', $priceinfo->numSinglePages * $priceinfo->colorSingleCost) ?></td>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:2px solid; border-bottom:2px solid;">Black & White</td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php echo $priceinfo->numSinglePages ?></td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php printf('$%01.2f', $priceinfo->bwSingleCost) ?></td>
 <td style="border-bottom:2px solid;"><?php printf('$%01.2f', $priceinfo->numSinglePages * $priceinfo->bwSingleCost) ?></td>
</tr>      
<tr>
 <th colspan="6" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid; border-left:1px solid;">Cost to Print Two-Sided</th>
</tr>    
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Type of Printing:</td>        
 <td colspan="2"style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;"># of Pages:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Cost per Page:</td>
 <td style="background:#6890CB; font-weight:bold; border-bottom:1px solid;">Cost to Print:</td>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:2px solid; border-bottom:1px solid;">Color</td>
 <td colspan="2"style="border-right:1px solid; border-bottom:1px solid;"><?php echo $priceinfo->numDoublePages ?></td>
 <td colspan="2" style="border-right:1px solid; border-bottom:1px solid;"><?php printf('$%0.2f', $priceinfo->colorDoubleCost) ?></td>
 <td style="border-bottom:1px solid;"><?php printf('$%0.2f', $priceinfo->numDoublePages * $priceinfo->colorDoubleCost) ?></td>
</tr>
<tr>        
 <td style="background:#6890CB; font-weight:bold; border-right:2px solid; border-bottom:2px solid;";">Black & White</td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php echo $priceinfo->numDoublePages ?></td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->bwDoubleCost) ?></td>
 <td style="border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->numDoublePages * $priceinfo->bwDoubleCost) ?></td>
</tr>
<tr>
 <th colspan="6" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid; border-left:1px solid;">Cost to Laminate Single-Sided</th>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Type of Lamination:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;"># of Pages:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Cost per Page:</td>
 <td style="background:#6890CB; font-weight:bold; border-bottom:1px solid;">Cost to Laminate:</td>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:2px solid; border-bottom:2px solid;">8.5 x 11 Inch Sheets</td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php echo $priceinfo->numSinglePages ?></td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->laminateCost) ?></td>
 <td style="border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->numSinglePages * $priceinfo->laminateCost) ?></td>
</tr>
<tr>
 <th colspan="6" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid; border-left:1px solid;">Cost to Trim Laminate Single-Sided</th>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Type of Lamination:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Cost per Cut:</td>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;"># of Sheets:</td>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:2px solid;"># of Cuts per Page:<br>(per 100 Sheet Stack)</td>
 <td style="background:#6890CB; font-weight:bold; border-bottom:1px solid;">Cost to Trim Laminate:</td>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:2px solid; border-bottom:2px solid;">8.5 x 11 Inch Sheets</td>
 <td colspan="2"style="border-right:1px solid; border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->cuttingCost) ?></td>
 <td style="border-right:1px solid; border-bottom:2px solid;"><?php echo $priceinfo->numSinglePages ?></td>  
 <td style="border-right:1px solid; border-bottom:2px solid;"><?php echo $priceinfo->numCutsPerPage ?></td>
 <td style="border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->cuttingCost * $priceinfo->numCutsPerPage) ?></td>
</tr>
<tr>
 <th colspan="6" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid; border-left:1px solid;">Cost to Laminate Two-Sided</th>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Type of Lamination:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;"># of Pages:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Cost per Page:</td>
 <td style="background:#6890CB; font-weight:bold; border-bottom:1px solid;">Cost to Laminate:</td>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:2px solid; border-bottom:2px solid;">8.5 x 11 Inch Sheets</td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php echo $priceinfo->numDoublePages ?></td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->laminateCost) ?></td>
 <td style="border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->numDoublePages * $priceinfo->laminateCost) ?></td>
</tr>
 <th colspan="6" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid; border-left:1px solid;">Cost to Trim Laminate Two-Sided</th>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Type of Lamination:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Cost per Cut:</td>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;"># of Sheets:</td>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:2px solid;"># of Cuts per Page:<br>(per 100 Sheet Stack)</td>
 <td style="background:#6890CB; font-weight:bold; border-bottom:1px solid;">Cost to Trim Laminate:</td>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:2px solid; border-bottom:2px solid;">8.5 x 11 Inch Sheets</td>
 <td style="border-right:1px solid; border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->cuttingCost) ?></td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php echo $priceinfo->numDoublePages ?></td>  
 <td style="border-right:1px solid; border-bottom:2px solid;"><?php echo $priceinfo->numCutsPerPage ?></td>
 <td style="border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->cuttingCost * $priceinfo->numCutsPerPage) ?></td>
</tr>
<tr>
 <th colspan="6" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid; border-left:1px solid;">Cost to Bind Single-Sided</th>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Type of Binding:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;"># of Pages:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Cost per 100 Pages:</td>
 <td style="background:#6890CB; font-weight:bold; border-bottom:1px solid;">Cost to Bind:</td>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:2px solid; border-bottom:2px solid;">Coil</td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php echo $priceinfo->numSinglePages ?></td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->bindingCost) ?></td>
 <td style="border-bottom:2px solid;"><?php printf('$%0.2f', (int)(($priceinfo->numSinglePages - 1) / 100 + 1) * $priceinfo->bindingCost) ?></td>
</tr>
<tr>
 <th colspan="6" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid; border-left:1px solid;">Cost to Bind Two-Sided</th>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Type of Binding:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;"># of Pages:</td>
 <td colspan="2" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid;">Cost per 100 Pages:</td>
 <td style="background:#6890CB; font-weight:bold; border-bottom:1px solid;">Cost to Bind:</td>
</tr>
<tr>
 <td style="background:#6890CB; font-weight:bold; border-right:2px solid; border-bottom:2px solid;">Coil</td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php echo $priceinfo->numDoublePages ?></td>
 <td colspan="2" style="border-right:1px solid; border-bottom:2px solid;"><?php printf('$%0.2f', $priceinfo->bindingCost) ?></td>
 <td style="border-bottom:2px solid;"><?php printf('$%0.2f', (int)(($priceinfo->numDoublePages - 1) / 100 + 1) * $priceinfo->bindingCost) ?></td>
</tr>
<tr>
 <th colspan="6" style="background:#6890CB; font-weight:bold; border-right:1px solid; border-bottom:1px solid; border-left:1px solid;">Total Cost</th>
</tr>
<tr>
 <td colspan="5" style="background:#6890CB; font-weight:bold; text-align:left; border-right:1px solid; border-bottom:1px solid;">Cost to Print, Laminate, Trim, & Bind in Color Single-Sided:</td>
 <td style="border-bottom:1px solid;"><?php printf('$%0.2f', $priceinfo->numSinglePages * ($priceinfo->colorSingleCost + $priceinfo->laminateCost)  + (int)(($priceinfo->numSinglePages - 1) / 100 + 1) * $priceinfo->bindingCost) ?></td>
</tr>
<tr>
 <td colspan="5" style="background:#6890CB; font-weight:bold; text-align:left; border-right:1px solid; border-bottom:1px solid;">Cost to Print, Laminate, Trim, & Bind in Black/White Single-Sided:</td>
 <td style="border-bottom:1px solid;"><?php printf('$%0.2f', $priceinfo->numSinglePages * ($priceinfo->bwSingleCost + $priceinfo->laminateCost)  + (int)(($priceinfo->numSinglePages - 1) / 100 + 1) * $priceinfo->bindingCost) ?></td>
</tr>
<tr>
 <td colspan="5" style="background:#6890CB; font-weight:bold; text-align:left; border-right:1px solid; border-bottom:1px solid;">Cost to Print, Laminate, Trim, & Bind in Color Two-Sided:</td>
 <td style="border-bottom:1px solid;"><?php printf('$%0.2f', $priceinfo->numDoublePages * ($priceinfo->colorDoubleCost + $priceinfo->laminateCost)  + (int)(($priceinfo->numDoublePages - 1) / 100 + 1) * $priceinfo->bindingCost) ?></td>
</tr>
<tr>
 <td colspan="5" style="background:#6890CB; font-weight:bold; text-align:left; border-bottom:2px solid">Cost to Print, Laminate, Trim, & Bind in Black/White Two-Sided:</td>
 <td style="border-bottom:2px solid"><?php printf('$%0.2f', $priceinfo->numDoublePages * ($priceinfo->bwDoubleCost + $priceinfo->laminateCost)  + (int)(($priceinfo->numDoublePages - 1) / 100 + 1) * $priceinfo->bindingCost) ?></td>
</tr>
<tr>
 <th colspan="6" style="background:#6890CB;">Updated : <?=date("D M j G:i:s T Y", strtotime($priceinfo->updated.' UTC')) ?></th>
</tr>
</table>
</div>
</div>

Calculator Database Table Field Names:
Code:
id
name
title
header
numSinglePages
numDoublePages
numCutsPerPage
colorSingleCost
bwSingleCost
colorDoubleCost
bwDoubleCost
laminateCost
bindingCost
cuttingCost
updated

Now if you look at line #126 of "calculator.php (View)" , you will see the following:

Code:
<td style="border-bottom:2px solid;"><?php printf('$%0.2f', (int)(($priceinfo->numSinglePages - 1) / 100 + 1) * $priceinfo->bindingCost) ?></td>

More specifically the PHP code block:
Code:
<?php printf('$%0.2f', (int)(($priceinfo->numSinglePages - 1) / 100 + 1) * $priceinfo->bindingCost) ?>

I don't have the foggiest idea what she was calculating here. I was expecting something like the PHP computed output value in the HTML table added together with the next total below it, and so on; to get the correct calculation in a simpler way like: number/variable + number/variable + number/variable + etc. Especially, since NONE of the totals in the HTML table are stored in the database.

The only logical way I can figure one would calculate-what as far as am aware-is an arbitrary number as far trying to do PHP calculations with those totals. Unless of course I enter each total as either a variable and calculate the defined variable which can change quite often. Or, I use just a raw number like 55.46 + 140.06 + 7.96 + 2.49, etc.. Correct?

So, If someone could decipher that for me I'd be most appreciative.

Thank you,

P.S. I am new to PHP and this project started out as me learning as she wrote the majority of the code. But, so far i've been on my own.
The values are read from the database table fields numSinglePages and bindingCost

He is taking the numSinglePages subtracting 1 from it then dividing it by 100 plus 1
and then multipling it by bindingCost.
(12-31-2018, 09:24 AM)InsiteFX Wrote: [ -> ]The values are read from the database table fields numSinglePages and bindingCost

He is taking the numSinglePages subtracting 1 from it then dividing it by 100 plus 1
and then multipling it by bindingCost.

Well if that is true, then the number you get makes no sense. here is the break down as I understand it:

numSinglePages = 94 (the number of single pages in the database)
Subtract            -  1
---------------------
                        93
divide by           /100
---------------------
                      0.93
add                       1
----------------------
                       0.94
multiply by        x 2.79 (Binding Cost per 100 pages)
----------------------
                        2.62 (roughly)

Which means if my math above is accurate, $2.62 is not correct. When the binding rate is a fixed $2.79 rate per every 100 sheets and the number of total sheets in that formula is 94. Clearly 94<100. As is outlined in the OP. So, why would she be adding, subtracting, multiplying or dividing anything?

Moreover, At what point would doing all that multiplying, dividing, adding, and subtraction ever be necessary? Why would one not simply add, subtract, multiply or divide the database table fields directly as in:
Code:
<?php printf('$%0.2f', (int)($priceinfo->numSinglePages + $priceinfo->bindingCost) ?>
...or (another possible solution) do the equation in the query and receive the answer as a part of the returned query results.
PHP Code:
<?php printf('$%0.2f', (int)(($priceinfo->numSinglePages 1) / 100 1) * $priceinfo->bindingCost?>

94 pages - 1 = 93
divided by 100 = .93
add 1 = 1.93
multiply by bindingcost = 1.93 * 2.79 = 5.3847
convert the result to an integer: (int) 5.3847 = 5
(12-31-2018, 03:06 PM)Wouter60 Wrote: [ -> ]
PHP Code:
<?php printf('$%0.2f', (int)(($priceinfo->numSinglePages 1) / 100 1) * $priceinfo->bindingCost?>

94 pages - 1 = 93
divided by 100 = .93
add 1 = 1.93
multiply by bindingcost = 1.93 * 2.79 = 5.3847
convert the result to an integer: (int) 5.3847 = 5

Thank your for you help. However, that total has the same flaw as mine. Yours is likely ordered correctly within what we are trying to get PHP to do. However, the outcome was still the same. The 5.3847 or the integer 5, all fails to produce the correct mathematical expected outcome for a flat rate of 2.79 for pages up to 100.

Which brings me to my new question in this same vein and with all the help offered, which is greatly appreciated.. How would I go about making a correct string to compute the following:
Code:
 55.46
+ 140.06
+   7.96
+ $priceinfo->bindingCost) = 2.79, but would like to still use the database calculator field value instead of a raw number.
---------------------------
$206.27 = The cumulative total for "Cost to Print, Laminate, Trim, & Bind in Color Single-Sided:"

Any ideas there? The original line of code was this:
Code:
td style="border-bottom:1px solid;"><?php printf('$%0.2f', $priceinfo->numSinglePages * ($priceinfo->colorSingleCost + $priceinfo->laminateCost)  + (int)(($priceinfo->numSinglePages - 1) / 100 + 1) * $priceinfo->bindingCost) ?></td>

This is on or around Line #152. But as far as I can tell/remember, it did not account for the lamination trim cost in the grand total.

Thank you all again. This has been a very interesting lesson thus far.