Php Price calculation based on given table - php

using this price table we are calculating price of the given input measurement
example:
(1) width = 30; , height =56 ; Price=?
so
(width>25){ new_width=50; }
(height<100){ new_height=100; }
so price = 20;
(2) width =12; height=267;
(width<25){ new_width=25; }
(height>250){ new_height=300; }
so price= 30;
I wrote a function for to calculate price based on the given value, and this function help to get the new_width , and height based on the table , But I don't know how to get price based on this table.
function calculate_price($width,$height ){
if($width<25){
$new_width=25;
}elseif($width>25 && $width<50 ){
$new_width=50;
}else{
$new_width=50;
}
if($width<100){
$new_width=100;
}elseif($width>100 && $width<150 ){
$new_width=150;
}elseif($width>150 && $width<200 ){
$new_width=200;
}elseif($width>200 && $width<250 ){
$new_width=250;
}elseif($width>250 && $width<300 ){
$new_width=300;
}
else{
$new_width=300;
}
$price=needed forumula [ based on new_width & new_height ];
return $price ;
}

You can use ceil to round up the width and height this function
You can use array to store the price with key height_width for simplicity.
function calculate_price($width, $height) {
$price = array(
"100_25" => 10,
"100_50" => 20,
"150_25" => 15,
"150_50" => 25,
"200_25" => 18,
"200_50" => 31,
"250_25" => 24,
"250_50" => 42,
"300_25" => 20,
"300_50" => 45,
);
$newWidth = ceil($width / 25) * 25; //Round up by 25
$newHeight = ceil($height / 50) * 50; //Round up by 50
return $price[ $newHeight . "_" . $newWidth ];
}
You can call
echo calculate_price( 30, 56 ); /* width and height parameters */
This will result to
20

Base on the info you provided, you have two logical paths. The first, is the width less than or equal to 25 or is the width greater then 25. Then you have to reason out the height.
Consider the following:
function calculate_price($width, $height){
$price = false;
if($width > 25){
switch(true){
case $height <= 100:
$price = 20;
break;
case $height <= 150:
$price = 25;
break;
case $height <= 200:
$price = 31;
break;
case $height <= 250:
$price = 42;
break;
case $height <= 300:
$price = 45;
break;
}
} else {
switch(true){
case $height <= 100:
$price = 10;
break;
case $height <= 150:
$price = 15;
break;
case $height <= 200:
$price = 18;
break;
case $height <= 250:
$price = 24;
break;
case $height <= 300:
$price = 30;
break;
}
}
return $price;
}
With the following input: echo calculate_price(30, 56); we would have an output of: 20.
Running the following:
echo calculate_price(30, 56) . "<br />";
echo calculate_price(12, 267) . "<br />";
echo calculate_price(25, 249) . "<br />";
I get:
20
30
24
Format as needed (E.G.: echo sprintf("$%d.00", calculate_price(30, 56));)
Full Test
<?php
function calculate_price($width, $height){
$price = false;
if(!is_int($width) || !is_int($height)){
return $price;
}
if($width > 25){
switch(true){
case $height <= 100:
$price = 20;
break;
case $height <= 150:
$price = 25;
break;
case $height <= 200:
$price = 31;
break;
case $height <= 250:
$price = 42;
break;
case $height <= 300:
$price = 45;
break;
}
} else {
switch(true){
case $height <= 100:
$price = 10;
break;
case $height <= 150:
$price = 15;
break;
case $height <= 200:
$price = 18;
break;
case $height <= 250:
$price = 24;
break;
case $height <= 300:
$price = 30;
break;
}
}
return $price;
}
echo sprintf("$%d.00", calculate_price(30, 56)) . "<br />";
echo sprintf("$%d.00", calculate_price(12, 267)) . "<br />";
echo sprintf("$%d.00", calculate_price(25, 249)) . "<br />";
echo sprintf("$%d.00", calculate_price(-1, 'a')) . "<br />";
?>
Results:
$20.00
$30.00
$24.00
$0.00

You can use Multidimensional Array
$x = "300";
$y = "50";
$multi = array();
$multi["100"]["25"] = "10";
$multi["100"]["50"] = "20";
$multi["200"]["25"] = "30";
$multi["300"]["50"] = "40";
echo $multi[$x][$y];

