[Year 12 SofDev] Records in PHP

Kevork Krozian K.Krozian at fhc.vic.edu.au
Thu Jul 22 07:16:28 EST 2010


Looks good Laurie,

 It can be enhanced by saving to the one file ( vs one file per student ) and then read back to screen in a table.
The next task is to create a form to add and modify each object ( class Student ) and write back.
This is where the point I have been making for a while is brought into focus:
     1. Transactions on an object (instances of class Student) are only possible by loading the whole file into memory, adding/deleting/editing the one object, then writing the whole file back again.
     2. Searching suffers from the same malaise. Load whole file into memory, sort, search, edit or print or whatever once located then write the whole file back again.
      If a database is used this manual work would be avoided .....
Keep well


<?
class Student
{
public $name;
public $age;
public $class;

 public function writeStudent($fh)
 {
 
  $contents = "$this->name\t$this->age\t$this->class\n";
  fwrite($fh, $contents);
 }
}

$fileName = "allStudents.txt";
$fh = fopen($fileName, 'w');
$johnny = new Student();
$johnny->name = "Johnny";
$johnny->age = "12";
$johnny->class = "6A";
$johnny->writeStudent($fh);

$jenny = new Student();
$jenny->name = "jenny";
$jenny->age = "12";
$jenny->class = "6A";
$jenny->writeStudent($fh);
$john = new Student();
$john->name = "john";
$john->age = "12";
$john->class = "6A";
$john->writeStudent($fh);
fclose($fh); 

//display the screen
echo '<table border=0>';
//$fp = fopen('flat-file-data.txt','r'); 
$fh = fopen($fileName, 'r');
if (!$fh) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;} 
$loop = 0; 
while (!feof($fh)) { 
$loop++; 
$line = fgets($fh, 1024); //use 2048 if very long lines 
$field[$loop] = explode ('\t', $line); 
echo ' 
<tr> 
<td>'.$field[$loop][0].'</td> 
<td>'.$field[$loop][1].'</td> 
<td>'.$field[$loop][2].'</td> 
</tr>'; 
$fh++; 
} 
echo '<table>';
fclose($fh); 
 
 
 
?>


-----Original Message-----
From: sofdev-bounces at edulists.com.au [mailto:sofdev-bounces at edulists.com.au] On Behalf Of Laurie Savage
Sent: Thursday, 15 July 2010 7:25 AM
To: Year 12 Software Development Teachers' Mailing List
Subject: Re: [Year 12 SofDev] Records in PHP

I was playing with an idea along the lines of

class Student{
	public $name;
	public $age;
	public $class;

	public function writeStudent(){
		$fileName = $this-> name.".csv";
		$fh = fopen($fileName, a+);
		$contents = "$this->name, $this->age, $this->class";
		fwrite($fh, $contents);
	}
}

$johnny = new Student;
$johnny->name = "Johnny";
$johnny->age = 12;
$johnny->class = "6A";
$johny->writeStudent();

$liza = new Student etc

which creates a data file for each student.

Laurie Savage
Pascoe Vale Girls College


