Zend - The PHP Company




Text

Add Code


Member register without database  

Type: application
Added by: Jilette
Entered: 09/01/2002
Last modified: 01/12/2001
Rating: - (fewer than 3 votes)
Views: 7094
Purpose?: Useful when you have PHP but no database .... Features: 1) You can sort the table by clicking on the column headers 2) Add new member 3) Update existing member Note: two pages included in the code example, make sure you split them.


<!-- read.phtml
Purpose?: Useful when you have PHP but no database .....
Here's one of two PHP-pages (read.phtml and editaddress.phtml)
You will also need a empty file called "address.txt" to be placed in the same directory.

This page reads a members including address, mail, phone from a file and displays it in a table

Features:
1) You sort the table by clicking on the column headers
2) Add new member
3) Update existing member

Missing features:
4) remove member
5) .. and a lot more a assume

Feel free to use it!
Please send me a mail with any comments/improvements you make:
mailto://jilette@hotmail.com

Stockholm, 8:th of Jan. 2002
tjillivippen p� dig!
/David

-->

<HTML>
<HEAD>
<TITLE>Adresser</TITLE>
</HEAD>
<BODY LINK="#0000ff" VLINK="#800080" BACKGROUND="images/bckgrnd.gif">
<H1>Adresser</H1>
Tips! Om du vill skriva ut den h&auml;r sidan: H&ouml;gerklicka i tabellen och v�lj "Print".
<HR>
<FONT SIZE=2>
<TABLE BORDER=5 CELLSPACING=1 WIDTH=864>

<TR>
<TD WIDTH="9%" VALIGN="MIDDLE" HEIGHT=10>
<B><P><a href="read.phtml?col=stamma">St&auml;mma</a></B></TD>
<TD WIDTH="9%" VALIGN="MIDDLE" HEIGHT=10>
<B><P><a href="read.phtml?col=fornamn">F&ouml;rnamn</a></B></TD>
<TD WIDTH="10%" VALIGN="MIDDLE" HEIGHT=10>
<B><P><a href="read.phtml?col=efternamn">Efternamn</B></TD>
<TD WIDTH="8%" VALIGN="MIDDLE" HEIGHT=10>
<B><P><a href="read.phtml?col=address">adress</B></TD>
<TD WIDTH="6%" VALIGN="MIDDLE" HEIGHT=10>
<B><P><a href="read.phtml?col=postnummer">postnr</B></TD>
<TD WIDTH="10%" VALIGN="MIDDLE" HEIGHT=10>
<B><P><a href="read.phtml?col=stad">postadress</B></TD>
<TD WIDTH="9%" VALIGN="MIDDLE" HEIGHT=10>
<B><P><a href="read.phtml?col=hemtel">hem tele</B></TD>
<TD WIDTH="10%" VALIGN="MIDDLE" HEIGHT=10>
<B><P><a href="read.phtml?col=mobil">mobil</B></TD>
<TD WIDTH="10%" VALIGN="MIDDLE" HEIGHT=10>
<B><P><a href="read.phtml?col=jobbtel">jobb</B></TD>
<TD WIDTH="22%" VALIGN="MIDDLE" HEIGHT=10>
<B><P><a href="read.phtml?col=mail">mail</B></TD>
<TD WIDTH="5%" VALIGN="MIDDLE" HEIGHT=10>
<B><I><P>&auml;ndra!</B></I></TD>
</TR>



<!-- php-code start -->
<?
// --------------------- THE MAIN FUNCTION -------------------------
// CALL 1) function readcontent, 2) function sort_by_column, 3) function print_content
// $col is passed on the URL, if $col= empty sort by "fornamn"

if ($col == "")
    
$col "fornamn";

$content readcontent();

$sort sort_by_column($content,$col);

print_content($content$sort);


//----------------------------------------------------------------------



