Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSV textfield output split into arrays

Status
Not open for further replies.

dreamaz

Technical User
Dec 18, 2002
184
CA
Hi,

Forgive the subject i didn't really know how to explain it. I have a textfield where i paste csv data roughly 27 fields. When you hit submit it will display on the same page, each field in its own column.

What i need now is when its converted, i'd like to send someone the URL for the page and they can view the same data that was used, im guessing a tmp file to be created which can be included on the URL - not sure how this is done. Below is my current code, any help is appreciated.

Thanks,

dR


-----TEXTFIELD------
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">

<p><textarea name="feild1" cols="50" rows="5" id="feild1"></textarea>

<input type="submit" value="CONVERT!">

-----DISPLAY IN TABLE------
<table width="2370" border="0" cellpadding="3" cellspacing="0">
<?php if(isset($_POST['feild1'])){
$stripped = str_replace("\r", ",", $_POST['feild1']);
$BREAK = explode(",", $stripped);
//
echo "<TR>";

$count=count($BREAK);

for ($i = 0; $i < $count; $i++) {

//if the count is greater than 28, then start a new table row

if (($i)%32==0)
{
echo "</TR><TR>";
echo "<TD><FONT SIZE=2>".$BREAK[$i]."</FONT></TD>";
}
else {
if (($i+1)%32==0)
{
echo "<TD><FONT SIZE=2>".$BREAK[$i]."</FONT></TD>";
}
else {
echo "<TD><FONT SIZE=2>".$BREAK[$i]."</FONT></TD>";
};
};
};

}
?>
</tr>
</table>
</form>
 
You would have to save the information from the textarea into a file on the server.
You would then have to pass the url of the file to your script, and then display it.

You could then just give the other person the URL and have the script parse it and display it.

First a function to save the file and give out a url:
Code:
function save_field_to_file($field){
$filename="tempfile.tmp";
$handle=fopen($fielname,"w");

 if (fwrite($handle, $field) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
  
   echo "Success, wrote ($somecontent) to file <a href='myscript.php?display=$filename'";
  
   fclose($handle);



}

Then add it to your existing code.


Code:
DISPLAY IN TABLE------
<table width="2370" border="0" cellpadding="3" cellspacing="0">
<?php if(isset($_POST['feild1'])){
[green]//save to file[/green]
[red]$fileurl=save_field_to_file($_POST['feild1'];[/red]

      $stripped = str_replace("\r", ",", $_POST['feild1']);
$BREAK = explode(",", $stripped);


You would of course have to check whether the request for the display is coming from the form or from the link to the file.

so you check for a $_GET value for display:

Code:
if(isset($_GET['display'])){
[green]you should check that the file name is valid etc...[/green]

$handle=fopen($_GET['display'],'r');
$contents = fread($handle, filesize($filename));
fclose($handle);


and then just pass the $contents variable to your exisiting script.

To summarize and in pseudo code:

[code]

check wether a fiel variable exists in $_GET]['display']
if there is one open the file read it, and pass it to the table creation script to parse.

If no file is in the GEt variable.
Check if a field from the form was submitted.

if nothing was submitted just display the form.



}
[/code]




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi,

I am getting

Fatal error: Call to undefined function: save_field_to_file()

I included in my header:

function save_field_to_file($field){
$filename="tempfile.tmp";
$handle=fopen($filename,"w");

if (fwrite($handle, $field) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file <a href='myscript.php?display=$filename'";

fclose($handle);
}

Thanks for your help!
 
You realize the code was not meant to be taken verbatim. I juts typed it into the reply box to give you an idea.

In any case where are you declaring the function? and where are you calling it?





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top