mysql data insertion line by line - php

asking for insert multiple lines in one mysql column (with something like line break like show didn't mean inserting data in multiple rows have multiple variables contain different urls like
$var_one = "http://example.com";
$var_two = "http://example.org";
$var_two = "http://example.net";
want to store these values in one mysql column ever value should be in new line refer image describe better
asking for insert multiple lines in one mysql column (with something like line break like didn't mean inserting data in multiple rows

If you trying in php, then you can serialize the data and store as single column in DB.
While retriving data, we can unserialize and get the data as array. Find below example.
<?php
$var = array();
$var[0] = "http://example.com";
$var[1] = "http://example.org";
$var = array("1","2","3");
print_r($var); // Array ( [0] => http://example.com [1] => http://example.org )
$b=serialize($var);
echo $b; // a:2:{i:0;s:18:"http://example.com";i:1;s:18:"http://example.org";}
$c=unserialize($b);
print_r($c); // Array ( [0] => http://example.com [1] => http://example.org )

Related

How to insert data in SQLite table using php?

I am working on a php code as shown below which scans the directory ($src_dir) and list all mp4 files.
$src_dir = ('\\\ABCD-ST-001\Audio_Test\podcast\incoming_folder');
$mp4_files = preg_grep('~\.(mp4)$~', scandir($src_dir));
print_r(array_values($mp4_files)); // LineA
Here is the O/P obtained from Line#A:
Array ( [0] => 36031P.mp4 [1] => hello.mp4 )
I am getting two values at Index 0 and Index 1. The 1st is 36031P.mp4 and 2nd is hello.mp4
After that I have wrote a script which insert data in table Podcast_Export.
$db->exec("INSERT INTO Podcast_Export ('House#', 'Status') VALUES ('array_values($mp4_files)', 'Go')"); /* have to place this question on SO*/
On running the above script, I am getting the following data inside the table which is not I want.
array_values(Array) Go
array_values(Array) Go
Problem Statement:
I am wondering what changes I should make in the script above so that it inserts the following data inside the table:
36031 Go
hello Go
You are inserting a string value 'array_values($mp4_files)' , you just need to remove quotes around this value. something like:
$db->exec("INSERT INTO Podcast_Export ('House#', 'Status') VALUES (array_values($mp4_files), 'Go')");
But, if you delete the quotes around array_values then it will insert following array as a string Array ( [0] => 36031P.mp4 [1] => hello.mp4 )
so, its better to use loop here with $mp4_files array and insert your data.
Side Note:
I suggest you to use different name for House# column something like house_no.

PHP -- Convert a csv file into a named multidimensional array

I am a bit new to PHP, and I am having some trouble converting a csv file into a multidimensional array, where each dimension has a name. I need to two distinct named array so that I can use them in a preg_replace statement like the below:
$expand_abbrev=preg_replace($contracted_form , $expanded_form , $sentence);
the file content are as follows:
contracted_form,expanded_form
'/a./','in dates ante'
'/abbrev./','abbreviation of'
'/Abbrev./','abbreviations'
'/Abd./','Aberdeen'
'/Aberd./','Aberdeen'
'/Aberdeensh./','Aberdeenshire'
This is so far what I have come up with, but it does not achieve the desired output.
$abbrev_list=file_get_contents('files/abbreviations.text');
$test=str_getcsv($abbrev_list, ",");
$expand_abbrev=preg_replace($contracted_form, $expanded_form, $sentence);
Can anyone help me out with this please ? I have been trying so many times but so far no success.
---- Please allow me to make a clarification, because it seems I might have mislead you. I would like to process a csv file which has two two values per line contracted_form and expanded_form. These two values per line are separated by a comma.
I am not sure how best to approach it, I was thinking perhaps splitting the each line into two arrays like for example each contracted_form is stored in the contracted_from array and each expanded_form is stored in the expanded_form array.
So that preg_replace can replace any instance of contracted_from, encountered in a sentence with its corresponding expanded_form. So for example the following sentence:
Hi sir, I live in a flat in Abd.
So preg_replace(Adb. , Aberdeen, Hi sir, I live in a flat in Abd.) would result in the below.
Hi sir, I live in a flat in Aberdeen.
So from what I understood you want to put each line of the csv into a multi dimensional array, if I understood you wrong please correct me.
Also bare in mind the algorithm below does not take into csv headers into account.
// The Multidimensional Array
$array = array();
$handle = fopen("file.csv", "r");
// If csv if empty, this will not run
while((($data = fgetcsv($handle, null, ',')) !== false)) {
// The "magic" happen here.
temp = array();
$temp["contrated_form"] = $data[0];
$temp["expanded_form"] = $data[1];
array_push($array, $temp);
}
For example if your csv file had the following:
data1,data2
data3,data4
data5,data6
The result of the above algorithm would be:
Array (
[0] => Array (
[contrated_form] => data1
[expanded_form] => data2
)
[1] => Array (
[contrated_form] => data3
[expanded_form] => data4
)
[2] => Array (
[contrated_form] => data5
[expanded_form] => data6
)
)
Tips:
str_getcsv does not know that the first row of your csv file contains your column names.
str_getcsv does not need "," in the statement str_getcsv($abbrev_list, ",") because , is default separator.
Statement $expand_abbrev=... has no idea what you're arguments mean.
You must write a foreach to create array $contracted_form using $test.
You must write a foreach to create array $expanded_form using $test.

How to assign array to array variable which is store in a database

I store array structure in database table.
example
table name - example
id=1
data= array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43")
I want to get that array structure from table and assign data column to an array.
Example
while($row=mysqli_fetch_array($result))
{
$table=$row['data'];
}
I did this way.. but it's not working.
It results in:
$table[0]=>array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43")
You can save array data to database field in multiple ways.
I suggest two ways:
1) Serialized array:
you can save data using serialize() function.
e.g. $arr = array('234' => 'asfdads', 'asdf' => 'asdf2');
$toDb = addslashes(serialize($arr));
And then you can unserialize() them to get it back like:
$toDb = unserialize(stripslashes($fromDb));
2) Using json_encode() and json_decode();
e.g. $arr = array('234' => 'asfdads', 'asdf' => 'asdf2');
$toDb = json_encode($arr);
And then you can json_decode() them to get it back like:
$toDb = json_decode($fromDb)

