How can I write data to an excel table from textarea? - php

I am new in coding and sufferin from some basic issues. I searched for a topic to find the answer that I need but I could not find. I've been trying to write and searching codes for writting into xls file for 3 days but could not find a suitable one. The problem is:
I created a page with textareas and submit/reset button like that and I've got an excel table in document classgrades.xls. The colums of that table are named Name, Midterm1, Midterm2, Final, Attendance and Average.
Now, I am trying to write a code which automatically adds a new row and writes user inputs from textares into the excel table. If you help I appreciate very much and if you write the code briefly I can understand better.

The class I use to modify Excel documents is PHPExcel. Was at http://phpexcel.codeplex.com/ but I just googled and it's moved to GitHib https://github.com/PHPOffice/PHPExcel.
Small warning: I have found it occasionnally buggy (turn off "NOTICE" and "WARNING" errors before diving in, for example - or at least you did when I last used it) but otherwise it's easy to use.
It has some nice classes to navigate the workbooks and add rows. No point quoting examples here as there are lots of examples in the "test" section. https://github.com/PHPOffice/PHPExcel/tree/develop/Tests Use those and piece together what you need.

Related

How to parse CSV file in PHP and store fields in a database?

I need help parsing the following a CSV file in PHP, so I can insert the contents into a database.
I know I use file_get_contents() but after that I feel a bit lost.
What I'd like to store.
Collection1 - events.text & date
Collection2 - position & name.text & total
I'm not sure how best structure the data to insert into a database table.
"**collection1**"
"events.href","**events.text**","**date**","index","url"
"tur.com/events/classic.html","John Deere Classic","Thursday Jul 9
- Sunday Jul 12, 2015","1","tur.com/r.html"
"collection2"
"**position**","name.href","**name.text**","**total**","index","url"
"--","javascript:void(0);","Scott","--","2","tur.com/r.html"
"--","javascript:void(0);","Billy","--","3","tur.com/r.html"
"--","javascript:void(0);","Jon","--","4","tur.com/r.html"
"--","javascript:void(0);","Bill","--","5","tur.com/r.html"
"--","javascript:void(0);","Tim","--","6","tur.com/r.html"
"--","javascript:void(0);","Carlos","--","7","tur.com/r.html"
"--","javascript:void(0);","Robert","--","8","tur.com/r.html"
"--","javascript:void(0);","Rod","--","9","tur.com/r.html"
As per your previous question, I think this needs to be broken down into sections. As it stands it is rather too broad to answer.
Read the information using file_get_contents(). Make sure this works first, by echoing it to the console. (It sounded from your other question that you felt this would not work if the URL does not have a .csv suffix. It should work regardless of the file extension - try it. If it fails it may be dependent on cookies or JavaScript or some other problem).
Design and create your table structure in MySQL. It seems like you have two tables. They should both have a primary key. Are they related in some fashion? If so, perhaps one has a foreign key to the other one?
Explode your text file on the new line character and loop across the resulting array of lines.
If your CSV data has a title row in the first row position, delete that from your array.
For each line, read the elements of interest using PHP's build-in CSV parsing functions, and store them in variables.
Pass these variables to a custom function that saves the data.
For each save, you'll need to do an INSERT. I recommend using PDO here. Make sure you bind your parameters.
Where you get stuck on a specific problem, you can ask a new and focussed question. At present, the task is to break things down into discrete and researchable pieces.
One trick worth remembering is this shortcut to the PHP manual. If you do not know how fgetcsv works, for example, type php.net/fgetcsv into your browser address bar, and the PHP site will find the function for you. The documentation is excellent.

Google Maps filters

I am very frustrated with my one old month problem. This is my first web page and it's quite complicated (for me).
My problem is with AND and OR in filtering categories in google maps. All tutorials go in the direction of the OR (golf OR theather). What about Golf and London? The more boxes checked, the less markers on the map.
I have two questions:
(1)
I am trying to follow this tutorial. I guess this is a trivial question: where is the data that is displayed in the panel? (I have followed several other tutorials and never had problems with finding the data, but this one...). I believe this tutorial may solve my second question below.
(2) The goal is to have a real estate page where user-sellers can insert properties for sale and user-buyers can view it, filtering the the data by multiple checkbox and/or drop down menus.
The problem I have is that I can't find a way to coordinate the OR and AND.
OR: I could use the Mike's tutorial and I have studied it very carefully.
I didn't find any tutorial when the filters get more complex.
(2B) I tried to work with my old post, and it works with OR as well. but couldn't find the right code when user only checks one (or two) box (like anything in Berkeley). The code(based on the kind Jobsen's code) required user to fill out all checkboxes all the way to the end, then the marker would show up. Couldn't check only 2 boxes and leave others unchecked. Sometimes I want the first filter (City) to be filtered a second time (Bedrooms). Not add any Berkeley + Bedrooms (2 or 3 or 4). Or only Berkeley. Or Berkeley +zip code + type + bedroom OR any combination. The and / or were not working as expected.
Any help with any of the above will be greatly appreciated. Feel free to add any info in the old post as well.Thank you again for your time.
Answer for the question (1) above, pick one:
(a) CSV file; then take a look at this.
(b) stackoverflow question and answer;
(C) you tube;
i chose CSV format. Here is a screenshot to turn csv format into a table using phpmyadmin.
To do the static tutorial which requires only CSV file and not Json (no need to mess with phpmyadmin): download the csv document, but don't open and save it as csv from MS Excel. Saved it inside the notepad as csv file. No idea about google docs.
Any help with the other part of my question (how to filter database with Jquery or Javascript) will be greatly appreciated.

