Quick Search for:  in language:    
Ever,needed,pass,class,data,from,page,next,wi
   Code/Articles ╗ |  Newest/Best ╗ |  Community ╗ |  Jobs ╗ |  Other ╗ |  Goto ╗ | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
PHP Stats

 Code: 113,010. lines
 Jobs: 41. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for PHP
Simplest Web-Counter
By Kamran Riaz on 1/18


Easy Webcomic System
By Kyle Temkin on 1/17


Click here to see a screenshot of this code!AutoIndex PHP Script (Directory Indexer)
By J J H on 1/17

(Screen Shot)

DB Selector
By Martyn Merrett on 1/17


MapQuest Link Generator
By Steve Schoenfeld on 1/15


Click here to see a screenshot of this code!A Simple FTP Solution
By Marcelo Valle Franco on 1/15

(Screen Shot)

Web Blog / Content Updater
By Matthew Sparrow on 1/14


Current Users Online v1
By Robert Peterson on 1/13


Click here to see a screenshot of this code!Blobsy : MSN Messenger Bot
By JawishHameed on 1/12

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Passing classes from page to page

Print
Email
 

Submitted on: 11/16/2002 3:44:23 PM
By: Dustin R Davis  
Level: Beginner
User Rating: By 3 Users
Compatibility:PHP 4.0

Users have accessed this article 2333 times.
 

(About the author)
 
     Ever needed to pass a class and its data from one page to the next? I will show you how.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

 

Introduction

The scenario: Lets say you have a customer who is buying something. The customer is checking out, because he is done shopping. You have setup two pages, one with his personal information such as name address etc. and the other with the billing information. Since you are so organized and neat, you don't want to jumble the page with form fields so you decided to have two separate pages.

You think to yourself, "Wow, that's allot of variables to keep track of!". You ponder how much work this will be and you finally come up with a solution: You will use a class to store the information!

After designing and building a class, you sit back and sip your beer. Unfortunately you cant enjoy that sip because you realized that you have no idea how you are going to send the information from page one over to page two!

Well, I'm going to show you how to move your classes and your data that's held in them from page to page.


The Class

This will held in your class.php file. You will need to include this in every page you want to access your class and its data.

<?PHP
class cMyClass {

var $Name;

function PrintName() {

echo "Your name is ".$this->Name;
return true;

}

}
?>


The Input page

This page will be what you submit your personal information to.

<?PHP
include("./class.php");

$myClass = new cMyClass;

$myClass->Name = $HTTP_POST_VARS["Name"];

... //other code you have

$_SESSION["MyClassData"] = serialize($myClass);

?>


The last page

This page will display their information to them. The information that was carried over from page to page on the site.

<?PHP
include("./class.php");

$temp = $_SESSION["MyClassData"];

$myClass = unserialize($temp);

$myClass->PrintName();

?>


Conclusion
Unfortunately, due to my laziness, I gave a small, crude example. I think you get the point though. Once you have designed and built your class, you can save it in an include file. Make sure to have this file included in every .php file you will be using your class in. In each page you will need to access your class data by using unserialize() and when you are finished you will need to save it again using serialize(). I'm using the session method because I do not have access to write files on my server, and it would be too hard to write a file for each person that wants to order something. There are too many people. You can look in the PHP manual (v10) to figure out how to write them to a file and retrieve them from a file.


Other 3 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
11/16/2002 3:45:46 PM:Dustin R Davis
I hope you enjoy my tutorial. Its simple but easy. I hope it helps someone. It was such a pain to figure this out on my own.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/20/2002 9:23:16 AM:Srirangan
Very helpful but Wrongly Titled!!<br> Should be "Passing objects from page to page"

:-)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/29/2002 10:04:35 AM:Mr. Chuckles
You can get the session ID variable and use that as a file name. I run PHP under Apache in Windows. The sessions are stored in the sessions directory using the session ID as the file name. If that is good enough for the PHP developers then it's good enough for me. Check if your hosting service offers MySQL or another database. If not - move! You can then write the serialied object (class - whatever) to a field in the users record. Either way simple things can be used to purge old data. This is a cool foundation onto which yo can build things.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/8/2003 8:49:07 PM:
Hi, Thanks heaps for this tut. Every time i unserialize my class then attempt to use its functions i get this err... Fatal error: Call to a member function on a non-object in c:\inetpub\wwwroot\tgm\admin\index.php on line 10. I give there seems to be no reason for this PLEASE HELP????
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | PHP Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyrightę 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.