Jump to content


Photo

Variables


  • Please log in to reply
6 replies to this topic

#1 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 25 August 2006 - 12:23 AM

Are variables supposed to open ONLY on the page that the data is assisgned to a variable? I have a page that with a piece of data that has a variable assigned to it. I can use that variable anywhere on that page and the data from it will come up. On the next page I tried to use the echo feature to show that data like I would on the first page and that did not work. The page would not come up and showed an error. So, I don't know if that is the way it is or if I'm doing something wrong.

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 25 August 2006 - 12:26 AM

Are variables supposed to open ONLY on the page that the data is assisgned to a variable?


Yes.  PHP does not run constantly like a C or C++ program.  It executes the page, then it stops, which kills all variables.  You can have some variables persist by putting them into the session, or using cookies.

The exception to this is if you are writing a program for php-gtk...but then it's closer to writing a C program than a web app.

#3 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 25 August 2006 - 12:33 AM

Just as I thought. Thanks, but I have no idea how to make sessions or putting data in cookies.............

#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 25 August 2006 - 12:41 AM

Thanks, but I have no idea how to make sessions or putting data in cookies


Refer to the manual for sessions...http://www.php.net/sessions

To keep data in a cookie, write it on small strips of paper and mix it into the dough.  You may also want to lower the oven temp to keep the paper from burning.  (http://www.php.net/setcookie)

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 25 August 2006 - 01:13 AM

example of session:

page1.php
<?php
   session_start(); //start the session
   $_SESSION['foo'] = 'bar'; //make a session variable
   header('Location: page2.php'); //redirect to new script
?>

page2.php
<?php
   session_start();
   echo $_SESSION['foo']; //output: bar
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 25 August 2006 - 03:01 AM

Am I supposed to understand this some how? Lol. For a n00bie this doesn't make much sense. I appreaciate you trying hopfully I'll get better at this some day so I can start helping other people out and know what I'm talking about.  :)

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 August 2006 - 06:11 AM

when you have a variable in a script that displays info from a variable to the user, and then the user goes to another page, you have to do certain things in order to get the same data to show on the new page. it doesn't automatically show up anywhere. you have to "carry it over" to the new page.  there are lots of different ways of doing this. It really depends on your needs, as to what exactly is the best way to go about doing this.  You can make a form for the user to fill out, and post the information to a new page. You can store your information in a database or a flatfile and have each page access the database or flatfile, retrieve the information and show it.  You can make a cookie - a variable stored on the user's computer, and each page would access that cookie.  You can make a session variable, which is mostly the same as a cookie, but stored on the server. Well, it's more complex than that, but we'll just leave it at that. 

The example shown be myself and others was a session variable. The idea is to tell your script that you want to start a session, which is basically a fancy way of telling the server that hey, you have some information that you want to carry over from one page to another, so pay attention.  then you declare the session variable, just like you would any other variable, except that it's stored in an array called $_SESSION. you would access the information just like you would access information from any other array. 

On each new page, you will always start with session_start(); that tells the server that you have information out there somewhere and you want to be able to get to it.  Then you would use the session variable just like a normal variable. 

you can try this out for yourself by making 2 .php files just like in the example i showed you.  make 2 files. one called page1.php and one called page2.php. copy/paste the contents of each one, as shown above. upload it to your server and goto page1.php in your browser. it will make the session variable, redirect you to page2.php and echo it out on the screen. 
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users