FORUMS: list search recent posts

Can you do this in PHP?

COW Forums : Web Design

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Scott_GCan you do this in PHP?
by on Dec 18, 2005 at 9:10:49 am

Following is a test piece of PHP code that receives input from an HTML form. In 'C' there's a way to create a container, fill it with data, then write the whole container to disk with one command. You can see that I have used 7 different fwrite commands to accomplish this. Does anyone know how to do something like this in PHP?

I am working toward either a sequential or random access records file. Like it was done in 'the old days' before database software was available. We used to use arrays and loops. I am attempting to figure this out using an object.

Any information / input appreciated.

By the way, my code is being interpreted by your web site. :( So, it will not display properly. Any ideas?

Thank you

class blankRecord {
var $serverName;
var $userType;
var $userChar;
var $userFN;
var $userLN;
var $userEmail;
var $userComment;

$rec = new blankRecord;

// Receive form data
$rec->serverName = $_POST['selectName'] . "n";
$rec->userType = $_POST['selectType'] . "n";
$rec->userChar = $_POST['textfieldCharName'] . "n";
$rec->userFN = $_POST['textfieldFName'] . "n";
$rec->userLN = $_POST['textfieldLName'] . "n";
$rec->userEmail = $_POST['textfieldEmail'] . "n";
$rec->userComment = $_POST['textareaComment'] . "n";

// Validate email address
if (! ereg('[A-Za-z0-9_-]+@[A-Za-z0-9_-]+.[A-Za-z0-9_-]+', $textfieldEmail))

Please enter a valid email address


//User must enter an email address
if (strlen($textfieldEmail) == 0 )

textfieldEmail is empty


// Write user file
$userFileName = $textfieldCharName . ".txt";
$userFileHandle = fopen("users/" . $userFileName, 'w') or die("can't open file");
fwrite($userFileHandle, $rec->serverName);
fwrite($userFileHandle, $rec->userType);
fwrite($userFileHandle, $rec->userChar);
fwrite($userFileHandle, $rec->userFN);
fwrite($userFileHandle, $rec->userLN);
fwrite($userFileHandle, $rec->userEmail);
fwrite($userFileHandle, $rec->userComment);

// Success

Data successfully saved!" . "


// Print received data to screen
echo $rec->serverName . "
echo $rec->userType . "
echo $rec->userChar . "
echo $rec->userFN . "
echo $rec->userLN . "
echo $rec->userEmail . "
echo $rec->userComment . "


Return to posts index

Curtis ThompsonRe: Can you do this in PHP?
by on Dec 18, 2005 at 11:12:52 pm


i'd just use a file write type of thing like you have, but build it out in a variable first and then just write that once...but yes. there are a ton of ways to do what you want in php.

you could do it in a for loop over the post vars if you wanted to write them all, or you could add to that to filter out only fields that matched a certain pattern...but build the variable once and then write it all in one shot - that would be easier. i have a few functions that i put into an external lib that i use for any time i need it - here's a write function from that lib:

* writes the passed contents to the passed file
* $file: file (including path)
* $data: data
function writeFileContents ($file, &$data) {

// get rid of them ugly windows "^M" doohickies
$data = ereg_replace("rn", "n", $data);

// write the file
$fp = fopen($file, "w") or die ("unable to open $file for writing");
fwrite($fp, $data);

then it's a quick call to write the variable that you've populated with your form data...i would probably skp creating the class in that instance - you can just loop through the $_POST array and get the data that way...

and with regards to the site messing up code - just put "&lt;" instead of "<" and the html tags won't be parsed...


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2016 All Rights Reserved