How can I sort multiple tables i export from an sql database to excel by date?

I have searched for help with this extensively but haven't been able to figure it out. This is my first attempt at asking a question here so please bare with.
I have a project called www.farmathand.com where farmers keep track of their field activities through multiple pre-defined forms and a few dynamic forms. These forms then have an associated date with them to keep a record of when something was done. Here's a screen shot
Farmers have the ability to export all their entered information into an excel file. The problem I am having is figuring out a solution so sort the activities by date. Currently I have simple export code that looks and print data from each table (i.e. each type of activity) then moves on the to the next able. The fields in each table differ as you can see in the image.
Here is a screen shot of an excel file for the above example:
What i would like to be able to do is sort the activities by the date. So in the example in the above image. Line 7 would be at the top or like one should be line 6.
I was wondering if anyone had any suggesting or could guide me to the appropriate reference. The data base is a mysql database. I'm running php scripts currently.
If I understand your question correctly then what you want is to include an ORDER BY clause in your select query to sort the data. More information here:
http://dev.mysql.com/doc//refman/5.0/en/sorting-rows.html

Store data using a txt file

This question might seem strange but I have been searching for an answer for a long time and I couldn't find any.
Let's suppose you have a blog and this blog has many post entries just like any other blog. Now each post can have simple user comments. No like buttons or any other resource that would require data management. Now the query is: Can I store user comments on a single text file? Each post will be associated to a text file that holds the comments. So, if I have n posts I'll have n text files.
I know I can perfectly do this, but I have never seen it anywhere else and no one is talking about it. For me this seems better than storing all coments from all posts in a single mysql table but I don't know what makes it so bad that no one has implemented it yet.
Storing comments in text files associated with corresponding post? Lest see if it's good idea.
Okay adding new comments easy - write new text to the file. But what about format of your data? CSV? Ok then you would have to parse it before rendering.
Paging. If you have a lot of comments you may consider creating paging navigation for it. It can be done easily, sure. But you would need to open the file and read all the records to extract say 20.
Approve your comments. Someone posted new comment. You place it with pending status. So.. In admin panel you need to find those marked comments and process then accordingly - save or remove. Do you think it's convinient with text files? The same if use decided to remove its comment himself.
Reading files if you have many comments and many posts will be slower the it would be in case of database.
Scalability. One day you deside to extend you comments functionality to let one comment to respond to another. How would you do it with text files? Or example from comments by nico: "In 6 months time, when you will want to add a rating field to the comments... you'll have a big headache. Or, just run a simple ALTER query".
This is just for beggining. Someone may add something.
Well, there are good reasons why this isn't done. I can't possibly name them all, but the first things that come to mind:
Efficiency
Flexibility
Databases are much more efficient and flexible than plain text files. You can index, search and assign keys to individual comments and edit and delete any comments based on their key.
Furthermore, you'd get a huge pile of text files if the blog is quite big. While in itself that's not a problem, if you all save them in one directory, it can grow out of proportion and really increase the access time needed to find and open a specific text file.

handle csv import with larga array through a three step process

i need some help with a project of mine. It is about a dvd database. In the moment i am planning to implement a csv data function to import dvds with all information from a file.
I will do this in three steps.
Step 1
- show data i want to import, building array
- import data, building session arrays
Step 2
- edit informations
Step 3
- showing result before update
- update data
so far it works but i have a problem with large files. the csv data has 20 columns (title, genre, plot etc.) and for each line in the csv there are some arrays i create to use it in the next steps.
When i have more about 500 lines the browser often collapse while importing. I get no response.
Anyway now i trying to do this as an ajax call process. The advantage is, that i can define how many procedures the system handle each call and the user can see that the system is still working, like an statusbar when down/uploading a file.
In the moment i try to find some usefull example illustrating how i can do this, but i could not find something useful till now.
Maybe you have some tipps or an example how this could work, saying processing 20 lines each call, building the array.
After i would like to use the same function to build the session arrays using in the next step and so on.
Some information:
i use fgetcsv() to read the rows from the file. i go through the rows and each column i have different querys like is the item id unique, the title exist, description exist etc.
So if one of these data is not entered i get an error which row and column the error occures.
I´d appreciate any help from you
use 'LOAD DATA INFILE' syntax. ive used it on files upwards of 500mb with 3mil rows and it takes seconds, not minutes.
http://dev.mysql.com/doc/refman/5.0/en/load-data.html
While this is not the direct answer you were looking for
500 lines shouldnt take too long to process, so.. heres another thought for you.
Create a temporary table with the right structure of fields
you can then extract from it using select statements the various unique entries for the plot, genre etc rather than making a bunch of arrays along the way
mysql import would be very fast of your data
You can then edit it as required, and finally insert into your final table the data you have from your temporary but now validated table.
In terms of doing it with ajax, you would have to do a repeating timed event to refresh the status, the problem is rather than 20 lines, it would need to be a specific time period, as your browser has no way to know, assuming the csv is uploaded and you can process it in 20 line chunks.
If you enter the csv in a big big textbox, you could work on by taking the first 20 lines, passing it the remainder to the next page etc, would strike me as potential mess.
So, while I know ive not answered your question directly, I hope I gave you food for thought as to alternative and possibly more practical alternatives

Categories