13 Replies - 4817 Views - Last Post: 05 May 2010 - 12:45 AM Rate Topic: -----

#1 redrin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 46
  • Joined: 01-May 10

transfer variable from one page to another

Posted 04 May 2010 - 09:50 AM

so i'm still working on my little creature catcher game, and thus far for the part where you get the creature, i'm pretty pleased. the only issue is, i want to make the .php to be called on so that it doesn't need to be posted for every page that a creature is caught on. right now i have it set so that it can be copy/pasted with a single variable at the top changed, but i want to be able to change that variable externally.

not sure how to explain this exactly, so i apologize if it's hard to understand. i'm basically trying to use the same code sheet for each creature without making new ones.

here is my code for the sheet itself:
<?php
session_start();
        //
        //edit the below ONLY
        //
        $petsname = 'charmander'; //just edit the name here. NOTHING else.
        //
        // DO NOT EDIT BELOW THIS LINE
        //
echo "You got a $petsname!<br>"; // announces user got the creature
// now find out which pet was selected
// and list info
// using a switch function
switch ( $petsname )
{
    case caterpie:
        $petinfo = 'A small caterpillar';
        $type = 'bug';
        $attack1 = 'stringshot';
        $attack2 = 'tackle';
        break;
    case charmander:
    $petinfo = 'A firey salamandar';
    $type = 'fire';
    $attack1 = 'growl';
    $attack2 = 'scratch';
    break;
    case ekans:
    $petinfo = 'a vicious snake';
    $type = 'poison';
    $attack1 = 'tackle';
    $attack2 = 'growl';
    break;
  default:
  echo "ERROR - unknown creature - please inform admin. ";
         break;
}
 // add age
$age = rand(1, 3);
//
//get random stats
$str = rand(10, 13);
$def = rand(8, 11);
//
// determine hp/mp by def/str - mp first
//   
if($str == '13')  // set high mp 
{ $mp = rand(13, 15);}
elseif($str == '12')
{ $mp = rand(11, 13);} // set medium mp
else { $mp = rand(9, 11);}
//
// next determine hp
//
if($def == '13')  // set high mp 
{ $hp = rand(13, 15);}
elseif($def == '12')
{ $hp = rand(11, 13);} // set medium mp
else { $hp = rand(9, 11);}
//
// get gender
$gen = rand(1, 2);
if($gen == '1'){$gender = 'female';}
else {$gender = 'male';}
//
// save info to database
//
$con = mysql_connect ("gamelogin.db.6086537.hostedresource.com","gamelogin","----") or die(mysql_error());
$db = mysql_select_db("gamelogin",$con) or die(mysql_error());
$sql = "INSERT INTO `user_pets` (`type`, `petname`, `username`, `age`, `hp`, `mp`, `str`, `def`, `attack1`, `attack2`) 
VALUES ('$type', '$petsname', '$username', '$age', '$hp', '$mp', '$str', '$def', '$attack1', '$attack2')";
mysql_query( $sql ) or die ( mysql_error() );
//
// tell user pet info
//
echo "Your new pet, {$petsname}, is a {$type}-type!<br>";
echo "It is {$age} years old.<br>";
echo "It's abilities are {$attack1} and {$attack2}.<br>";
echo "{$str} is it's strength and {$def} is it's defense.<br>";
echo "It's health is {$hp}, while it has {$mp} move points.<br>";
echo "{$petsname}'s gender is {$gender}.";
//
// setting cookie to go to next page
setcookie(addpetstats, gotpet, time()+60*30);
?>


ignore the ridiculous amounts of notes, i plan to take those all out when it's finished for sure. i know how to do an include, but not how to transfer a variable from one page to another, which is all i can think of.
any suggestions?

This post has been edited by redrin: 04 May 2010 - 09:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: transfer variable from one page to another

#2 Fratyr  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 139
  • Joined: 10-April 08

Re: transfer variable from one page to another

Posted 04 May 2010 - 10:24 AM

i'm basically trying to use the same code sheet for each creature without making new ones.


It's called 'Class' in Object Oriented Programming. :)

Can't help with code, since your explanation is confusing. Try to explain yourself again or write pseudo-code.

Transferring variable VALUE can be accomplished with:
1) post form
2) in session
3) ajax call
Was This Post Helpful? 0
  • +
  • -