Just recently, on Wed, Jul 14, 2010 at 07:22:21PM +1000  in fact, Kevork Krozian mentioned:
> Hi Folks,
>  
> This is where it gets interesting to store a record in PHP ( in memory or in a file ).
> See http://www.designdetector.com/archives/04/10/FlatFileDatabaseDemo.php#database. Basically it is a very painful process without a database.
> An associative array can represent a record. And an array of associative arrays can  represent an array of records. These are effectively multidemensional arrays.
> As for classes, I wouldn't think it necessary to represent a record as such as in the serial or a sequential file example in the Fitzpatrick book in the quote from Laurie. 
> Interestingly the syntax for a class is :
> 
>    <?php 
>             class student {
>                 var $fname = "john";
>                 var $surname = "smith";
>                 var $age = "17";
>            }
>      $myStudent = new student();
>      echo "Student details".$myStudent->fname." " .$myStudent-> surname."  " .$myStudent->age;
> ?>
>    
>   It does look and feel like a record.  It is the class definition and instantiation that might throw the students I am thinking. 
> 
> Keep well
> 
> Kevork Krozian
> Digital Learning Manager
> Forest Hill College
> k.krozian at fhc.vic.edu.au
> Tel: 0419 356 034
> ________________________________________
> From: sofdev-bounces at edulists.com.au [sofdev-bounces at edulists.com.au] On Behalf Of David Dawson [David.Dawson at wesleycollege.net]
> Sent: Wednesday, 14 July 2010 11:23 AM
> To: sofdev at edulists.com.au
> Subject: Re: [Year 12 SofDev] Records in PHP
> 
> I am not sure of what the text says ( I use the other book :) but records per se are not basic data types.
> In PHP you can use a CSV file to store records and then process these using a simple array where the array position is a "field" and every element is initially a string.
> 
> 
> David Dawson
> Head of Information Technology Learning Area
> Head of Learning Technologies
> St Kilda Rd Campus
> Wesley College
> 577 St Kilda Rd
> Melbourne 3004
> Ph 8102 6340
> Mob 0425 718147
> 
> 
> >>> sofdev-bounces at edulists.com.au 07/14/10 8:40 am >>>
> 
> 
> This is my first time teaching SD. We’re using PHP and the Fitzpatrick/Keane text. PHP doesn’t have a specific type called a “record” and a single vector array doesn’t seem to match the concept as illustrated (fig 6.5, page 162). Looking at the example in the text I’m wondering if I could describe it as an instance of a class?
> 
> 
> 
> Laurie Savage
> 
> Pascoe Vale Girls College
> 
> 
> 
> 
> ____________________________________________________________________________
> 
> Sapere Aude - Dare To Be Wise
> 
> Wesley College Melbourne is a world class coeducational independent school
> developing the whole person through timeless principles of learning:
> - to know
> - to do
> - to live with
> - to be
> with innovation and wisdom
> 
> ABN 38 994 068 473  CRICOS 00354G
> ____________________________________________________________________________
> 
> This email is intended only for the use of the individual or entity named
> above and may contain information that is confidential and privileged. If
> you are not the intended recipient, you are hereby notified that any
> dissemination, distribution or copying of this email is strictly prohibited.
> If you have received this email in error, please email a reply to Wesley
> College and destroy the original message.
> 
> 
> _______________________________________________
> http://www.edulists.com.au - FAQ, Subscribe, Unsubscribe
> IT Software Development Mailing List kindly supported by
> http://www.vcaa.vic.edu.au - Victorian Curriculum and Assessment Authority and
> http://www.vitta.org.au/vce/studies/infotech/softwaredevel3-4.html
> http://www.vitta.org.au  - VITTA Victorian Information Technology Teachers Association Inc
> _______________________________________________
> http://www.edulists.com.au - FAQ, Subscribe, Unsubscribe
> IT Software Development Mailing List kindly supported by
> http://www.vcaa.vic.edu.au - Victorian Curriculum and Assessment Authority and
> http://www.vitta.org.au/vce/studies/infotech/softwaredevel3-4.html 
> http://www.vitta.org.au  - VITTA Victorian Information Technology Teachers Association Inc

-- 
Laurie Savage
====================================================================
Markbook/Moodle Coordinator::Pascoe Vale Girls College::03 9306 2544
====================================================================
You are standing on my toes.
_______________________________________________
http://www.edulists.com.au - FAQ, Subscribe, Unsubscribe
IT Software Development Mailing List kindly supported by
http://www.vcaa.vic.edu.au - Victorian Curriculum and Assessment Authority and
http://www.vitta.org.au/vce/studies/infotech/softwaredevel3-4.html 
http://www.vitta.org.au  - VITTA Victorian Information Technology Teachers Association Inc



More information about the sofdev mailing list