// -------------------- THE READCONTENT FUNCTION---------------------
// read file, create content array "$addresspost"
function readcontent() {

// set filename to read and update
$filename "address.txt";

// get filecontent into string variable called $filecontent
if ($fd = (fopen($filename"r")))
    {
    
$filecontent fread ($fdfilesize ($filename));
    
fclose ($fd);
    }

else
    {
    print (
"couldn't open file");
    }


// explode filecontent into vector of strings called $addresspost
$addresspost explode ("#"$filecontent);

$multiarray = array ();
for (
$k=0$k<sizeof($addresspost); $k++)
    {
    
$addresselement explode (";",$addresspost[$k]);

    
$singelarray = array (
    
"index" => $addresselement[0],
    
"fornamn"  => $addresselement[1],
    
"efternamn"  => $addresselement[2],
    
"address"  => $addresselement[3],
    
"postnummer"  => $addresselement[4],
    
"stad"  => $addresselement[5],
    
"hemtel"  => $addresselement[6],
    
"mobil"  => $addresselement[7],
    
"jobbtel"  => $addresselement[8],
    
"mail"  => $addresselement[9],
    
"stamma"  => $addresselement[10],
    );
        
    
array_push($multiarray$singelarray);

    }
return 
$multiarray;
}
//----------------------------------------------------------------------



// -------------------THE SORT FUNCTION---------------------------
// create a array on the column the user requested and sort it!

function sort_by_column($content,$col) {

$sortedout = array ();
    for (
$k=1$k<sizeof($content); $k++) {
    
$sortedout[$k] = $content[$k][$col];
    }

asort ($sortedout);

return 
$sortedout;
}

//----------------------------------------------------------------------




// ---------------THE PRINT FUNCTION------------
// use the sorted array to print the content array correct sort order

function print_content($content_array$sorted_in) {

$new 0;
for (
1$key key ($sorted_in); next ($sorted_in)) 
    {

    if (
$new <= $content_array[$key][index])
        
$new $content_array[$key][index]+1;

    echo
    
"<TR>
    <TD WIDTH="
8%" VALIGN="MIDDLE"><font size=1>".$content_array[$key][stamma]."</font></TD>
    <TD WIDTH="
8%" VALIGN="MIDDLE"><font size=1>".$content_array[$key][fornamn]."</font></TD>
    <TD WIDTH="
8%" VALIGN="MIDDLE"><font size=1>".$content_array[$key][efternamn]."</FONT></TD>
    <TD WIDTH="
12%" VALIGN="MIDDLE"><font size=1>".$content_array[$key][address]."</font></TD>
    <TD WIDTH="
5%" VALIGN="MIDDLE"><font size=1>".$content_array[$key][postnummer]."</font></TD>
    <TD WIDTH="
9%" VALIGN="MIDDLE"><font size=1>".$content_array[$key][stad]."</font></TD>
    <TD WIDTH="
9%" VALIGN="MIDDLE"><font size=1>".$content_array[$key][hemtel]."</font></TD>
    <TD WIDTH="
9%" VALIGN="MIDDLE"><font size=1>".$content_array[$key][mobil]."</font></TD>
    <TD WIDTH="
9%" VALIGN="MIDDLE"><font size=1>".$content_array[$key][jobbtel]."</font></TD>
    <TD WIDTH="
9%" VALIGN="MIDDLE"><font size=1><a href="mailto://".$content_array[$key][mail]."">".$content_array[$key][mail]."</a></font></TD>
    
<TD WIDTH="9%" VALIGN="MIDDLE"><font size=1><I><a href="editaddress.phtml?memberid=".$content_array[$key][index]."&amp;status=0">&auml;ndra!</a></I></font></TD>
    </
TR></FONT>
    
";
    }

echo
"
</TABLE>
<
HR>
<
a href="editaddress.phtml?memberid=".$new."&amp;status=0">ny snubbe/snubba!</a>";

}
//----------------------------------------------------------------------


?>
<!-- php code end -->

<P><A HREF="
index.htm">Tillbaks....</A></P>
</BODY>
</HTML>


