PHP: Formatting irregular CSV files into HTML tables - php

My client receives a set of CSV text files periodically, where the elements in each row follow a consistent order and format, but the commas that separate them are inconsistent. Sometimes one comma will separate two elements and other times it will be two or four commas, etc ...
The PHP application I am writing attempts to do the following things:
PSEUDO-CODE:
1. Upload csv.txt file from client's local directory.
2. Create new HTML table.
3. Insert the first three fields FROM csv.txt into HTML table row.
4. Iterate STEP 2 while the FIRST field equals the First field below it.
5. If they do not equal, CLOSE HTML table.
6. Check to see if FIRST field is NOT NULL, IF TRUE, GOTO step 2, Else close HTML table.
I have no trouble with steps 1 and 2. Step 3 is where it gets tricky since the fields in the csv.txt files are not always separated by the same number of commas. They are, however, always in the same relative order and format. I am also having issues with step 4. I don't know how to check if the beginning field in a row matches the beginning field in the row below it. Steps 5 should be relatively simple. For step 6, I need to find an eqivalent of a "GOTO" function in PHP.
Please let me know if any part of the question is unclear. I appreciate your help.
Thank you in advance!

If you want to group the rows by their first element you can try something like:
read the next row via fgetcsv()
filter empty elements (a,,b,c -> a,b,c)
if the row contains fields <-> is not empty append the row to "its" group
That's not exactly what you've described but it may be what you want ;-)
<?php
$fp = fopen('test.csv', 'rb') or die('!fopen');
$groups = array();
while(!feof($fp)) {
$row = array_filter(fgetcsv($fp));
if ( !empty($row) ) {
// # because I don't care whether the array exists or not
#$groups[$row[0]][] = $row;
}
}
foreach( $groups as $g ) {
echo '
<table>';
foreach( $g as $row ) {
echo '
<tr>
<td>', join('</td><td>', array_map('htmlentities', $row)), '</td>
</tr>
';
}
echo '</table>';
}

why not simply start by going through any replacing any multiples of commas with a single comma. eg:
abc,def,,ghi,,,,jkl
becomes:
abc,def,ghi,jkl
and then just continue normally.

If you mean that there are different numbers of commas on each line, then as far as I can see it is actually impossible to do what you want to do by looking at the commas alone. For example:
ab,c,d,ef // could group columns a-f in that way, but
a,bc,de,f // could also group columns a-f
... and you would have no way of knowing which was the proper arrangement, unless you're given some other instructions or the type of data is identifiable by regular expression as someone else said.
If on the other hand you just mean that sometimes there are blanks, but there are still the same number of columns, like this:
a,b,,d,e,f
a,,c,d,e,f
... then you can still form the table correctly. I would recommend using explode(',' $line) in that case and then doing your processing on the elements of the exploded array without worrying about what is inside them.

Related

How to update particular images among the images separated by comma in db

I have a column which contains the image path.It can hold many images separated by comma.Its working well and fine till inserting.
The problem is when i try to update only particular image from all the images(i mean among images separated by comma) of row all inserted images of that particular row are getting updated by single image.
How can i fix this?Please help me.
Thanks in advance.
It is bad design to have comma separated data inside a column. But, if you really want to do this extract the string. Check the code below.
// $text is what you use.
// Break apart at commas
$pieces = explode(",", $text);
// Use reference to be able to modify the pieces
foreach ($pieces as &$piece)
{
$piece = wordwrap($piece, 80);
// Modify the correct field.
}
// Join the pieces together back into one line.
$wrapped_lines = join(',', $pieces);
echo $wrapped_lines;
// You can do the database update operation here.
Let me know if this works..

PHP Extract Single String from List of Values

