I have this simple pricing table. I'd want to insert another 2 columns displaying MIN and MAX values from column $przoff.
Here's a screenshot that shows how columns should look like:
<?php
$i=0;
while ($i < $num) {
$cod=mysql_result($result,$i,"cod_int");
$name=mysql_result($result,$i,"nome");
$conf=mysql_result($result,$i,"conf");
$type=mysql_result($result,$i,"tipo");
$cconf=mysql_result($result,$i,"cost");
$cunit=mysql_result($result,$i,"costo_prod");
$incqta=mysql_result($result,$i,"incid_qta");
$inconf=($cconf*$incqta/100)+($cunit*$incqta/100);
if ($cunit > 0 && $cunit <= 0.2500) {
$incprd=$cunit*10/100; }
elseif ($cunit > 0.2500 && $cunit <= 0.5000) {
$incprd=$cunit*5/100; }
else {
$incprd=0; }
$przoff=($cconf+$cunit+$inconf+$incprd);
?>
<tr>
/* TABLE CONTENT */
</tr>
<?php $i++; } ?>
Find the min and max value before printing the table with same process. Then print in table.
Process 1:
<?php
$i = 0;
while ($i < $num) {
$cunit = mysql_result($result, $i, "costo_prod");
$incqta = mysql_result($result, $i, "incid_qta");
$inconf = ($cconf * $incqta / 100) + ($cunit * $incqta / 100);
if ($cunit > 0 && $cunit <= 0.2500) {
$incprd = $cunit * 10 / 100;
} elseif ($cunit > 0.2500 && $cunit <= 0.5000) {
$incprd = $cunit * 5 / 100;
} else {
$incprd = 0;
}
$przoff = ($cconf + $cunit + $inconf + $incprd);
if ($i == 0) {
$min_val = $przoff;
$max_val = $przoff;
} else {
if ($przoff < $min_val) {
$min_val = $przoff;
}
if ($przoff > $max_val) {
$max_val = $przoff;
}
}
?>
<?php
$i++;
}
$i = 0;
while ($i < $num) {
$cod = mysql_result($result, $i, "cod_int");
$name = mysql_result($result, $i, "nome");
$conf = mysql_result($result, $i, "conf");
$type = mysql_result($result, $i, "tipo");
$cconf = mysql_result($result, $i, "cost");
$cunit = mysql_result($result, $i, "costo_prod");
$incqta = mysql_result($result, $i, "incid_qta");
$inconf = ($cconf * $incqta / 100) + ($cunit * $incqta / 100);
if ($cunit > 0 && $cunit <= 0.2500) {
$incprd = $cunit * 10 / 100;
} elseif ($cunit > 0.2500 && $cunit <= 0.5000) {
$incprd = $cunit * 5 / 100;
} else {
$incprd = 0;
}
$przoff = ($cconf + $cunit + $inconf + $incprd);
$min_val = $min_val;
$max_val = $max_val;
?>
<tr>
/* TABLE CONTENT */
</tr>
<?php
$i++;
}
?>
Process 2:
<?php
$min_val = "";
$max_val = "";
$i = 0;
while ($i < $num) {
$cunit = mysql_result($result, $i, "costo_prod");
$incqta = mysql_result($result, $i, "incid_qta");
$inconf = ($cconf * $incqta / 100) + ($cunit * $incqta / 100);
if ($cunit > 0 && $cunit <= 0.2500) {
$incprd = $cunit * 10 / 100;
} elseif ($cunit > 0.2500 && $cunit <= 0.5000) {
$incprd = $cunit * 5 / 100;
} else {
$incprd = 0;
}
$przoff = ($cconf + $cunit + $inconf + $incprd);
if ($przoff < $min_val || empty($min_val)) {
$min_val = $przoff;
}
if ($przoff > $max_val || empty($max_val)) {
$max_val = $przoff;
}
?>
<?php
$i++;
}
$i = 0;
while ($i < $num) {
$cod = mysql_result($result, $i, "cod_int");
$name = mysql_result($result, $i, "nome");
$conf = mysql_result($result, $i, "conf");
$type = mysql_result($result, $i, "tipo");
$cconf = mysql_result($result, $i, "cost");
$cunit = mysql_result($result, $i, "costo_prod");
$incqta = mysql_result($result, $i, "incid_qta");
$inconf = ($cconf * $incqta / 100) + ($cunit * $incqta / 100);
if ($cunit > 0 && $cunit <= 0.2500) {
$incprd = $cunit * 10 / 100;
} elseif ($cunit > 0.2500 && $cunit <= 0.5000) {
$incprd = $cunit * 5 / 100;
} else {
$incprd = 0;
}
$przoff = ($cconf + $cunit + $inconf + $incprd);
$min_val = $min_val;
$max_val = $max_val;
?>
<tr>
/* TABLE CONTENT */
</tr>
<?php
$i++;
}
?>
Only difference is min value and max value initializing.
Related
I have tried to complete Euler project problem 11 but somehow I got a product that was actually above the right answer. I have tried to calculate the adjacent numbers through the use of a single dimensional array and nested for loops. What error in my code is making this occur?
Code:
$numbers = str_replace(" ","",$numbers);
$numbers = str_split($numbers, 2);
function removeZeros($numbers) {
for ($i = 0; $i < count($numbers); $i++) {
$firstChar = substr($numbers[$i], 0, 1);
if ($firstChar == "0") {
$numbers[$i] = substr($numbers[$i], 1, 1);
}
}
return $numbers;
}
function gridProduct($numbers) {
removeZeros($numbers);
$i = 0;
$product = 0;
$largestGrid = 0;
// across
for ($j = 0; $j < 20; $j++) {
for ($i = 0; $i < 17; $i++) {
$k = $i + ($j * 20);
$product = $numbers[$k] * $numbers[$k + 1] * $numbers[$k + 2] * $numbers[$k + 3];
if ($product > $largestGrid) {
$largestGrid = $product;
}
}
}
// vertically
for ($j = 0; $j < 20; $j++) {
for ($i = 0; $i < 17; $i++) {
$k = $j + ($i * 20);
$product = $numbers[$k] * $numbers[$k + 20] * $numbers[$k + 40] * $numbers[$k + 60];
if ($product > $largestGrid) {
$largestGrid = $product;
}
}
}
// diagonally right
for ($j = 0; $j < 17; $j++) {
for ($i = 0; $i < 17; $i++) {
$k = $i + ($j * 20);
$product = $numbers[$k] * $numbers[$k + 21] * $numbers[$k + 21] * $numbers[$k + 21];
if ($product > $largestGrid) {
$largestGrid = $product;
}
}
}
// diagonally left
for ($j = 19; $j > 4; $j--) {
for ($i = 0; $i < 17; $i++) {
$k = $i + ($j * 20);
$product = $numbers[$k] * $numbers[$k - 19] * $numbers[$k - 19] * $numbers[$k - 19];
if ($product > $largestGrid) {
$largestGrid = $product;
}
}
}
echo " ";
echo $largestGrid;
}
gridProduct($numbers);```
I have a PHP pie chart which uses data from Mysql to show the chart. However if one data is missing the whole chart turns to one color. For example for grading if the input is A, B, D and F(pay attention C grade input is missing) then the whole pie chart is in one color like Orange or red.
Can you please help me with this? Thanks
<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('0000ff', '006600', 'ffff00','DD7C1D', 'FF3300', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...
// DON'T CHANGE ANYTHING BELOW THIS LINE...
$data = $_GET["data"];
$label = $_GET["label"];
$height = $width/2;
$data = array_filter(explode('*',$data));
if ($label != '') $label = explode('*',$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == 'round' && $show_label)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == 'square' && $show_label)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($show_percent) $label_output = $number[$i].' ';
if ($show_text) $label_output = $label_output.$label[$i].' ';
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = '';
$label_place = $label_place + 15;
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i--)
{
for ($j = 0; $j < count($slice); $j++)
{
ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header('Content-type: image/jpg');
ImageJPEG($img,NULL,100);
}
?>
im trying to translate a sortingmethod from Javascript to php and i have run in to some trouble.
The code looks as follows:
private static function quicksort($ids, $dists, $left, $right) {
if ($right - $left <= 20) {
for ($i = $left + 1; $i <= $right; $i++) {
$temp = $ids[$i];
$tempDist = $dists[$temp];
$j = $i - 1;
while ($j >= $left && $dists[$ids[$j]] > $tempDist) {
$ids[$j + 1] = $ids[$j--];
}
$ids[$j + 1] = $temp;
}
} else {
$median = ($left + $right) >> 1;
$i = $left + 1;
$j = $right;
self::swap($ids, $median, $i);
if ($dists[$ids[$left]] > $dists[$ids[$right]]) self::swap($ids, $left, $right);
if ($dists[$ids[$i]] > $dists[$ids[$right]]) self::swap($ids, $i, $right);
if ($dists[$ids[$left]] > $dists[$ids[$i]]) self::swap($ids, $left, $i);
$temp = $ids[$i];
$tempDist = $dists[$temp];
while (true) {
do $i++; while ($dists[$ids[$i]] < $tempDist);
do $j--; while ($dists[$ids[$j]] > $tempDist);
if ($j < $i) break;
self::swap($ids, $i, $j);
}
$ids[$left + 1] = $ids[$j];
$ids[$j] = $temp;
if ($right - $i + 1 >= $j - $left) {
self::quicksort($ids, $dists, $i, $right);
self::quicksort($ids, $dists, $left, $j - 1);
} else {
self::quicksort($ids, $dists, $left, $j - 1);
self::quicksort($ids, $dists, $i, $right);
}
}
}
private static function swap($arr, $i, $j) {
$tmp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $tmp;
}
I run this code with these parameters:
$ids - array with 80 objects
$dists - array with 80 objects
$left - 0
$right - 79
and get an error on this line saying Undefined offset: 161
do $i++; while ($dists[$ids[$i]] < $tempDist);
This is my scenario:
if units are <= 100 then charge Rs 1 rupee/unit
if units are > 100 && <= 200 then charge Rs 2 rupee/unit
if units are > 200 && <= 300 then charge Rs 3 rupee/unit
if units are > 300then charge Rs 7 rupee/unit
if($units > 100){
$bill = 100 * 1;
$remaining_units= $units -100;
if($remaining_units > 100 ){
$remaining_units= $remaining_units -100;
$bill = $bill + (100* 2);
if($remaining_units > 100 || $remaining_units < 100){
$remaining_units= $remaining_units -100;
$bill = $bill + (100* 3);
if($remaining_units > 100 || $remaining_units < 100){
$bill = $bill + (100* 7);
}
}
}
}
echo $bill;
Use this function this function will work for you:
echo calculate_bill(210);
function calculate_bill($units, $multipler = 1, $oldBill=0){
if($units > 100){
$remainingUnits = $units -100;
$oldBill = (100 * $multipler) + $oldBill;
}else{
$a = ($units * $multipler) + ($oldBill);
return $a;
}
/*first level complete*/
if($multipler == 1){
$multipler = 2;
}elseif($multipler == 2){
$multipler = 3;
}elseif($multipler >= 3){
$multipler = 7;
}
return calculate_bill($remainingUnits,$multipler, $oldBill);
}
Ya its working fine now
if($units < 100){
$bill = $units*1;
}elseif($units > 100 && $units <=200){
$temp = 100*1;
$remaining_units = $units - 100;
$bill = $temp + ($remaining_units *2);
}elseif($units > 200 && $units <=300){
$temp = (100*1)+ (100*2);
$remaining_units = $units - 200;
$bill = $temp + ($remaining_units *3);
}else{
$temp = (100*1)+ (100*2)+ (100*3);
$remaining_units = $units - 300;
$bill = $temp + ($remaining_units *7);
}
echo $bill;
I want to create piechart in my pdf file created using fpdf. already i had created pdf with fpdf . then i want to create pie chart in that using same table data, is there any option to create pie chart using fpdf ?
Please Help.
Thanks in advance
Try this make changes in code as per your requirements:
You can display following view file on your pdf using pdf helper.
you can use dom pdf helper download it from following link.
http://code.google.com/p/dompdf/downloads/detail?name=dompdf_0-6-0_beta3.zip
<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('003366', 'CCD6E0', '7F99B2','F7EFC6', 'C6BE8C', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...
// DON'T CHANGE ANYTHING BELOW THIS LINE...
$data = $_GET["data"];
$label = $_GET["label"];
$height = $width/2;
$data = explode('*',$data);
if ($label != '') $label = explode('*',$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == 'round' && $show_label && $data[$i] > 0)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == 'square' && $show_label && $data[$i] > 0)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($data[$i] > 0)
{
if ($show_percent) $label_output = $number[$i].' ';
if ($show_text) $label_output = $label_output.$label[$i].' ';
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = '';
$label_place = $label_place + 15;
}
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i--)
{
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header('Content-type: image/jpg');
ImageJPEG($img,NULL,100);
}
?>
Hope this will help you... :)