Location: PHPKode > projects > Modular Site Manager > htdocs/tutorials/PHP-8.php
Well, this is a pretty simple matter, if you do it right, so I'll explain it.<br />

<br />

<strong>Main Functions Used</strong><br />

fopen(string filename, string mode);<br />

fwrite(resource fp, string str);<br />

fclose(resource fp);<br />

explode(string seperator, string str);<br />

array_search(mixed needle, array haystack);<br />

file_get_contents(string filename);<br />

<br />

<strong>The HTML</strong><br />

Insert this HTML code where you want the user to type his/her text:<br />

<br />
<div class="code">Code<hr />
<?php xhtml_highlight('
<form action="<? echo $PHP_SELF; ?>" method="post">
  <table width="201" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="201">Your Name: </td>
    </tr>
    <tr>
      <td><input name="username" type="text">&nbsp;</td>
    </tr>
    <tr>
      <td>Your Email Address: </td>
    </tr>
    <tr>
      <td><input name="user_email" type="text"></td>
    </tr>
    <tr>
      <td>Personal Information:</td>
    </tr>
    <tr>
      <td><textarea name="input_text" cols="40" rows="5"></textarea></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><input name="Submit" type="submit" value="Insert!"></td>
    </tr>
  </table>
</form>
'); ?></div><br />

<br />

<strong>Inserting Into The Text DB</strong><br />

Insert this code underneath where you inserted the HTML:<br />
<div class="code">Code<hr />
<? xhtml_highlight('
<?
if(isset($_POST[\'Submit\'])){
   $in = "text_database.txt";
   /*Change this to the text file you\'ll be using as your database!*/
   $open = fopen($in, "a");
   /*Opens the database file in \'ammend\' mode.*/
   $input_text = $_POST[\'input_text\'];
   $username = $_POST[\'username\'];
   $user_email = $_POST[\'user_email\'];
   /*Retrieve the information from the HTML form that the user submitted.*/
   $out = fwrite($open, "$username|sep|$user_email|sep|$input_text|sep|");
   /*Insert the user submitted text into the text database.*/

   fclose($in);
   /*Close the file using fclose.*/
}
?>'); ?></div><br />

<br />

Also, you must remember to CHMOD your database file to 777 for this part to work!<br />

<br />

Okay, now that we've inserted the user's information, let's try and retrive specific parts of it.<br />

<br />

<strong>Searching The text DB</strong><br />

Now, lets say we wanted to have a profile for the user that just posted that information, how would we get their profile from the text DB?<br />

<br />

Easy with this code:<br />
<div class="code">Code<hr />
<? xhtml_highlight('<?

if(!empty($_GET[\'user\'])){
   $user = $_GET[\'user\'];
   /*Get the username from the URL.*/

   $in = "text_database.txt";
   /*Change this to the text file you\'ll be using as your database!*/

   $open = fopen($in, "r");

   /*Opens the database file in \'read\' mode.*/

   $content = file_get_contents($in);
   /*Grab the contents of the database.*/

   $data_array = explode("|sep|", $content);

   /*Use the explode command to break the contents of the DB down into an array,
   with each element of the array being seperated by "|sep|".*/


   $search = array_search($user, $data_array);

   /*Use the search_array function to search the text database for the info that
   you\'re looking for.*/

   $username = $data_array[$search];
   /*Get the contents found in the array key that the function array_search found for us.*/

   $personal_info_key = $search + 2;

   /*Get the \'personal info\' field key from the text database by adding 2 to the array key found with array_search.*/

   $personal_info = $data_array[$personal_info_key];
   /*Grab the personal info field itself from the array.*/

   $email_key = $search + 1;
   /*Get the \'user email\' field key from the text database by adding 1 to the array key found with array_search.*/

   $email = $data_array[$email_key];
   /*Grab the email field itself from the array.*/

   echo("
   <table width=\"474\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
     <tr>
       <td width=\"474\">Username: ".$username." </td>
     </tr>
     <tr>
       <td>&nbsp;</td>
     </tr>
     <tr>
       <td>My Email: ".$email."</td>
     </tr>
     <tr>
       <td>&nbsp;</td>
     </tr>
     <tr>
       <td>About Me: </td>
     </tr>
     <tr>
       <td>".$personal_info."</td>
     </tr>
     <tr>
       <td>&nbsp;</td>
     </tr>
   </table>");
   /*Echo the HTML to display the profile information.*/
}else{
   echo("Sorry, but you must specify a user!");
}
?>'); ?></div><br />

<br />

<strong>Conclusion</strong><br />

Well, that's about it, if you have any questions, just ask them here.
Return current item: Modular Site Manager