I need some assistance with php. I have been trying several things for the past several days including str_replace to no avail.
I have a field that may contain from 1 to 20 values, all listed on their own line, there is no html code in that field to separate them and some of the values may have their own spaces in between, so separating by space doesnt work.
What I need is to extract every single string of each line and convert it to code.
<p>For example, my field with values looks like this: </p>
<p><b>lang_fld </b><br>
------
<br>
English<br>
Spanish<br>
German<br>
French</p>
What I have in mind is to extract each line, ex. "English" from that string, and create a line of code like
<img src="images/flags/english.png> English
Basically I want to add the flag graphic to the word
I already tried
echo str_replace('English','<img src="images/flags/english.png>,lang_fld)<br>
...and so on
what I get after going through every single possible value is a bunch error
messages (different every time since I keep making changes - by guessing)
Can someone offer an easier option to do this? Not all 20 values will be in
that areatext field, some may contain just one language, some ten, some all 20,
etc.
Thank you!
Assuming you have all the values on a separate line, you can do explode() on the string to convert it to array of separate items, then loop through the array with foreach and perform any modifications with the single item. After you are done with the items and you would like to get them back together, you can use implode() to combine them into a single string.
A short sample ( you can of course use a for loop, I just prefer foreach here as it shows you better what is happening with the data ):
$text =
"French language
Italian language
English language";
$items = explode( "\n", $text ); // Split by newline ( you can also use "<br>" as separator )
$result = array(); // Modified data will be placed here
foreach ( $items as $item )
{
// Do something with $item
$item = "<img src=\"images/flags/$item.png\"> $item";
$result[] = $item;
}
// Merge them back together
$text = implode("\n", $result);

Alter Magento Index Fulltext Search?

I have a unique task that I have been given, and I am in the last leg of it, but this sub-task is proving to be extremely difficult! So you have background: We run a Magento site, and use a custom built SOLR search page. I am using phpSolrClient to parse the Solr XML and return usable result which I then build the search results page from.
The task I have been given is to have an "attribute" in the back end of Magento, lets call that "search_tags". The goal is to be able to insert a tag, and it's weight delimitered by commas:
ie sight^2,hearing^1,smell^3
I would like to edit the code in Magento's fulltext reindex to break apart the string, and insert that tag X of times into the fulltext1_en field. So it would add "sight" twice, "hearing" once, and "smell" three times. This is going to allow us to say, put a blender on the page when someone searches for juicers, even though the term "juicer" or "juice" does not appear in the fulltext1_en string. I have developed the code to pull, split and iterate ... However I am at an stand-still since I don't know what code to edit to include this in my fulltext1_en during the reindex process. If anyone has any experience with editing Magento's Fulltext Reindex, your input would be greatly appreciated! I looked in Indexer.php, but everything in that file is ambiguous at best, so that was no help! Gotta love Magento!
OK for those looking to alter and give "weighted tags" to custom search in Magento using SOLR, I was up all night getting this right, but it works...
First, create a filter in Magento and apply it to all products. I named mine "search_tags".
Next, use the following formula in that filter for a test item:
dude^25,crazyman^25,wierdsearch^25
Each word followed by a carat, and then the weight you want to give it. (This is how many times the word will be repeated and then added to fulltext1_en.)
After that is done, open the following file:
/app/code/core/Mage/CatalogSearch/Model/Mysql4/Fulltext.php
I know it says MySQL4, pay no attention, SOLR uses this index.
About line 500, you will see the following block:
if ($selects) {
$select = '('.join(')UNION(', $selects).')';
$query = $this->_getWriteAdapter()->query($select);
while ($row = $query->fetch()) {
JUST BELOW this block insert the following:
NOTE: Do not use the attribute ID I have listed here, that is unique to my setup. You are going to have to search your database to find this ID. I used JOIN to join eav_attributes with catalog_product_entity_varchar and used SELECT to find attribut_id and value WHERE entity_id = (Insert your product ID here). It's a pain, but it's the only way. This will return all the attributes for that product. Look for the one that has the tags we entered in earlier, and get it's ID. Insert that into the code below.
$attr_val = $row['value']; // Set attr_val so that it can be manipulated in following IF
if ($row['attribute_id'] == 457) { // 457 is the ID of MY search_tags filter, yours WILL be different! It can be found by joining eav_attributes table and catalog_product_entity_varchar and searching for the attribute value and ID where entity_id is X
$input = $row['value']; // Set $input to value of filter
$attr_val = ""; // Create Emtpy string
$pieces = explode( ',', $input ); // Explode filter by comma
foreach ($pieces as $val){
$i=1;
$val = explode( '^', $val); // Explode each "tag" by carat
while ($i <= $val[1]) { // Loop while $i is less than or equal to the number on the right side of the carat
$i++;
$attr_val = $attr_val . " " . $val[0]; // Append $attr_val with the word to the right side of the carat
}
}
}
$result[$row['entity_id']][$row['attribute_id']] = $attr_val; // Modified from Original
After you insert that ... Then comment out the following block.
$result[$row['entity_id']][$row['attribute_id']] = $row['value']; // ORIGINAL BLOCK -- UNCOMMENT -- DO NOT DELETE
Now run a fulltext reindex, and your fulltext1_en should show that you've added "dude", "crazyman", and "weirdsearch" all 25 times! When the index is completed, search for any of the tags in your site search: That item you added the tags to should show up close to, if not the top. Enjoy!

Generate x number of html elements based on PHP/MySQL

For every comma inside a certain MYSQL row,
I want to create x html elements, where x = number of commas inside a row.
So for example, if my mysql row looks like this:
dance, singing, drawing, acting,
That is 4 commas.
I want to create a html box (using a div) for every comma, and inside that box, show the hobby..
So like on the main.php page, show :
[ dance ]
[ singing ]
[ drawing ]
[ acting ]
How does one go about creating a certain number of html elements based on a certain number of commas in a mysql table, assuming that I will add more hobbies to the table?
BTW, I am using php as the server side languange.
Any help would be appreciated, thank you.
This is untested, so consider it psuedo-code. You may need to escape the square brackets. The other issue you could encounter is that there could be an empty div output at the very end. In that case, just check that $elem != ''.
$elements = explode(',', $mysql_data_column);
foreach($elements as $elem)
{
echo "<div style=\"block\">[$elem]</div>";
}

Parsing s-expressions with PHP

Well, I need to parse 2 textfiles. 1 named Item.txt and one named Message.txt They are configuration files for a game server, Item contains a line for each item in the game, and Message has Item names, descriptions, server messages etc. I know this is far less than ideal, but I can't change the way this works, or the format.
The idea is in Item.txt I have lines in this format
(item (name 597) (Index 397) (Image "item030") (desc 162) (class general etc) (code 4 9 0 0) (country 0 1 2) (plural 1) (buy 0) (sell 4) )
If I have the php variable $item which is equal to 397 (Index), I need to first get the 'name' (597).
Then I need to open Message.txt and find this line
( itemname 597 "Blue Box")
Then return "Blue Box" to PHP as a variable.
What I'm trying to do is return the item's name with the item's Index.
I know this is probably something really basic, but I've searched though dozens of file operation tutorials and still can't seem to find what I need.
Thanks
Following method doesn't actually 'parse' the files, but it should work for your specific problem...
(Note: not tested)
Given:
$item = 397;
open Item.txt:
$lines = file('Item.txt');
search index $item and get $name:
$name = '';
foreach($lines as $line){ // iterate lines
if(strpos($line, '(Index '.$item.')')!==false){
// Index found
if(preg_match('#\(name ([^\)]+)\)#i', $line, $match)){
// name found
$name = $match[1];
}
break;
}
}
if(empty($name)) die('item not found');
open Message.txt:
$lines = file('Message.txt');
search $name and get $msg:
$msg = '';
foreach($lines as $line){ // iterate lines
if(strpos($line, 'itemname '.$name.' "')!==false){
// name found
if(preg_match('#"([^"]+)"#', $line, $match)){
// msg found
$msg = $match[1];
}
break;
}
}
$msg should now contain Blue Box:
echo $msg;
Not sure if your problem is with parsing the expressions, or reading files per se since you mention "file operation tutorials".
Those parenthetical expressions in your files are called s-expressions. You may want to google for an s-expression parser and adapt it to php.
You should look into the serialize function, which allows data to be stored to a textfile in a format that PHP can reinterpret easily when it needs to be reloaded.
Serializing this data as an array and saving it down to the textfiles would allow you to access it by array keys. Let's take your example. As an array, the data you described would look something like this:
$items[397]['name'] = 'bluebox';
Serializing the item array would put it in a format that could be saved and later accessed.
$data = serialize($items);
//then save data down to the text files using fopen or your favorite class
You could then load the file and unserialize it's contents to end up with the same array. The serialize and unserialize functions are directly intended for this application.
the first text file has several features that you can use to help parse it. It is up to you to decide if it is well formed and reliable enough to key on.
I noticed:
1) a record is delimited by a single line break
2) the record is further delimted by a set of parens ()
3) the record is typed using a word (e.g. item)
4) each field is delimited by parens
5) each field is named and the name is the first 'word'
6) anything after the first word is data, delimited by spaces
7) data with double quotes are string literals, everything else is a number
A method:
read to the end of line char and store that
strip the opening and closing parens
strip all closing )
split at ( and store in temp array (see: http://www.php.net/manual/en/function.explode.php)
element 0 is the type (e.g. item)
for elements 1-n, split at space and store in temp array.
element 0 in this new array will be the key name, the rest is data
once you have all the data compartmentalized, you can then store it in an associative array or database. The exact structure of the array is difficult for me to envision without actually getting into it.

Categories