Php array_rand() printing variable name

I have an array that is filled with different sayings and am trying to output a random one of the sayings. My program prints out the random saying, but sometimes it prints out the variable name that is assigned to the saying instead of the actual saying and I am not sure why.
$foo=Array('saying1', 'saying2', 'saying3');
$foo['saying1'] = "Hello.";
$foo['saying2'] = "World.";
$foo['saying3'] = "Goodbye.";
echo $foo[array_rand($foo)];
So for example it will print World as it should, but other times it will print saying2. Not sure what I am doing wrong.
Drop the values at the start. Change the first line to just:
$foo = array();
What you did was put values 'saying1' and such in the array. You don't want those values in there. You can also drop the index values with:
$foo[] = 'Hello.';
$foo[] = 'World.';
That simplifies your work.
You declared your array in the wrong way on the first line.
If you want to use your array as an associative Array:
$foo=Array('saying1' => array (), 'saying2' => array(), 'saying3' => array());
Or you can go for the not associative style given by Kainaw.
Edit: Calling this on the not associative array:
echo("<pre>"); print_r($foo); echo("</pre>");
Has as output:
Array
(
[0] => saying1
[1] => saying2
[2] => saying3
[saying1] => Hello.
[saying2] => World.
[saying3] => Goodbye.
)
Building on what #Answers_Seeker has said, to get your code to work the way you expect it, you'd have to re-declare and initialise your array using one of the methods below:
$foo=array('saying1'=>'Hello.', 'saying2'=>'World.', 'saying3'=>'Goodbye.');
OR this:
$foo=array();
$foo['saying1'] = "Hello.";
$foo['saying2'] = "World.";
$foo['saying3'] = "Goodbye.";
Then, to print the contents randomly:
echo $foo[array_rand($foo)];

Storing difficult data structure in php arrays

I need to store this data structure in php array:
Movie has id, name and show_date. Each of show_dates have show_times. I need to dynamically in cilcle fill this array with data from my data source. When i do like this:
$Movie = array();
$Movie[0]['id']=10;
$Movie[0]['name']='Some Name';
$Movie[0]['date'][0]='12.12.12';
$Movie[0]['date'][0]['time'][0]='12:23:00'; //there it throws error
$Movie[0]['date'][0]['time'][1]='15:23:00';
Could you help me with this issuse ?
You're trying to do array access on a string.
Change to:
$Movie[0]['date'] = array();
$Movie[0]['date'][] = array( // shorthand push notation
"date" => "12.12.12",
"times" => array("12:23:00", "15:23:00")
);
// .. etc

Categories