CSV and random form elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michelangeloOo
Forum Newbie
Posts: 1
Joined: Fri Oct 28, 2005 12:17 am

CSV and random form elements

Post by michelangeloOo »

Hi all,

OK let me try to explain the problem:

I have a form created by the following:

Code: Select all

<?php

setcookie ("start", time());
$array = array("block1.html","block2.html","block3.html");
shuffle($array);
$show = implode(",",$array);
include ("formopen.html"); //contains html tags and form tag
include("$array[0]");// form part I
include("$array[1]");//form part II
include("$array[2]");//form part III
include ("formclose.html"); // contains submit button ...


?>
When the submit button is clicked,the form handler is as follow:

Code: Select all

<?php

$handle = fopen ("data/data.csv",'r'); // open file
$data = fgetcsv($handle, 1000, ","); // get csv
$data_fields = explode (",",$data); // create array
fclose($handle); // close file

// write data in a csv file
$fp = fopen("data/data.csv", 'a+');
foreach ($_POST as $k => $v)
{
fwrite ($fp, "$v,");
;
}
fclose($fp);
?>
The problem is that I have a header in the CSV file (that I picked up into $data) BUT .. I have no idea how to put the form elements value into the proper column of the csv file (NOTE: each column of the csv file corresponds to an elements name)
So that
HTML Code:

Code: Select all

<input type='text' name='rded1' size='40' maxlength='30'>

rded1 is also the name of column in the csv file (say position 4). BUT rded1 in $_POST might be position 1 or 6 or 15 depending the order determined by the randomization..
PLEASE HELP ME FINISH THIS >>>>> IT DRIVES ME INSANE !!!!!!!!!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I'd suggest not using the foreach ($_POST as $k => $v) loop, explicity call upon your expected variables and filter, validate and escape them where necessary.

It is possiblly that, that is giving you headache.
Post Reply