I am using php to enter data from CSV file in mysql table. One column of CSV has text data for which I cannot maintain any formatting, and so the data when displayed in the webpage does not show up in a nice way.
I am thinking now to have the data in MS Word table and use php to insert data in mysql table from the Word table. Once again, the only advantage for me is to maintain the formatting of the data of one column.
How can I enter data from Word table in mysql table?
So according to the comments you need to format the text in the Description column using HTML.
This is not that easy task and can be very complex depending on the exact features of the formatting you need.
For example, to make new lines appear as new lines in HTML page, you will need to replace new line characters with <br /> tags:
$description = str_replace("\n", "<br />", $description);
If you need some more complex formatting like bulleted lists or italic/bold tags you will need to implement more complex parsing and formatting code.
For bulleted lists you will need to split the content into lines ($arr = explode("\n", $description)), find the lines starting with a character representing the bullet (* for example, depends on your column format) and replace them with <li> tags. Besides that you will need an <ul> and </ul> tags marking the start and the end of the list.
Similar approach will be needed for every formatting you need to keep in HTML.
Related
Complete novice with a very simple text-based webpage. Not sure whether to use PHP, JQuery, Javascript, or what, but I just want to pull text from a specified range of cells of a CSV file (located on server) to be used as HTML code; for example, this is the content of cells N2-N3:
<p><sup>1</sup> In the beginning<span class="CNC"></span> <span class="I"></span> created the heavens and the earth.</p>
<p><sup>2</sup> And the earth was <i class="alt" data-text='["without form","formless"]'></i> and <i class="alt" data-text='["void","empty"]'></i>.</p>
CSV details: 34 columns. 31103 rows (first is field names). Separator is %.
Live example here: http://jsfiddle.net/zZpJy/20/
Thanks in advance for your help!
I would ACTUALLY drop the csv into a database - this is VERY easy as you have it in CSV format with column names in the first row (using PHP my admin - then import.)
Then you can just query the database.
Otherwise (lets say you want the data in row 30504 - you have to read the whole file to row 30504 to get the data.
I have a feed that comes from the State of Florida in a CSV that I need to load daily into MySQL. It is a listing of all homes for sale in my area. One field has a list of codes, separated by commas. Here's one such sample:
C02,C11,U01,U02,D02,D32,D45,D67
These codes all mean something (pool, fenced in area, etc) and I have the meanings in a separate table. My question is, how should I handle loading these? Should I put them in their own field as they are in the CSV? Should I create a separate table that holds them?
If I do leave them as they are in a field (called feature_codes), how could I get the descriptions out of a table that has the descriptions? That table is simply feature_code, feature_code_description. I don't know how to break them apart in my first query to do the join to bring the description in.
Thank you
As a general rule, csv data should never stored in a field, especially if you actually need to consider individual bits of the csv data, instead of just the csv string as a whole.
You SHOULD normalize the design and split each of those sub "fields" into their own table.
That being said, MySQL does have find_in_set() which allows you sort-of search those csv strings and treat each as its own distinct datum. It's not particularly efficient to use this, but it does put a bandaid on the design.
You should keep the information about feature codes in a separate table, where each row is a pair of house identifier, and feature identifier
HouseID FeatureID
1 C07
1 D67
2 D02
You can use explode() to separate your CSV string : http://php.net/manual/en/function.explode.php
$string = 'C02,C11,U01,U02,D02,D32,D45,D67';
$array = explode(',', $string);
Then with your list of feature_codes you can easily retrieve your feature_code_description but you need to do another query to get an array with all your feature_codes and feature_code_description.
Or split your field and put it in another table with the home_id.
You can save it in your DB as is and when you read it out you can run the php function explode. Go check that function out. It will build an array for you out of a string separating the values by whatever you want . In your case you can use:
$array_of_codes = explode(",", $db_return_string);
This will make an array out of each code separating them by the commas between them. Good luck.
I have a tab delimited text file with the first row being label headings that are also tab delimited, for example:
Name ID Money
Tom 239482 $2093984
Barry 293984 $92938
The only problem is that there are 30 some columns instead of 3 so I'd rather not have to type out all the (name VARCHAR(50),...) if it's avoidable.
How would I go about writing a function that creates the table from scratch in php from the text file, and say the function takes in $file_path and $table_name? Do I have to write all the column names again telling mysql what type they are and chop off the top or is there a more elegant solution when the names are already there?
You would somehow need to map the column type to the columns in your file. You could do this by adding that data to your textfile. For instance
Name|varchar(32) ID|int(8) Money|int(10)
Tom 239482 $2093984
Barry 293984 $92938
or something similar. Then write a function thet get's the column name and columntype using the first line and the data to fill the table with using all the other rows. You might also want to add a way to name the given table etc. However, this would probably be as much work (if not more) than creating SQL queries using you text file. Add a create table statement at the top and insert statements for each line. With search and replace this could be done very fast.
Even if you could find a way to do this, how would you determine the column type? I guess there would be some way to determine the type of the columns through checking for certain attributes (int, string, etc). And then you'd need to handle weird columns like Money, which might be seen as a string because of the dollar sign, but should almost certainly be stored as an integer.
Unless you plan on using this function quite a bit, I wouldn't bother spending time cobbling it together. Just fat finger the table creation. (Ctrl-C, Ctrl-V is your friend)
I have a simple PHP script that loops over data in a CSV file, and adds the records to the database accordingly. One of my fields is a description field, but that description field itself has a comma (or multiple comma's) in it. It seems as though data for that field is only read up until the comma, however the next field is correct, so it is not as though the field after that is the remainder of the description, is is using the next column which is right.
Am I supposed to escape the comma? I am adding this data to a MySQL database, could that be where the issue is being caused?
My SQL query could be something like:
$description = $data[7]; //description column eg: "hello, my name is xxxxx, I am old"
INSERT INTO tblsomething (id, description) VALUES ($id, '$description');
The above statement only inserts the description as "hello" and nothing after the first comma it encounters.
Any ideas why this is?
Many thanks,
Simon
EDIT: This is solved, apologies to all as it was a silly mistake. It appears that the person who did the front end was creating arrays of content using the patter ',' to split the content. IT seems that the description - although supposed to be one array entry - was being split into multiple entries due to it containing comma's. This will be solved by using a more rare character like the pipe symbol to create our separators.
Thanks to all
Because it's not a CSV file. Fields in a CSV file that contain commas are supposed to be delimited by double quotes; this way the CSV functions in PHP will handle them properly.
I have a large file that I would like to read via php, and then insert various fields into MySQL.
Each file in the feed is in plain text format, separated into columns and rows. Each record has the same set of fields. The following are the delimiters for each field and record:
Field Separator (FS): SOH (ASCII character 1)
Record Separator (RS) : STX (ASCII character 2) + "\n"
If I look at the first few lines of the file they look like this:
#export_dateapplication_idlanguage_codetitledescriptionrelease_notescompany_urlsupport_urlscreenshot_url_1screenshot_url_2screenshot_url_3screenshot_url_4screenshot_width_height_1screenshot_width_height_2screenshot_width_height_3screenshot_width_height_4
#primaryKey:application_idlanguage_code
#dbTypes:BIGINTINTEGERVARCHAR(20)VARCHAR(1000)LONGTEXTLONGTEXTVARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(1000)VARCHAR(20)VARCHAR(20)VARCHAR(20)VARCHAR(20)
#exportMode:FULL
I am struggling to no where to start in order to read this file into PHP, can anyone help with the basic PHP to read each record, and assign a variable to each field, which I then will be able to write into MySQL. I can handle the writing into SQL once I have the various fields set up.
Thanks in advance,
Greg
files greator than 2gb cant be read in PHP (32 bit limit).
For lower size use simple fopen function
And inserting mysql is all the work of macthing patterns and inserts.
If structure of table is same every row then better make it manual once and then just execute inserts by extracting values either by regex or other functions like explode and split .
If every line has delimiters between each field, you may look at fgetcsv().
When you use fgetcsv() on a line, it will return an array with the contents from that line. Since you have several lines, put the funciton inside a while()-loop (look at example #1)