All pastes #950843 Raw Edit

du

public php v1 · immutable
·published 2008-03-20 21:35 UTC
rendered paste body
<?php  //Turn error reporting on for development  error_reporting( E_ERROR | E_WARNING | E_PARSE | E_NOTICE );  //Array of parents  $parents = array( 'Wendy' => 'Mom', 'Mark' => 'Dad' );  //Use loop to cycle through array of parents  echo "<p>";  foreach( $parents as $key => $value )   {      echo $key . ' ' . $value . '<br/>' . "\n";  }  echo "</p>";  //Reset array  reset( $parents );  //Use different loop to approach cycling through array of parents  echo "<p>";  while ( list( $name, $title ) = each( $parents ) )  {    echo $name . ' ' . $title . '<br/>' . "\n";  }  echo "</p>";  //Initialize array that will contain sisters  $sisters = array();  echo '<em>' . getType($sisters) . '</em> for sisters created.';  //Add sisters to array  $sisters[ 'Sierra' ] = "Sister";  $sisters[ 'Maia' ] = "Sister";  $sisters[ 'Amanda' ] = "Sister";  //Iterate through array of sisters  echo '<p>';  while ( list( $name, $title ) = each ( $sisters ) )  {    echo $name . ' ' . $title . '<br/>' . "\n";  }  echo '</p>';  //Combine (union) serpate arrays into one family array  //Defines brother array inline  $family = $parents + $sisters + ( $brother = array( 'Kyle' => 'Brother' ) );  //Cycle through family array  echo '<p>';  while ( list( $key, $value ) = each ( $family ) )   {      echo $key . ' ' . $value . '<br/>' . "\n";  }  echo '</p>';  //Clear the family array from memory  unset( $family );  //Create multidimmensional family array  $family[ 'parents' ] = $parents;  $family[ 'sisters' ] = $sisters;  $family[ 'brother' ] = $brother;  //Work through multidimmensional array  echo '<table border=2>' . "\n";  echo '<tr>' . "\n" . '<th colspan=2> Family </th>' . "\n" . '</tr>' . "\n";  echo '<tr>' . "\n" . '<th> Name </th> <th> Title </th>' . "\n" . '</tr>' . "\n";  while ( $members = each( $family ) )   {    while ( list( $name, $title ) = each( $members[ 'value' ] ) )    {      echo '<tr>' . "\n";      echo '<td>' . $name . '</td>';      echo '<td>' . $title . '</td>' . "\n";      echo '</tr>' . "\n";    }  }  echo "</table>  \n";  echo "<br/>  \n  ";  //Moves array pointer from the end back to the start  reset( $family );  //Copies the names from the family array to another array  while ( $members = each ( $family ) )  {   while ( list( $name, $title ) = each ( $members[ 'value' ] ) )   {     $sorted[] = $name;   }  }  //Sort the array by value  arsort( $sorted );    //Reference the sorted array by a different name  $names = &$sorted;    //Work through array sorted by name  while ( $name = each( $names ) )   {    echo $name[ 'value' ] . "<br/>  \n  ";  }  echo "<br/>  \n  ";  //Form for writting new members to the flatfile  echo '<form action="array.php" method="post">' . "\n";  echo "\t" . '<table>' . "\n";  echo "\t \t" . '<tr>' . "\n";  echo "\t \t \t" . '<th>Name</th><th>Title</th><th>Location</th><th>Add</th>' . "\n";  echo "\t \t" . '</tr>' . "\n";  echo "\t \t" . '<tr>' . "\n";  echo "\t \t \t" . '  <td> ' . '<input type=' . "\"" . 'text' . "\"" . 'name=' . "\"" . 'name' . "\"" . '/></td>' . "\n";  echo "\t \t \t" . '  <td> ' . '<input type=' . "\"" . 'text' . "\"" . 'name=' . "\"" . 'title' . "\"" . '/></td>' . "\n";  echo "\t \t \t" . '  <td> ' . '<input type=' . "\"" . 'text' . "\"" . 'name=' . "\"" . 'location' . "\"" . '></td>' . "\n";  echo "\t \t \t" . '  <td> ' . '<input type=' . "\"" . 'submit' . "\"" . 'value=' . "\"" . 'Add' . "\"" . '/></td>' . "\n";  echo "\t \t" . '</tr>' . "\n";  echo "\t" . '</table>' . "\n";  echo '</form>' . "\n";  echo "<br/>  \n";  //After the form is submitted create short names for post data form variables  $name = $_POST[ 'name' ];  $title = $_POST[ 'title' ];  $location = $_POST[ 'location' ];  //Create variable reference to working directory  $DOCUMENT_ROOT = $_SERVER[ 'DOCUMENT_ROOT' ];  //Writes to text file only if the form has been submitted  if ( !empty( $name ) || !empty( $title ) || !empty( $location ) )  {    //Concatenate a delimiated string to add to the file    $string = "\r\n" . $name . ' - ' . $title . ' - ' . $location;     //Create file pointer to file that will be written too    //Suppress errors    @$fp = fopen( "$DOCUMENT_ROOT/data_family.txt", 'ab' );    //Write to file    fwrite( $fp, $string );    //Close file    fclose( $fp );        //Clear cached POST variables with a redirect to the page of origin    header( 'Location: array.php' );  }  //Read and copy the contents of this file into an array  $filearray = file( "$DOCUMENT_ROOT/data_family.txt" );  //Prepare table to present results returned from file  echo '<table border=2>' . "\n";  echo "\t" . '<tr>' . "\n";  echo "\t \t" . '<th colspan=3>Family</th>' . "\n";  echo "\t" . '</tr>' . "\n";  echo "\t" . '<tr>' . "\n";  echo "\t \t" . '<th>Name</th>' . '<th>Title</th>' . '<th>Location</th>' . "\n";  echo "\t" . '</tr>' . "\n";  //Cycle through file returning delimiated results  while ( list( $line, $string ) = each( $filearray ) )  {    $copy = explode( " - ", $string );    $name = $copy[0];     $title = $copy[1];    $location = $copy[2];    echo "\t" . '<tr>' . "\n";    echo "\t \t" . '<td>' . $name . '</td><td>' . $title . '</td><td>' . $location . '</td>' . "\n";    echo "\t" . '</tr>' . "\n";  }  //End table used to present results  echo '</table>' . "\n";  echo '<br/>' . "\n";?>