<!-- editaddress.phtml
Purpose?: Useful when you have PHP but no database .....
Here's one of two PHP-pages (read.phtml and editaddress.phtml)
You will also need a empty file called "
address.txt" to be placed in the same directory.

This page reads the address.txt file and fetches the wanted member.
Next it fills a form with current data.
The member edit the fields and post it back to the same page.
The page then writes the updated values back into the file.

Feel free to use it!
Please send me a mail with any comments/improvements you make:
mailto://jilette@hotmail.com

Stockholm, 8:th of Jan. 2002
tjillivippen p� dig!
/David
-->


<HTML>
<HEAD>
<TITLE>Joxa med adressen</TITLE>
</HEAD>
<BODY LINK="
#0000ff" VLINK="#800080" BACKGROUND="images/bckgrnd.gif">
 
<H1>&Auml;ndra raring</H1>
<
HR>
 <!-- 
php-code start -->
<?
 
// --------------------get current values from the file--------------------
// done every time this page is loaded
$filename "address.txt";
 
// get filecontent into string variable called $fileconent
if ($fd = (fopen($filename"r")))
    {
    
$filecontent fread ($fdfilesize ($filename));
    
fclose ($fd);
    }
 else
    {
    print (
"couldn't open file");
    }
 
// explode filecontent into array of strings called $addresspost
$addresspost explode ("#"$filecontent);
//--------------------------------------------------------------------------

// ------------------- IF $status==0 ---------------------------------------
// If $status = 0 => display form to change member
// Display FORM to change member and post it to this page but with $status=1
// $memberid and $status and staus from the URL (read.phtml=>editaddess.phtml) 
if ($memberid and $status==0)
    {
    for (
$k=0$k<count($addresspost); $k++)
        {
        
$addresselement explode (";",$addresspost[$k]);

        
// wanted member         if ($addresselement[0] == $memberid)
            
{

            
// replace " " with &nbsp to dislpay correctly in HTML
            
$firstname str_replace(" ""&nbsp;"$addresselement[1]);
            
$lastname str_replace(" ""&nbsp;"$addresselement[2]);
            
$street str_replace(" ""&nbsp;"$addresselement[3]);
            
$zip str_replace(" ""&nbsp;"$addresselement[4]);
            
$city str_replace(" ""&nbsp;"$addresselement[5]);
            
$homephone str_replace(" ""&nbsp;"$addresselement[6]);
            
$mobilephone str_replace(" ""&nbsp;"$addresselement[7]);
            
$workphone str_replace(" ""&nbsp;"$addresselement[8]);
            
$email str_replace(" ""&nbsp;"$addresselement[9]);
            
$stamma str_replace(" ""&nbsp;"$addresselement[10]);
            }
        }

    
// make the radio button dynamic and get the value of $stamma
    // used futher down in the HTML-code....
    //--------------------------------------------
    
$radiostring "";
    
$options = array ("1"=>"Sopran","2"=>"Alt","3"=>"Tenor","4"=>"Bas","5"=>Dirigent);
    
    for (
reset ($options); $key key ($options); next ($options))
        {
        
$radiostring $radiostring."<input type=radio name=stamma value="".$options[$key].""";

        if (
$options[$key] == $stamma)
        
$radiostring $radiostring." checked>".$options[$key];

        else
        
$radiostring $radiostring." >".$options[$key];
        }

    
//--------------------------------------------
    
print("
 <FORM ACTION="
editaddress.phtml?memberid=$memberid&amp;status=1" METHOD="POST">
  <TABLE>
    <TR>
      <TD>Stamma:</TD>
      <TD>
    
    <!-- created above, makes radio button dynmic -->
    
$radiostring
    
      </TD>
    </TR>
      <TR>
      <TD>
        <INPUT type=hidden name=memberid value="
$memberid">
      </TD>
    </TR>
     <TR>
      <TD>Namn: </TD>
      <TD>
        <INPUT type=text size=50 name=firstname value="
$firstname">
      </TD>
    </TR>
     <TR>
      <TD>Efternamn: </TD>
      <TD>
        <INPUT type=text size=50 name=lastname value="
$lastname">
      </TD>
    </TR>
     <TR>
      <TD>adress: </TD>
      <TD>
        <INPUT type=text size=50 name=street value="
$street">
      </TD>
    </TR>
     <TR>       <TD>Postnummer: </TD>
      <TD>
        <INPUT type=text size=50 name=zip value="
$zip"> 
      </TD>
    </TR>
     <TR>
      <TD>stad: </TD>
      <TD>
        <INPUT type=text size=50 name=city value="
$city">
      </TD>
    </TR>
     <TR>
      <TD>hemtel: </TD>
      <TD>
        <INPUT type=text size=50 name=homephone value="
$homephone">
      </TD>
    </TR>
     <TR>
      <TD>jobb: </TD>
      <TD>
        <INPUT type=text size=50 name=workphone value="
$workphone">
      </TD>
    </TR>
     <TR>
      <TD>mobil: </TD>
      <TD>
        <INPUT type=text size=50 name=mobilephone value="
$mobilephone">
      </TD>
    </TR>
     <TR>
      <TD>E-post: </TD>
      <TD>
        <INPUT type=text size=50 name=email value="
$email">
      </TD>
    </TR>
    </TABLE>
  <P> 
    <INPUT type=submit VALUE="
�ndra">
</FORM>
    "
);

    
//close if ($memberid and $status==0)
    
}
//-----------------------------------END IF $status==0---------------------------------------------
 