Based of your code, if you don't have database to get those $price, I think you can try somehting like this :
function calculate_price($width,$height ){
// Your $new_width according to your $width
if($width<=25){
$new_width=25;
}else
$new_width=50;
}
// Your $new_height according to your $height
if($height<=100){
$new_height=100;
}elseif($height>100 && $height<=150 ){
$new_height=150;
}elseif($height>150 && $height<=200 ){
$new_height=200;
}elseif($height>200 && $height<=250 ){
$new_height=250;
}elseif($height>250){
$new_height=300;
}
// Now, according to your new value you will find the price with some test :
if ($new_width == 25) {
switch($new_height) {
case 100 : $price = 10; break;
case 150 : $price = 15; break;
case 200 : $price = 18; break;
case 250 : $price = 24; break;
case 300 : $price = 30; break;
}
} else {
switch($new_height) {
case 100 : $price = 20; break;
case 150 : $price = 25; break;
case 200 : $price = 31; break;
case 250 : $price = 42; break;
case 300 : $price = 45; break;
}
}
return $price ;
}
But I think you can find other way to achieve it too as suggested in other anwser !
Is this what you are looking for? It's not very flexible since you don't use database to get those value but it should work

Related

Uploading profile pictures once (not twice) into database, and displaying them correctly

I am trying to built an option unto my site where users can upload a profile picture, so far so good, but the data is saved again and again in my database(table) when I refresh the page
++ the image doesn't show, it just shows a black square.
Thanks for any help, as you can see, I'm still an absolute beginner learning eagerly and ferociously
$images = (isset ($_FILES['images']['name']));
$paths= [];
if ($images)
{
$saveto = "uploads/$user.jpg";
move_uploaded_file($_FILES['images']['tmp_name'], $saveto);
$typeok = TRUE;
$paths[] = $saveto;
$date1=date("Y-m-d h:m:s");
$link=mysqli_connect("localhost","root","","vlindr");
mysqli_select_db($link,"imageupload"); //database connectivity
mysqli_query($link,"insert into images
(images_id, images_path, submission_date)
VALUES (NULL,'$user','$date1')")
or die(mysqli_error($link));
switch($_FILES['images']['type'])
{
case "image/gif": $src = imagecreatefromgif($saveto); break;
case "image/jpeg": // Both regular and progressive jpegs
case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
case "image/png": $src = imagecreatefrompng($saveto); break;
default: $typeok = FALSE; break;
}
if ($typeok)
{
list($w, $h) = getimagesize($saveto);
$max = 150;
$tw = $w;
$th = $h;
if ($w > $h && $max < $w) {
$th = $max ;
$tw = $max;
}
elseif ($h > $w && $max < $h) {
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w) {
$tw = $th = $max;
}
$tmp = imagecreatetruecolor($tw, $th);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imageconvolution($tmp, array(array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1)),
8, 0);
imagejpeg($tmp, $saveto);
imagedestroy($tmp);
imagedestroy($src);
}
}
Please let me know if the code below works.
I also recommend prepared statements, although the code below is not the neatest it will get the job done!
$images = (isset ($_FILES['images']['name']));
$paths= [];
if ($images)
{
$saveto = "uploads/$user.jpg";
move_uploaded_file($_FILES['images']['tmp_name'], $saveto);
$typeok = TRUE;
$paths[] = $saveto;
$date1=date("Y-m-d h:m:s");
$link=mysqli_connect("localhost","root","","vlindr");
mysqli_select_db($link,"imageupload"); //database connectivity
//Checks if Image already exists in database
$stmt = $link->prepare("SELECT * FROM images WHERE images_path=(?)");
$stmt->bind_param("s",$saveto);
$stmt->execute();
$stmt->store_result();
//If the amount of rows returned is zero, the script will insert the image into the database
if($stmt->num_rows === 0) {
//Inserts the image into the database
$stmt2 = $link->prepare("INSERT INTO images (images_id,images_path,submission_date) VALUES (NULL,?,?)");
$stmt2->bind_param("ss",$saveto,$date1);
if($stmt2->execute()) {
echo "Image is unique and uploaded to database";
}
switch($_FILES['images']['type'])
{
case "image/gif": $src = imagecreatefromgif($saveto); break;
case "image/jpeg": // Both regular and progressive jpegs
case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
case "image/png": $src = imagecreatefrompng($saveto); break;
default: $typeok = FALSE; break;
}
if ($typeok)
{
list($w, $h) = getimagesize($saveto);
$max = 150;
$tw = $w;
$th = $h;
if ($w > $h && $max < $w)
{
$th = $max ;
$tw = $max;
}
elseif ($h > $w && $max < $h)
{
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w)
{
$tw = $th = $max;
}
$tmp = imagecreatetruecolor($tw, $th);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imageconvolution($tmp, array(array(-1, -1, -1),
array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
imagejpeg($tmp, $saveto);
imagedestroy($tmp);
imagedestroy($src);
}
}//end if
else {
echo "Image Already Exists";
}//end else

Multi-dimensional table (excelsheet) as an php array. Or any other ideas?

I have a excelsheet with a table representing the price calculation for a tourplanning system.
You can download the sheet here:
https://www.dropbox.com/s/ctam8iwym9pumjz/Example.xlsx?dl=0
Now, I want to build a PHP function like:
priceCalc($kilometers, $numberOfPersons, $doubleTour)
Example values of parameters:
$kilometers = 130.0; (double)
$numberOfPersons = 4; (integer)
$doubleTour = true; (boolean)
Result has to be with these values = 218.00 € (see excelsheet)
How would you implement it the easiest way (without 3rd-party classes or extensions - pure PHP)?
Got it!
function getEcoPrice($distance, $persons, $doubleTour) {
$distance = ceil($distance); //round up to next integer
$startPrice = 69;
$endprice = $startPrice;
$amountPlusPerson = array(0, 0, 20, 20, 10, 10, 10, 10, 10); //first index is just for setting index = number of persons. value to add, if extra person.
$amountNextDistance = array(0, 10, 10, 10, 10, 10, 10, 10, 10); //first index is just for setting index = number of distance steps. value to add, if next distance is reached.
$amountDoubleTour = array(-20, -20, -20, -20, -20, -20, -20, -20, -20); //value to add, if doubleTour is enabled.
$index = 0;
switch (true) {
case $distance <= 130:
$index = 0;
break;
case $distance <= 160:
$index = 1;
break;
case $distance <= 170:
$index = 2;
break;
case $distance <= 180:
$index = 3;
break;
case $distance <= 190:
$index = 4;
break;
case $distance <= 200:
$index = 5;
break;
case $distance <= 210:
$index = 6;
break;
case $distance <= 220:
$index = 7;
break;
case $distance <= 230:
$index = 8;
break;
case $distance > 230:
return 99999;
break;
}
for($i = 0; $i <= $index; $i++) {
$endprice += $amountNextDistance[$i];
};
for ($i = 0; $i <= $persons; $i++) {
$endprice += $amountPlusPerson[$i];
}
if ($doubleTour) {
$endprice = $endprice * 2 + $amountDoubleTour[$index];
}
return $endprice;
}
function call:
echo getEcoPrice(200.1, 2, false);

how to do XIRR calculation in php?

I have used PHP class from, http://www.phpclasses.org/ but it gives -1571465320791500131898188521929991589462016.00 % this as answer many times(you can take this example, date1= 04/14/2015, date2=08/19/2015(mm/dd/yyyy format), value1=-110,value2=5) Is there any solution for this, any other better go?
This question is asked previously, but there is no satisfactory answer..
-110 14-Apr-15
5 19-Aug-15
--------------------
-0.99986137
Above is what Excel shows,,,
Below is the code from php class
///////////for xirr calc////////////////
function DATEDIFF($datepart, $startdate, $enddate)
{
switch (strtolower($datepart)) {
case 'yy':
case 'yyyy':
case 'year':
$di = getdate($startdate);
$df = getdate($enddate);
return $df['year'] - $di['year'];
break;
case 'q':
case 'qq':
case 'quarter':
die("Unsupported operation");
break;
case 'n':
case 'mi':
case 'minute':
return ceil(($enddate - $startdate) / 60);
break;
case 'hh':
case 'hour':
return ceil(($enddate - $startdate) / 3600);
break;
case 'd':
case 'dd':
case 'day':
return ceil(($enddate - $startdate) / 86400);
break;
case 'wk':
case 'ww':
case 'week':
return ceil(($enddate - $startdate) / 604800);
break;
case 'm':
case 'mm':
case 'month':
$di = getdate($startdate);
$df = getdate($enddate);
return ($df['year'] - $di['year']) * 12 + ($df['mon'] - $di['mon']);
break;
default:
die("Unsupported operation");
}
}
function XNPV($rate, $values, $dates)
{
if ((!is_array($values)) || (!is_array($dates))) return null;
if (count($values) != count($dates)) return null;
$xnpv = 0.0;
for ($i = 0; $i < count($values); $i++)
{
$xnpv += $values[$i] / pow(1 + $rate, $this->DATEDIFF('day', $dates[0], $dates[$i]) / 365);
}
return (is_finite($xnpv) ? $xnpv: null);
}
function XIRR($values, $dates, $guess = 0.1)
{
if ((!is_array($values)) && (!is_array($dates))) return null;
if (count($values) != count($dates)) return null;
// create an initial bracket, with a root somewhere between bot and top
$x1 = 0.0;
$x2 = $guess;
$f1 = $this->XNPV($x1, $values, $dates);
$f2 = $this->XNPV($x2, $values, $dates);
for ($i = 0; $i < FINANCIAL_MAX_ITERATIONS; $i++)
{
if (($f1 * $f2) < 0.0) break;
if (abs($f1) < abs($f2)) {
$f1 = $this->XNPV($x1 += 1.6 * ($x1 - $x2), $values, $dates);
} else {
$f2 = $this->XNPV($x2 += 1.6 * ($x2 - $x1), $values, $dates);
}
}
if (($f1 * $f2) > 0.0) return null;
$f = $this->XNPV($x1, $values, $dates);
if ($f < 0.0) {
$rtb = $x1;
$dx = $x2 - $x1;
} else {
$rtb = $x2;
$dx = $x1 - $x2;
}
for ($i = 0; $i < FINANCIAL_MAX_ITERATIONS; $i++)
{
$dx *= 0.5;
$x_mid = $rtb + $dx;
$f_mid = $this->XNPV($x_mid, $values, $dates);
if ($f_mid <= 0.0) $rtb = $x_mid;
if ((abs($f_mid) < FINANCIAL_ACCURACY) || (abs($dx) < FINANCIAL_ACCURACY)) return $x_mid;
}
return null;
}
And We have to call it like this
echo 'XIRR: ' . $f->XIRR(array(-10000,2750,4250,3250,2750), array(
mktime(0,0,0,1,1,2008),
mktime(0,0,0,3,1,2008),
mktime(0,0,0,10,30,2008),
mktime(0,0,0,2,15,2009),
mktime(0,0,0,4,1,2009),
), 0.1) . "\n";

Using PHP to grab a simplified colour palette from an image?

So far I have found examples on how to grab all the RGB values of each pixel in an image but I want something that will break down an image and give me a simplified color palette.
Is there a way to use imagetruecolortopalette to somehow spit out the reduced palette colours, or to break an image into 25 x 25 blocks and then grab the average value of that block?
Maybe there is an alternative I'm missing? I basically just want to be able to find the most common colors within an image.
Thanks in advance.
Hmm, well I have had created something like this for a client. The screenshot is below
The complete code is as follows
$microTime = microtime(true);
function textColor($R1, $G1, $B1) {
$a = (($R1 * 299) + ($G1 * 587 ) + ($B1 * 114 )) / 1000;
if ($a < 128)
return 'white';
else
return 'black';
}
function rgb2html($r, $g = -1, $b = -1) {
$hex = "#";
$hex.= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
$hex.= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
$hex.= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
return $hex;
if (is_array($r) && sizeof($r) == 3)
list($r, $g, $b) = $r;
$r = intval($r);
$g = intval($g);
$b = intval($b);
$r = dechex($r < 0 ? 0 : ($r > 255 ? 255 : $r));
$g = dechex($g < 0 ? 0 : ($g > 255 ? 255 : $g));
$b = dechex($b < 0 ? 0 : ($b > 255 ? 255 : $b));
$color = (strlen($r) < 2 ? '0' : '') . $r;
$color .= (strlen($g) < 2 ? '0' : '') . $g;
$color .= (strlen($b) < 2 ? '0' : '') . $b;
return '#' . $color;
}
function colorPalette($imageFile, $colorJump, $granularity = 5) {
$granularity = max(1, abs((int) $granularity));
$colors = array();
$ratio = array();
$wastageCount = array();
$occurrenceSCount = array();
$occurrenceMCount = array();
$size = #getimagesize($imageFile);
if ($size === false) {
return false;
}
$img = #imagecreatefromstring(file_get_contents($imageFile));
if (!$img) {
user_error("Unable to open image file");
return false;
}
for ($y = 0; $y < $size[1]; $y += $granularity) {
$lastColor = NULL;
$lastX = -1;
for ($x = 0; $x < $size[0]; $x += $granularity) {
$thisColor = imagecolorat($img, $x, $y);
$rgb = imagecolorsforindex($img, $thisColor);
$red = round(round(($rgb['red'] / $colorJump)) * $colorJump);
$green = round(round(($rgb['green'] / $colorJump)) * $colorJump);
$blue = round(round(($rgb['blue'] / $colorJump)) * $colorJump);
$thisRGB = $red . ',' . $green . ',' . $blue;
if ($lastColor != $thisRGB) {
if (array_key_exists($thisRGB, $wastageCount)) {
$wastageCount[$thisRGB]++;
} else {
$wastageCount[$thisRGB] = 1;
}
if ($lastX + 1 == $x) {
if (array_key_exists($lastColor, $occurrenceSCount)) {
$occurrenceSCount[$lastColor]++;
} else {
$occurrenceSCount[$lastColor] = 1;
}
}
if ($lastX + 1 != $x) {
if (array_key_exists($lastColor, $occurrenceMCount)) {
$occurrenceMCount[$lastColor]++;
} else {
$occurrenceMCount[$lastColor] = 1;
}
}
$lastColor = $thisRGB;
$lastX = $x;
}
if (array_key_exists($thisRGB, $colors)) {
$colors[$thisRGB]++;
} else {
$colors[$thisRGB] = 1;
}
}
}
$totalPixels = array_sum($colors);
foreach ($colors as $k => $v) {
$ratio[$k] = round(($v / $totalPixels ) * 100, 2);
}
return array($ratio, $wastageCount, $colors, $occurrenceSCount, $occurrenceMCount);
}
usage
$colorJump = 1;
$pixelJump = 1;
$paletteR = colorPalette($dbImgFile_dir, $colorJump, $pixelJump);
$palette = $paletteR[0];
$wastage = $paletteR[1];
$colorsFound = $paletteR[2];
$occSArray = $paletteR[3];
$occMArray = $paletteR[4];
$totalPixels = array_sum($colorsFound);
$totalTime = abs(microtime(true) - $microTime);
the looping around is more complex, as I have to get the pallet from the DB and to match the colors with them, and also the template parser is used which is full custom code, and will not help you.
Just ignore the Required Weight column from this, the single occurrences and multiple occurrences are calculated, if there is a single pixel, for example RED, RED, BLUE, RED, RED it will be 1 single occurrence and 2 multiple occurrence

Rounding to the Nearest Ending Digits

I have the following function that rounds a number to the nearest number ending with the digits of $nearest, and I was wondering if there is a more elegant way of doing the same.
/**
* Rounds the number to the nearest digit(s).
*
* #param int $number
* #param int $nearest
* #return int
*/
function roundNearest($number, $nearest, $type = null)
{
$result = abs(intval($number));
$nearest = abs(intval($nearest));
if ($result <= $nearest)
{
$result = $nearest;
}
else
{
$ceil = $nearest - substr($result, strlen($result) - strlen($nearest));
$floor = $nearest - substr($result, strlen($result) - strlen($nearest)) - pow(10, strlen($nearest));
switch ($type)
{
case 'ceil':
$result += $ceil;
break;
case 'floor':
$result += $floor;
break;
default:
$result += (abs($ceil) <= abs($floor)) ? $ceil : $floor;
break;
}
}
if ($number < 0)
{
$result *= -1;
}
return $result;
}
Some examples:
roundNearest(86, 9); // 89
roundNearest(97, 9); // 99
roundNearest(97, 9, 'floor'); // 89
Thanks in advance!
PS: This question is not about rounding to the nearest multiple.
This works for me:
function roundToDigits($num, $suffix, $type = 'round') {
$pow = pow(10, floor(log($suffix, 10) + 1));
return $type(($num - $suffix) / $pow) * $pow + $suffix;
};
$type should be either "ceil", "floor", or "round"
I think this should work, and it's more elegant to me, at least:
function roundNearest($number, $nearest, $type = null)
{
if($number < 0)
return -roundNearest(-$number, $nearest, $type);
$nearest = abs($nearest);
if($number < $nearest)
return $nearest;
$len = strlen($nearest);
$pow = pow(10, $len);
$diff = $pow - $nearest;
if($type == 'ciel')
$adj = 0.5;
else if($type == 'floor')
$adj = -0.5;
else
$adj = 0;
return round(($number + $diff)/$pow + $adj)*$pow - $diff;
}
Edit: Added what I think you want from negative inputs.

Categories