Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
help: HTML form submitted data to CSV file
Old 10-31-2010, 03:46 AM help: HTML form submitted data to CSV file
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
What I am trying to do is have user-input (from an HTML form) appended to a CSV file


I looked at the manual for fputcsv() and I am scratching my head.

Instead of confusing you with my crappy-code, perhaps I can have someone show or explain to me how to store field names to a multi-dimensional array, so I can run the built-in fputscv function with success.
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-31-2010, 04:43 AM Re: help: HTML form submitted data to CSV file
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
Does it have to be multi-dimensional array?

Here is an example with single array:
PHP Code:
$ar_fields = array('field_name1''field_name2''field_name3'); // name of the html fields (must be correct)

foreach($_POST as $key => $value) { // assuming you are using POST method
     
if(in_array($key$ar_fields)) {
          $
$key $value;
     }

After that you call the array with $ar_fields['field_name1'], $ar_fields['field_name2']...

Or in your case list the array while you write to csv file.

PHP Code:
$fp fopen('file.csv''w');

reset($ar_fields);
while(list(
$key$value) = each($ar_fields)) {
     
fputcsv($fp$value);
}
fclose($fp); 
Someone correct me if im wrong with csv writing.
miki86 is online now
Reply With Quote
View Public Profile
 
Old 11-06-2010, 05:58 AM Re: help: HTML form submitted data to CSV file
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
it aint workin' mang...

This is a shortcut of what I got.

PHP Code:
 
<?php
 
// if submit, send to CSV
if(isset($_POST['submit']))
 
//configure CSV input
$ar_fields = array('practice_name''num_prov''num_staff''num_rooms');
foreach(
$_POST as $key => $value) {
     if(
in_array($key$ar_fields)) { 
          
$key $value
     } 

$fp fopen('data.csv''w'); 
reset($ar_fields); 
while(list(
$key$value) = each($ar_fields)) { 
     
fputcsv($fp$value); 

fclose($fp);
 
?>
 
<form name=form  action="form.php" method="POST">
    <table border="0" align="left" cellpadding="11">
    <tr valign="top">
        <td>
        <p><FONT COLOR="#54C571">Name of practice:</FONT></p>
        </td>
        <td><input type="text" name="practice_name" value="<?Php echo $_POST['practice_name']; ?>" />
        </td>
    </tr>
 
<tr valign="top">
        <td>
        <p>Number of providers:</p>
        </td>
        <td><input type="text" name="num_prov" value="<?Php echo $_POST['num_prov']; ?>"/>
        </td>
</tr>
 
<tr valign="top">
        <td>
        <p><FONT COLOR="#54C571">Number of staff:</FONT></p>
        </td>
        <td><input type="text" name="num_staff" value="<?Php echo $_POST['num_staff']; ?>" />
        </td>
</tr>
 
 <tr valign="top">
        <td>
        <p><FONT COLOR="#54C571">Number of exam rooms:</FONT></p>
        </td>
        <td><input type="text" name="num_rooms" value="<?Php echo $_POST['num_rooms']; ?>" />
        </td>
</tr>
 
</table>
</form>
the err I keep getting is: "Warning: fputcsv() expects parameter 2 to be array"

How is it NOT an array?
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE

Last edited by Lashtal; 11-06-2010 at 06:00 AM..
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 11-06-2010, 10:17 PM Re: help: HTML form submitted data to CSV file
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
forgot to include the submit button in my HTML form, but you get the point.
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 11-08-2010, 01:31 AM Re: help: HTML form submitted data to CSV file
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
php.net shows fputcsv as a buggy built-in function, particularly on windows servers.

So i'm going to build a work-around.

What i'm going to do is store the HTML field names to variables, and INSERT them into MySQL

Then, export MySQL table names and rows as a CSV file
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 11-08-2010, 03:35 AM Re: help: HTML form submitted data to CSV file
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
How do you plan to export mysql to csv?
You still need to use fputcsv.

Second parameter for fputcsv needs to be an array.
By looking at the examples...you need to build array into an array.
My example above is for single array thats why it isn't working.

Last edited by miki86; 11-08-2010 at 03:40 AM..
miki86 is online now
Reply With Quote
View Public Profile
 
Old 11-08-2010, 04:21 AM Re: help: HTML form submitted data to CSV file
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
Quote:
Originally Posted by miki86 View Post
How do you plan to export mysql to csv?
You still need to use fputcsv.
MySQL data can be accessed through the browser as a CSV, without using built-in function fputcsv.

Quote:
Originally Posted by miki86 View Post
By looking at the examples...you need to build array into an array.
My example above is for single array thats why it isn't working.
do you mean: Multi-Dimensional?
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 11-08-2010, 04:58 AM Re: help: HTML form submitted data to CSV file
miki86's Avatar
Extreme Talker

Posts: 185
Location: print_r($serbia);
Trades: 0
Yeah Multi-Dimensional array.

Look at the first example on php.net

btw you have an error on my example:
PHP Code:
 if(in_array($key$ar_fields)) { 
          
$key $value
     } 
It needs to be double dollar sign in front of key if you want to change the value:
PHP Code:
 if(in_array($key$ar_fields)) { 
          $
$key $value
     } 

Last edited by miki86; 11-08-2010 at 05:01 AM..
miki86 is online now
Reply With Quote
View Public Profile
 
Old 11-08-2010, 07:11 AM Re: help: HTML form submitted data to CSV file
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
ARG, I deprecated it without a second thought, believing you made a syntax error. lol
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to help: HTML form submitted data to CSV file
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28966 seconds with 12 queries