problem on php file read-write-edit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dentrite
Forum Newbie
Posts: 16
Joined: Thu Jun 17, 2010 7:10 am

problem on php file read-write-edit

Post by dentrite »

hi, first let it be known to anyone answering my question that i am n00b to the php coding but i have been playing with php for more than a year or so, now i have a problem:
i have a structure like this(as i learnt in C++, i guess there is something similar in php too):
Struct student
{
int roll;
char name;
int class;
}

i want to write or append an object of this structure in a file named 'datafile.dat' on my server and read it later to display some specific object in other php file. i may have to edit the object data too some time. so is there any code for this?.. yeah there are a lot, but i cant get anything. so only codes and explanations needed here. if you can please tell me how to read write and edit a structure in to a file through php on server. (many would say i would have used database for it, but man, mind it, i have no extra bucks). thanks in advance.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: problem on php file read-write-edit

Post by twinedev »

Look up the function serialize and unserialize they be what you are looking for. (see http://us.php.net/manual/en/language.oo ... zation.php)

-Greg
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: problem on php file read-write-edit

Post by McInfo »

dentrite wrote:i have a structure like this(as i learnt in C++, i guess there is something similar in php too):
Struct student
{
int roll;
char name;
int class;
}
In PHP, the thing most analogous to C's struct is this.

Code: Select all

class Student
{
    var $roll;
    var $name;
    var $class;
}
$myStudent = new Student();
$myStudent->roll = 21;
$myStudent->name = 'My Student';
$myStudent->class = 42;
var_dump($myStudent->name);
If you want initialization to be a little cleaner, you can add a constructor.

Code: Select all

class Student
{
    var $roll;
    var $name;
    var $class;
    function __construct ($roll, $name, $class) {
        $this->roll = $roll;
        $this->name = $name;
        $this->class = $class;
    }
}
$myStudent = new Student(21, 'My Student', 42);
var_dump($myStudent->name);
There are other options, though, for example, creating an instance of the built-in stdClass.

Code: Select all

$myStudent = new stdClass();
$myStudent->roll = 21;
$myStudent->name = 'My Student';
$myStudent->class = 42;
var_dump($myStudent->name);
Or create an associative array.

Code: Select all

$myStudent = array (
    'roll' => 21,
    'name' => 'My Student',
    'class' => 42,
);
var_dump($myStudent['name']);
Any of those would be compatible with Greg's serialization suggestion.
(many would say i would have used database for it, but man, mind it, i have no extra bucks)
No extra bucks needed. PHP has a built-in database management system for SQLite (SQLite3, PDO SQLite).

See also: Filesystem Functions
Post Reply