Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Web Design (WordPress, Joomla, etc.) Can you do this in PHP?

  • Can you do this in PHP?

    Posted by Scott_g on December 18, 2005 at 9:10 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
    Scott


    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))
    {
    die(“

    Please enter a valid email address

    “);
    }

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

    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);
    fclose($userFileHandle);

    // Success
    echo(“

    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 . “
    “;

    ?>

    Curtis Thompson replied 20 years, 6 months ago 2 Members · 1 Reply
  • 1 Reply
  • Curtis Thompson

    December 18, 2005 at 11:12 pm

    hello…

    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(“\r\n”, “\n”, $data);

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

    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... sitruc

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy