I'm using a simple explode operation to use array values for inserting records to mysql database.
The code i use is:
// for loop above
$fieldsArr = explode(',', $field_names);
where $field_names is a string like :
'1_gps_update_coordinates','1' // prints out just fine
'1_meter_conf_holiday1_end','2099-01-01' // also fine
but
'1_electricity_unit_price','0,100' gives problem.
How can I overcome this? Any tip is appricated.
Ps $field_names values comes directly from database so I cannot really write an if statement.
if you can't change the input data, you can do this:
change the delimiter from comma to semicolon with str_replace function
$field_name = str_replace("','", "';'", $field_name)
then you can explode by semicolon
$fieldsArr = explode(';', $field_names);
You need to insert float using english number formatting
0.1
Instead of european 0,1
Use number_formatting() or simply str_replace(',', '.', $val);
Related
So in my database I have a field that holds values in this format "MSFT,AAPL,SNAP,MCD"
How can I setup these values in this format to PHP such as this
$symbols = ["MSFT","AAPL","SNAP","MCD"];
I know Im going to need to explode (comma delimited) values but cant put my finger on it.
After I would explode the values and I would then use count with a while loop to build the string.
In the end I would think it would look something like this
$new_string = '"MSFT","AAPL","SNAP","MCD"';
Then I think could just do this?
$symbols = [$new_string];
you could try this.
<?php
$str = "MSFT,AAPL,SNAP,MCD";
$symbols = (explode(",",$str));
var_dump($symbols); #displays the array values;
var_dump(count($symbols)); #array count
?>
I have a text box.
I am enter the value in text box like 12 13 14.
and i am want to convert this into 12,13,14 and then convert it into array and show each separate value.
If your form field asks for the values without a comma, then you will need to explode the POST data by space. What you're doing now is imploding it by comma (you can't implode a string to begin with), and then trying to pass that into a foreach loop. However, a foreach loop will only accept an array.
$ar = explode(' ',$da);
That simple change should fix it for you. You will want to get rid of the peculiar die() after your foreach (invalid syntax, and unclear what you're trying to do there!), and validate your data before the loop instead. By default, if you explode a string and no matching delimiters are found, the result will be an array with a single key, which you can pass into a loop without a problem.
Are you sure you want to expect the user enters data in that particular format? I mean, what if the user uses more than one space character, or separate the numbers actually with commas? or with semicolons? or enters letters instead of numbers? Anyway.. at least you could transform all the spaces to a single space character and then do the explode() as suggested:
$da = trim(preg_replace('/\s+/', ' ', $_POST['imp']));
$ar = explode(' ', $da);
before your foreach().
use explode instead of implode as
The explode() function breaks a string into an array.
The implode() function returns a string from the elements of an array.
and you cannot do foreach operation for a string.
$da=$_POST['imp'];
$ar = explode(' ',$da);
foreach($ar as $k)
{
$q="insert into pb_100_fp (draw_3_fp) values ('".mysqli_real_escape_string($conn, $k)."')";
$rs=mysqli_query($conn, $q);
echo $k.",";
}
then you will get this output
o/p : 12,13,14,
I have a comma separated string stored in a column of data type blob ,with values as given below.
date,time,A,B,C,D
11/31/2013,11:00,20,17,18,11
12/31/2013,14:00,18,16,18,14
10/31/2013,17:00,15,17,10,22
09/31/2013,19:00,19,17,20,17
I want the string parsed and the string containing date and time removed corresonding to its values finally i require it like this
A,B,C,D
20,17,18,11
18,16,18,14
15,17,10,22
19,17,20,17
I tried using
$exp = explode(',',$arr[0]);
$arrayOfReplacements = array(':' => '','/'=>'');
$clean = strtr($arr[0], $arrayOfReplacements);
print_r($clean);
it needs to remove the date,time and its values also.
what more needs to be done ?
You could use something like:
$exp = explode(',',$arr[0]);
array_shift($exp);
array_shift($exp);
$output = implode(',',$exp);
Assuming $arr[0] contains the comma separated values:
echo join(',', array_slice(explode(',', $arr[0]), 2));
See also: array_slice()
Alternatively, use substr() with strpos():
echo substr($arr[0], strpos($arr[0], ',', strpos($arr[0], ',') + 1) + 1);
The above assumes that the format of the blob column can be trusted to have at least two commas.
If the format of date and time columns is not going to change, it can be further simplified:
echo substr($arr[0], 17);
How'd I go about reading a tab delimited .txt file and then coding it so it puts each column of data(that is seperated with a tab) into a php variable.
for example....
505050 somedata moredata
and then, writing these variables to
$id
$somedata
$moredata
I read into fgetcsv and making an array although it seemed to make the whole line an array i need to split it into variables so that i can write it to individual columns in the MySQL database,
Can anyone give me any pointers?
Thanks so much...
A combination of fgetcsv() and list() will be the most efficient way to split this into named variables:
list($id, $somedata, $moredata) = fgetcsv($fp, 0, "\t");
However, you do not need to have them as named variables in order to insert them in MySQL, you can just use the raw array returned by fgetcsv():
$row = fgetcsv($fp, 0, "\t");
$query = "
INSERT INTO `tablename`
(`id`, `somedata`, `moredata`)
VALUES
('{$row[0]}', '{$row[1]}', '{$row[2]}')
";
Don't forget to escape the data before using it in a query ;-)
explode() will split a string. Try this:
list($id, $somedata, $moredata) = explode("\t", $string);
Where $string is the string you want to split. For more info about explode() and list(), just look up on php.net.
You can use explode():
http://php.net/manual/en/function.explode.php
Ok so i am importing a csv via a PHP script I am writing and the spreadsheet has values like $4090,00 and i need to insert these values in a mysql decimal field using PHP. Is there an easy way to get rid of the $ if its present in PHP
With str_replace, or even trim :
$str = str_replace('$', '', $str);
$str = trim($str, '$'); // only delete $ at start or end of the string
There are many solutions for this. Also, please note you have to use the period separator for decimals and integer part, not a comma.
$value = "$4090,00";
$value = preg_replace('/[\$,]/', '', $value);
Sure, you can use str_replace for this:
$nodollars = str_replace('$', '', $some_string);
Use sscanf if the input has a rigid format.