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.