I would like to sum 2 values, like 3,07 + 5.1 and I am getting result 8,10 €, not 8,17 €. What am I doing wrong?
<?php
$rezultat = "SELECT sum(value) as value FROM values";
$result = mysqli_query($link, $res) or die (mysqli_error($link));
$sum= mysqli_fetch_object($result);
echo number_format($sum->value, 2, ",", "");
mysqli_close($link);
?>
it seems a decimal separator issue. try replace "," by "." like this:
echo number_format($sum->value, 2, ".", "");
number_format(number,decimals,decimalpoint,separator)
see: http://www.w3schools.com/php/func_string_number_format.asp
This works for me:
$kategorija = mysqli_real_escape_string($link, $_POST['kategorija']);
$vrednost = number_format(str_replace("#", ".", preg_replace("/[^0-9#]/i", "", preg_replace("/[\.\,](?!.*[\.\,])/i", "#", $_POST['kolicina']))), 2, ".", "");
$vrednost = mysqli_real_escape_string($link, $vrednost);
Related
In my php code, while inserting new record, invoice number should be 0001. Here I want to add characters/a word(for example 'optical') and it should be like optical0001. How can I add this before the number?
$query = "select * from cust_report_invoices order by invoiceID desc limit 1";
$result = $link->query($query);
$row = $result->fetch_assoc();
$invoiceNo = $row['invoiceNo'];
$prefix = 'Baopt_';
$getinvoiceNo = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
$sql = "INSERT INTO cust_report_invoices (invoiceNo,invoiceDate)
VALUES ('$getinvoiceNo', '$getinvoiceDate' )";
str_pad return string (doc). So just concat with .:
$prefix = 'optical';
$invoice = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
Edit
When adding new element you need to cut the prefix out - you can use this:
$prefix = 'Baopt_';
$invoiceNo = $row['invoiceNo'];
if (substr($invoiceNo, 0, strlen($prefix)) == $prefix) // if already has prefix
$invoiceNo = substr($invoiceNo, strlen($prefix)); // remove it
$getinvoiceNo = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
When working in PHP you can use the . as concatenation-operator. I see that you use the function str_pad that returns as string.
So you can just concat the prefix with the invoice number like this.
$prefix = 'optical';
$invoice = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
$prefix can be anything you want.
When invoice = 0001 this example will return optical0001
Just "Opticals".$getinvoiceNo
Dot is concatenating strings.
Not sure if I understand correctly, you want to prepend "optical" to $getinvoiceNo?
If yes then it is simple,
invoiceId = "optical$getinvoiceNo"
I have the code from the database, the data should appear automatically in sequence. the code is composed of text and number like this CO-00001 but when I generate the code it just adds one digit in the back but not add the value of example CO-00001 to CO-000001 how to solve it?
ex :
$id = 0902340
$query = "SELECT substr(code_id,3,5) as cd FROM tb_inv WHERE substr(code_id,3,5) = '".$id."' ORDER BY code_id DESC LIMIT 1";
$get_id = $this->db->query($query);
$show_id = $get_id->num_rows();
$new_code = $get_id->row();
if($show_id > 0){
$new = $new_code->cd + 1;
$fix_code = $new;
}else{
$fix_code = 'CO-00001';
}
you can't do algebra on alphabets, therefore you need to split the alphabets and the numerics like so:
$id = 0902340
$query = "SELECT substr(code_id,3,5) as cd FROM tb_inv WHERE substr(code_id,3,5) = '".$id."' ORDER BY code_id DESC LIMIT 1";
$get_id = $this->db->query($query);
$show_id = $get_id->num_rows();
$new_code = $get_id->row();
if($show_id > 0){
$prefix = substr($new_code->cd, 0, 3); // get the prefix 3 chars at front
$number = substr($new_code->cd, 3); // get the remaining chars
$new = $number + 1; // add it with 1, php automatically convert strings that can be converted to numbers
$fix_code = $prefix . substr("00000" . $number, -5); // re-assemble the code
}else{
$fix_code = 'CO-00001';
}
you can split the id into two parts, the string one, and the digit one. then increment the digit part then re concat the two parts again
list($strPrefix, $digitPart) = split('-', $oldId);
$digitPart++;
$newCode = $strPrefix . '-' . $digitPart;
how to set if price row is comming soon than show update soon
how to set if price row is comming soon than show update soon
mysql
<?
//connect to database
mysql_connect('localhost','phone','pasword');
mysql_select_db('phone');
//////Displaying Data/////////////
$id=$_GET['id']; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error";
exit;
}
$result = mysql_query("SET NAMES utf8"); //the main trick
$q=mysql_query("select * from price where id=$id AND TRIM(model) IS NOT NULL");
//Adds one to the counter
mysql_query("UPDATE price SET counter = counter + 1 where id=$id ");
//Retreives the current count
$count = mysql_fetch_row(mysql_query("SELECT counter FROM price"));
$row=mysql_fetch_object($q);
echo mysql_error();
echo "<table class='hovertable'><tr><td><img src='admin/media/$row->photo' ></td></tr>";
echo "</table>";
?>
example
my rows
2000
3000
comming soon
4900
and this code is showing like this
2000
2000
3000
Warning: number_format() expects parameter 1 to be double, string given in
4900
and i want like this
2000
2000
3000
update soon
4900
how to solve this problem please help me to solve this issue
<?php if($row->price):?><tr class=\"style1\"><td width='200'><b>Price US $:</b></td><td>Price in Dollar:
<?php
function currencyExchange($amount,$baseCurrency,$quoteCurrency) {
$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$baseCurrency[0]$quoteCurrency[0]=X&f=sl1d1t1c1ohgv&e=.csv", "r");
$exchangeRate = fread($open, 2000);
fclose($open);
$exchangeRate = str_replace("\"", "", $exchangeRate);
$exchangeRate = explode(",", $exchangeRate);
$results = ($exchangeRate[1]*$amount);
$results = number_format ($results, 2);
$amount = number_format ($amount);
$timeStamp = strtotime($exchangeRate[2]);
$timeStamp = date('F d, Y', $timeStamp);
$timeStamp = "$timeStamp $exchangeRate[3]";
echo "$results\n";
}
// for additional currency ticker symbols visit: http://finance.yahoo.com/currency-converter
$usd = array('USD','US Dollars');
$INR = array('INR','Indian Rupees');
$usd = array('usd','US DOLLARS');
$b = str_replace( ',', '', $row->price );
if( is_numeric( $b ) ) {
$row->price = $b;
}
currencyExchange("$b",$INR,$usd);
?></td></tr><?php endif; ?>
Test if the amount is numeric before trying to use functions that only work for numbers.
Replace:
$results = ($exchangeRate[1]*$amount);
$results = number_format ($results, 2);
$amount = number_format ($amount);
with:
if (is_numeric($amount)) {
$results = ($exchangeRate[1]*$amount);
$results = number_format ($results, 2);
} else {
$results = 'update soon';
}
You don't need to call number_format() on $amount because you never print that value.
What about setting the column to something like 9999999999 ( which will get around numbers problem ) then testing the incoming for these numbers and if found then echo coming soon
Hi i have this line of code below
list($tag1, $tag2, $tag3, $tag4, $tag5) = array_pad(explode(',', $new_shout_tags, 5), 5, "");
It, basically assigns no value to the elements in the list if there is nothing else in the $new_shout_tags. So basically if i have
$new_shout_tags = "asd, asdf, asdfg";
then list will be
$tag1 = "asd";
$tag2 = "asdf";
$tag3 = "asdfg";
$tag4 = "";
$tag5 = "";
Which is all good however if the user enters
$new_shout_tags = "asd, asdf, asdfg, asdaf, asdafa, afads, asdasfd";
Which is 7 elemnts for example then the explode creates this in the list
$tag1 = "asd";
$tag2 = "asdf";
$tag3 = "asdfg";
$tag4 = "asdaf";
$tag5 = "asdafa, afads, asdasfd";
How do i make it not include the last 2 or however many over 5 that the user has entered, just the first 5 only.
Thanks
Use a garbage variable.
list($tag1, $tag2, $tag3, $tag4, $tag5, $garbage) =
array_pad(explode(',', $new_shout_tags, 6), 6, "");
this should work:
$v1=explode(',', $new_shout_tags); // with no limit
$v2=array_slice($v1,0,5);
$v3=array_pad($v2, 5, "");
list($tag1, $tag2, $tag3, $tag4, $tag5) = $v3
I'm generating auto incremental code through a PHP script. The sequence goes like this, L001, L002....L039, L040. Below is the PHP code I have written. The comments show the output of each statement.
<?php
require_once("db_handler.php");
$conn = iniCon();
$db = selectDB($conn);
$query = "SELECT LID FROM locations ORDER BY LID DESC LIMIT 1";
$result = mysql_query($query, $conn);
$row = mysql_fetch_array($result);
$last_id = $row['LID']; //L040
$id_letter = substr($last_id, 0, 1); //L
$id_num = substr($last_id, 1) + 1; //040 + 1
$new_id = $id_letter . $id_num;
mysql_close($conn);
?>
I'm displaying the $new_id in a HTML textbox. though everything works fine up to this point, the new generated code shows as L41, cutting off the leading zero.
How can I stop this from happening? I need to have that zero in the front.
Thank you.
Use either str_pad or sprintf function:
$id_num = str_pad($id_num, 3, "0", STR_PAD_LEFT);
or
$id_num = sprintf("%03d", $id_num);
$id_num = substr($last_id, 1) + 1; //40 + 1
$id_num = str_pad($id_num, 3, "0", STR_PAD_LEFT); //return 041
$new_id = $id_letter . $id_num;
take a look at sprintf[1]. hint: %03d
[1] http://br2.php.net/sprintf