//-----------------------------------IF $status==1---------------------------------------------
// Recieve FORM and change member in file
// The form is completed and posted from editaddress.phtml => to editaddress.phtml
// with $status==1
// if there is a member id present start process 

function clearstring($string)
    {
    
$string str_replace("<""&lt;"$string);
    
$string str_replace(">""&gt;"$string);
    
$string str_replace("#"" "$string);
    
$string str_replace(";"" "$string);
    return 
$string;
    }

if (
$memberid and $status==1)
    {

    
//clear out some unwanted characters
    
$firstname clearstring($firstname);
    
$lastname clearstring($lastname);
    
$street clearstring($street);
    
$zip clearstring($zip);
    
$city clearstring($city);
    
$homephone clearstring($homephone);
    
$mobilephone clearstring($mobilephone);
    
$workphone clearstring($workphone);
    
$email clearstring($email);
    
//$stamma -- no need to check values from radio buttons
     // get all the the fields into a new member string, $newstring
    
$newstring $memberid .";" .$firstname .";" .$lastname .";" .$street .";" .$zip .";" .$city .";" .$homephone .";" .$mobilephone .";" .$workphone .";" .$email.";".$stamma;
      
// the filecontent is already stored in $addresspost
    // create new file as $filecontent
    
$filecontent "";
     
// the for loop explodes each $addresspost once more into a new vector called $addresselement
    // check the first element [0] = $memberid
    // for each post[0] not equal to current memberid just write it back into a $filecontent
    
for ($k=0$k<count($addresspost); $k++)
        {
        
$addresselement explode (";",$addresspost[$k]);
         
//something fishy with the array or file, don't get it. Happens sometimes....
        
if ($addresselement[0] == "")
            {
            continue;
            }
        
// All other members back into $filecontent
        
if (!($addresselement[0] == $memberid))
            {
            
$filecontent $filecontent ."#" .$addresspost[$k];
            }
         
//no else            
         
}
     
// now we should append the update member last in the string $filecontent
    
$filecontent $filecontent ."#" .$newstring;
     
// if file still exists...write the new content to it
    
if ($fil = (fopen($filename"w")))
        {
        
fputs($fil,$filecontent);
        
fclose($fil);
        }
    
    print (
"<a href="read.phtml">Tack!</a>");
     
//close if $memberid present and $status=1
    
}
//-----------------------------------END IF $status==1---------------------------------------------
 
?>
 </BODY>
</HTML>


Usage Example




Rate This Script





Search



This Category All Categories