#3 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: transfer variable from one page to another

Posted 04 May 2010 - 10:24 AM

Are you Nick?

You've started a session with session_start(), you can put anything into the $_SESSION array and access it on any other page that calls session_start().
Was This Post Helpful? 0
  • +
  • -

#4 redrin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 46
  • Joined: 01-May 10

Re: transfer variable from one page to another

Posted 04 May 2010 - 11:11 AM

View PostRudiVisser, on 04 May 2010 - 09:24 AM, said:

Are you Nick?

You've started a session with session_start(), you can put anything into the $_SESSION array and access it on any other page that calls session_start().

nope. no idea who Nick is. i'm fairly new here/to coding.

i did try this:

<?php
$_SESSION['loggedin'] = logged;
$_SESSION['gotpet'] = gotpet;
$petsname = 'charmander'; 
header( 'Location: test-gotpet.php' ) ;
?>


but it shows up as it being an undefined variable.

View PostFratyr, on 04 May 2010 - 09:24 AM, said:

i'm basically trying to use the same code sheet for each creature without making new ones.


It's called 'Class' in Object Oriented Programming. :)

Can't help with code, since your explanation is confusing. Try to explain yourself again or write pseudo-code.

Transferring variable VALUE can be accomplished with:
1) post form
2) in session
3) ajax call

classes are something i've just barely begun to get into, unfortunately.

i tried the session thing, but it didn't seem to work. it's quite possible i'm just screwing up, though.

ajax is something i've looked at, but i don't think i'm quite ready for.
Was This Post Helpful? 0
  • +
  • -

#5 Fratyr  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 139
  • Joined: 10-April 08

Re: transfer variable from one page to another

Posted 04 May 2010 - 11:22 AM

Find me in Skype {REMOVED} or icq {REMOVED}
will help ya
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: transfer variable from one page to another

Posted 04 May 2010 - 11:24 AM

Fratyr Don't do that. When someone comes to this community looking for help don't invite them somewhere else for help. Keep it in the forums otherwise it removes the chance for someone to learn something from the solution found.
Was This Post Helpful? 0
  • +
  • -

#7 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: transfer variable from one page to another

Posted 04 May 2010 - 12:04 PM

$_SESSION['loggedin'] = logged;
$_SESSION['gotpet'] = gotpet;


They are undefined... You're trying to use undefined constants.

I assume you mean to put strings in there..???

$_SESSION['loggedin'] = "logged";
$_SESSION['gotpet'] = "gotpet";


Basics ;)
Was This Post Helpful? 0
  • +
  • -

#8 Fratyr  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 139
  • Joined: 10-April 08

Re: transfer variable from one page to another

Posted 04 May 2010 - 12:07 PM

Alright, for playing with sessions, try this:

assume this is index.php file:
<?php
session_start();
// ALWAYS ENCLOSE STRING IN QUOTES SINGLE/DOUBLE
$_SESSION['loggedin'] = "logged";
$_SESSION['gotpet'] = "gotpet";
$_SESSION['petsname'] = "charmander"; 
header( 'Location: test-gotpet.php' ) ;
?>



This is your test-gotpet.php:
<?php
session_start();
echo $_SESSION['loggedin'] . '<br />';
echo $_SESSION['gotpet'] . '<br />';
echo $_SESSION['petsname'] . '<br />';
?>


Was This Post Helpful? 0
  • +
  • -

#9 redrin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 46
  • Joined: 01-May 10

Re: transfer variable from one page to another

Posted 04 May 2010 - 12:37 PM

thank you guys, really appreciate.

and yeah, it's basic, but i'm still a newb, so i tend to screw up basics a lot. :dontgetit:

Fratyr: it echos the information, but how do i make the petsname assign to the $petsname variable in the gotpet.php? it still says this:

Notice: Undefined variable: petsname in D:\Hosting\6086537\html\ferrin\test-gotpet.php on line 47

This post has been edited by redrin: 04 May 2010 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#10 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: transfer variable from one page to another

Posted 04 May 2010 - 12:40 PM

Hehe not a problem - Did you get everything working alright now then?
Was This Post Helpful? 0
  • +
  • -

#11 redrin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 46
  • Joined: 01-May 10

Re: transfer variable from one page to another

