in "$grandtotal1" and "$grandtotal2" does not display the sum
while($data=mysql_fetch_array($hasil)){
$tgl =$data['date_added'];
$jenis_pemilik=$data['pemilik'];
$no_invoice =$data['order_id'];
$pengiriman =$data['shipping_method'];
$berat =$data['weight'];
$namabarang =$data['name'];
$artikel =$data['model'];
$retails =$data['retail']*1;
$odv =$data['odv'];
$ongkir =$data['text'];
$diskon =$data['price']*1;
$harga_jual =$data['harga_jual']*1;
$stars =(($harga_jual-$odv)*80)/100*1;
$grandtotal1 +=($stars);
$divisi =(($harga_jual-$odv)*20)/100*1;
$grandtotal2 +=($divisi);
I apply this code to FPDF does not work , but I apply to html page works
from looking at your code - it seems you want to add 80% of an amount to $grandtotal1 and 20% to $grandtotal 2 - meaning that these are calculations and so the += is appropriate since you want to add numbers rather than concatenate values. I think the issue is that you haven't declared these values intiially so there is nothing to add them to.
before the while loop - declare the values:
$grandtotal1 = 0;
$grandtotal2 = 0;
while($data=mysql_fetch_array($hasil)){....
then when it is time, add the values
$stars =(($harga_jual-$odv)*80)/100*1;
$grandtotal1 +=($stars);
$divisi =(($harga_jual-$odv)*20)/100*1;
$grandtotal2 +=($divisi);
This will add the values of the calculations to each of the $grandTotals. not sure why you are multiplying all values by 1?
Related
I made a survey page and there I'm reading the number of votes from a DB.
Now I wanted to get the percentage of votes for displaying a "progress bar".
I wrote this funcion to get the percentage:
function progress($cur, $max, $round=1) {
return ($max!=0) ? round(($cur/$max) *100, $round).'%' : '0%';
}
This seemed to be working fine, but for some numbers it adds many zeros and a 1 to the correct value.
For example: 3/34 -> 8.800000000000001%. For other values (tested, 1,2,4,6) it's working correctly.
How can I correct this to display 8.8%?
Use the sprintf function in PHP:
$formattedOutput = sprintf("%.1f",$floatValue);
gives you the the $floatValue with one decimal.
see PHP manual for sprintf
I hope that someone can help me figure this out because it is driving me crazy. First off some background and values of the variables below.
The $TritPrice variable fluctuates as it comes from another source but for an example, lets say that the value of it is 5.25
$RefineTrit is constant at 1000 and $Minerals[$oretype][0] is 333
When I first goto the page where this code is, and this function runs for some reason the $TritPrice var either get truncated to 5.00 or gets rounded down but only during the formula itself. I can echo each of variables and they are correct but when I echo the formula and do the math manually the $TritPrice is just 5 instead of 5.25.
If I put in $TritPrice = 5.25; before the if statement it works fine and after the form is submitted and this function is rerun it works fine.
The page that uses this function is at here if yall want to see what it does.
If ($Minerals[$oretype][1] <> 0) {
$RefineTrit = getmintotal($oretype,1);
if ($RefineTrit < $Minerals[$oretype][1]) {
$NonPerfectTrit = $Minerals[$oretype][1] +
($Minerals[$oretype][1] - $RefineTrit);
$Price = (($TritPrice * $NonPerfectTrit) / $Minerals[$oretype][0]);
} else {
$Price = $TritPrice * $RefineTrit / $Minerals[$oretype][0];
}
}
This is where the $TritPrice
// Get Mineral Prices
GetCurrentMineralPrice();
$TritPrice = $ItemPrice[1];
$PyerPrice = $ItemPrice[2];
$MexPrice = $ItemPrice[3];
$IsoPrice = $ItemPrice[4];
$NocxPrice = $ItemPrice[5];
$ZydPrice = $ItemPrice[6];
$MegaPrice = $ItemPrice[7];
$MorPrice = $ItemPrice[8];
and the GetCurrentMineralPrice() function is
function GetCurrentMineralPrice() {
global $ItemPrice;
$xml = simplexml_load_file("http://api.eve-central.com/api/marketstat?typeid=34&typeid=35&typeid=36&typeid=37&typeid=38&typeid=39&typeid=40&typeid=11399&usesystem=30000142");
$i = 1;
foreach ($xml->marketstat->type as $child) {
$ItemPrice[$i] = $child->buy->max;
$i++;
}
return $ItemPrice;
}
The problem is not in this piece of code. In some other part of the program, and I suspect it is the place where the values from the textboxes are accepted and fed into the formula - in that place there should be a function or code snippet that is rounding the value of $TritPrice. Check the place where the $_POST values are being fetched and also check if any javascript code is doing a parseInt behind the scenes.
EVE-Online ftw.
with that out of the way, it's possible that your precision value in your config is set too low? Not sure why it would be unless you changed it manually. Or you have a function that is running somewhere that is truncating your variable when you call it from that function/var
However, can you please paste the rest of the code where you instantiate $TritPrice please?
there. I'm having a problem with creating arrays in certain conditions in php, i'll try to explain. Here's my code:
for ($i = 1; $i < $tamanho_array_afundamento; $i++) {
if ($array_afundamento[$i] - $array_afundamento[$i - 1] > 1) {
$a = $array_afundamento[$i - 1];
$con->query('CREATE TABLE IF NOT EXISTS afunda_$a
SELECT (L1_forma_tensao_max + L1_forma_tensao_min)/2 as L1_forma_tensao, (L2_forma_tensao_max + L2_forma_tensao_min)/2 as L2_forma_tensao, (L3_forma_tensao_max + L3_forma_tensao_min)/2 as L3_forma_tensao
FROM afundamento
WHERE id > $prevNum AND id < $a');
$tabelas_intervalos_afunda1 = ($con->query("SELECT * FROM afunda_$a");
while ($row = $tabelas_intervalos_afunda->fetch(PDO::FETCH_ASSOC)) {
$array_forma_onda_fase1_afund[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund[] = $row['L3_forma_tensao'];
}
$prevNum = $a;
}
}
So as u can see, i have an if statement in a for loop, what i'm wishing to do is to create
one set of:
{
$array_forma_onda_fase1_afund[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund[] = $row['L3_forma_tensao'];
}
every time the if statement is runned. I was trying replacing this in the original code:
{
$array_forma_onda_fase1_afund_$a[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund_$a[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund_$a[] = $row['L3_forma_tensao'];
}
so as $a is changed everytime the if statement is accessed, i could have a different set of these arrays for everytime the if statement is accessed, but php doesn't accept this and i wouldn't have a very good result, though if i can reach it i would be pleased.
But my goal is to get:
{
$array_forma_onda_fase1_afund_1[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund_1[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund_1[] = $row['L3_forma_tensao'];
}
{
$array_forma_onda_fase1_afund_2[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund_2[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund_2[] = $row['L3_forma_tensao'];
}
...
where the last number represents the array retrieved for the n-th time the if statement runned. Does someone have a tip for it?
Thanks in advance! Would appreciate any help.
EDIT
As asked, my real world terms is as follows:
I have a table from which i need to take all the data that is inside a given interval. BUT, there's a problem, my data is a sine function whose amplitude may change indefinite times (the data bank is entered by the user) and, when the amplitude goes inside that interval, i need to make some operations like getting the least value achieved while the data was inside that interval and some other parameters, for each interval separately, (That's why i created all those tables.) and count how many times it happpened.
So, in order to make one of the operations, i need an array with the data for each time the databank entered by the user goes in that interval (given by the limits of the create query.).
If i were not clear, just tell me please!
EDIT 2
Here's the image of part of the table i'm working with:
http://postimg.org/image/5vegnk043/
so, when the sine gets inside the interval i need, it can be seen by the L1_RMS column, who accuses it, so it's when i need to get the interval data until it gets outside the interval. But it may happens as many times as this table entered by the user brings it on and we need to bear in mind that i need all the intervals separately to deal with the data of each one.
Physics uh?
You can do what you wanted with the arrays, it's not pretty, but it's possible.
You can dynamically name your arrays with the _$a in the end, Variables variables, such as:
${"array_forma_onda_fase3_afund_" . $a}[] = "fisica é medo";
I'm writing a script that calculates the total distance, in miles, traveled on a trip. It needs to calculate the total distance, and the distance between any midpoints. Right now it calculates the total distance fine. I am using drop-boxes, and have allowed to it accept up to 5 midpoints. Now, I may just be trying to make this more complicated than it needs to be but...
I'd like to be able to run the script even if three of the midpoint forms are left empty (which would just say "Select City" with a value of " " or null). Is there a way I can have the script run when the three fields of the form are left empty??
EDIT: Each 'index' or 'pont' is defined with a = " ";. I'm using a multidimentional array for the whole script and the 7 drop boxes start with a selected value of "Select City" which has an empty/null value. I just want to know how to run the form/script when two of the mid-point values/drop boxes are left with the initial value of "Select City".
Sorry, here is some of my code:
<?php
if (isset($_POST['Submit'])) {
$StartIndex = stripslashes($_POST['Start']);
$EndIndex = stripslashes($_POST['End']);
if (isset($Distances[$StartIndex][$EndIndex]))
echo "Total trip distance from $StartIndex to $EndIndex is". $Distances[$StartIndex][$EndIndex]." miles<br/>";
else
echo "Distance could not be calculated";
}
?>
This is where I'm stopped at...
<?php
if (isset($_POST['Submit'])) {
$Point1 = stripslashes($_POST['1']);
?>
^^the other four midpoints are the same, I have zero clue how to still run the form when only the start city, end city, and two mid-point cities are selected. I'd add the whole script but it very long with the multidimensional array
To see what my form looks like you can go to >> 131.118.95.215/users/mjswiontek0/project/city/citytocity.php
Just evaluate your test (e.g. empty($_POST["1"])) on every midpoint to find, if you want to include it into calculation.
See commented (very generic) example:
if (isset($_POST['Submit']))
{
// start with the start index
$PrevIndex = stripslashes($_POST['Start']);
$Point = 1;
$Distance = 0;
do
{
// do we have the N-th midpoint?
$HavePoint = !empty($_POST[$Point]);
// if not, use end point
$NextIndex = stripslashes($_POST[$HavePoint ? $Point : 'End']);
// add distance between the two midpoint to total
$Distance += $Distances[$PrevIndex][$NextIndex];
// make current point the previous point to the next iteration
$PrevIndex = $NextIndex;
// move to the next point for the next iteration
$Point++;
}
while ($HavePoint);
}
If I have this pattern to build ID's:
CX00 where the 0's are replaceable with just a number.. but the following is already in use:
- CX00
- CX02
- CX04
- CX05
- CX07
- CX10
- CX11
- CX12
How can I easily find, either via PHP or MySQL the values CX01, CX03, CX06, CX08, CX09, CX13+ as available values?
I don't know how your data is stored, so I'll leave getting the IDs in an array. Once you do, this will find the next available one.
<?php
function make_id($n) {
$out = 'CX';
if ($n < 10) $out .= '0';
return $out . $n;
}
// Get these from some source
$items = array('CX00', 'CX02', 'CX04', 'CX05');
$id = make_id(0);
for($i=0; in_array($id, $items); $i++)
$id = make_id($i);
echo $id;
?>
This is called a brute-force method, and if you have a lot of IDs there are probably more efficient ways. With 100 maximum, there shouldn't be any problems.
In php simply count up through the ids using a for loop until you have found enough of the unused ids... how many total ids do you expect to have?