Posted 04 May 2010 - 12:51 PM

almost, i'm still trying to figure out how to store the session as a variable.

i thought this would work, but it's not:

$_SESSION['petsname'] = '$petsname';

it's got to be something close to that. :dozingoff:

edit: nevermind, i got it. had it backward. XD
is there a way to put the
<?php
session_start();
// ALWAYS ENCLOSE STRING IN QUOTES SINGLE/DOUBLE
$_SESSION['loggedin'] = "logged";
$_SESSION['gotpet'] = "gotpet";
$_SESSION['petsname'] = "charmander"; 
header( 'Location: test-gotpet.php' ) ;
?>



into a url? if i could, then i could surpass the needing a page for each creature. they all randomly appear on the index.php page upon refresh, so if i could insert the code into the link for each creature it would save a LOT of space and work.

This post has been edited by redrin: 04 May 2010 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#12 Fratyr  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 139
  • Joined: 10-April 08

Re: transfer variable from one page to another

Posted 04 May 2010 - 01:36 PM

I think we got to the point, where I need to send you to the Google, The God Of Search

But here's some tips:

$_SESSION['petsname'] = '$petsname';
wrong, wrong, wrong...

take a look, and remember:

$variable_str = 'im a string'; // this is a string enclosed in quotes
$variable_int = 10; // this is an int, and must be without any quotes
$petsname = 'charmander';

$_SESSION['some_name_of_session_key'] = $variable_str; // outputs: im a string
$_SESSION['some_name_of_session_key'] = $variable_int; // outputs: 10
$_SESSION['some_name_of_session_key'] = '$petsname'; // outputs: $petsname
$_SESSION['some_name_of_session_key'] = $petsname; // outputs: charmander

Hope you will finally understand the real need of quotes. If you enclose your variable with a single quotes, it will NOT be parsed as variable and It's value will not be extracted/recognized as variable, either as string, if you put variable in double-quotes, your variable will be extracted/recognized as variable.
Was This Post Helpful? 0
  • +
  • -

#13 redrin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 46
  • Joined: 01-May 10

Re: transfer variable from one page to another

Posted 04 May 2010 - 02:20 PM

funny enough i've learned everything i know from googling to death. :sweatdrop: i do tend to switch things on accident or replace " with ' quite a bit, but i'm learning. i get the general idea of things, i think.

i'm still not sure how to put the code in the link, though.

basically, each random creature is accessible via a link you click (you click on their picture) and it should automatically put in the session code and petsname variable, and redirect with said info to the gotpet page.

This post has been edited by redrin: 04 May 2010 - 02:21 PM

Was This Post Helpful? 0
  • +
  • -

#14 Fratyr  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 139
  • Joined: 10-April 08

Re: transfer variable from one page to another

Posted 05 May 2010 - 12:45 AM

use $_GET global array.

<?php
session_start();

$petsname = 'charmander';

$_SESSION['loggedin'] = "logged";
$_SESSION['gotpet'] = "gotpet";
$_SESSION['petsname'] = $petsname; 
?>

<!-- You are on index.php somewhere
And this is your pokemon's image:
 -->
<a href="test-gotpet.php?loggedin=<?php echo $_SESSION['loggedin'] ?>&gotpet=<?php echo $_SESSION['gotpet'] ?>&petsname=<?php echo $_SESSION['petsname'] ?>"><img src="images/charmander.jpg" alt="" width="100" height="100" border="0" /></a>



Alright, the href is unsecured and not formatted, which is a bad way of coding, but it should give you a knowledge, how to use information on another page by clicking on the image

test-gotpet.php
<?php
session_start();

echo 'Hello, Guest<br />';

echo 'You\'ve catched a pokemon: ' . $_GET['petsname'];

echo 'Your session keys is: ' . $_GET['logged'] . ' and ' . $_GET['gotpet'];



Try it. You don't actually need a session, if you plan to transfer information by link clicking.

$_GET is an array that shows everything after filename.php?

like filename.php?file=name1&pokemon=charmander&age=31

on filename.php you can extract this data by using this code:
echo $_GET['file']; // outputs: name1
echo $_GET['pokemon']; // outputs: charmander
echo $_GET['age']; // outputs: 31

This post has been edited by Fratyr: 05 May 2010 - 12:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1