Jump to content

STUMPED! plz help


aut0tek

Recommended Posts

Okay, I have 2 .php's one includes the other... the First file has variables that need to be utilized in the second... Here's how it goes:

 

CLASS class{

 

function myfunction()

 

$variables = here

include file2.php

 

 

//end of function

// end of class

 

 

SECOND FILE:

//I want to be able to access the vars in class's myfunction. These variables were set immediately before my execution, and I would love to have them aboard! lol

 

 

 

//thanks.

Link to comment
Share on other sites

I think that maybe my 2nd file has a few includes that cause it to no longer be inside the class/function of the first file... so in other words... how do I get the variables from OUSTIDE of the class and/or function?

Link to comment
Share on other sites

it returns the variables perfectly for other functions in the class... They can just get them by:

 

function($name, $name2, $name3) //etc etc etc

 

However, outside of that class, they are not seen no matter how hard I try. lol. :-(

 

Link to comment
Share on other sites

They are! That's what baffled me...

 

1st file:

class myclass{

function myfunction() {

        $my_email, $my_name, $my_user, $my_pw;

$my_name = $row->name;

}

}

 

2nd file:

 

$custom_lastname = $my_name;

 

 

However, when called, the variable is null. :-(

 

Link to comment
Share on other sites

sry... at the top of function, I didn't cut/paste, I typed... and I missed the important part... function GLOBAL then the vars. ;-)

 

Hey... this may be something I need to address... I am calling the other php file by it's FULL URL.... not /home/mystuff/etcetcetc

 

Think that may cause it? if I call it by: require_once '/home/mystuff/'

it gives me a db error (the script within is accessing a SQL db.)

Link to comment
Share on other sites

You would need to return the variable then call it. You won't be able to assign variables AND include something in the same function unless you put everything into an array and break it up when assigned.

 

CLASS class{

function my_var_function(){
$variables = here;
return $variables;
}

function my_include(){
// use variables from other function
$vars = $this->my_var_function();
include file2.php;
}
}

 

now you can assign the variables in your scrip

 

<?php
$class = new class;
// assign your vars
$vars = $class->my_var_function();
// now include the page
$class ->my_include();
?>

 

You can't assign a function result to a variable AND have it echo out something. Do one or the other.

 

Ray

 

Link to comment
Share on other sites

Okay, so now that I've got that, how do I access the vars in the 2nd file? like... let's say the 1st file had a variable named $name...

 

in the 2nd file I wanna make a variable with the same string inside it without knowing the string... soo...

 

$class->name

 

?

Link to comment
Share on other sites

If your first file is nothing but a class(which it should be) you can get the result from the class on any page you include it in.

 

The variables in the class can be named anything you like when you call a function in the class you assign it to a variable in you main script. so if inside the function my_var_function() you assign a value to $name. You don't call $name you call the function that assigned it.

Example:

file1.php = class

file2.php = page to be displayed

 

so file1.php you leave alone once all your class is complete. You call the class from any other page

file2.php

<?php
include('file1.php');  // This is the class to be included
$class = new class;
$name = $class-> my_var_function(); 
?>

 

Ray 

 

 

 

Link to comment
Share on other sites

BTW thanks for helping me out, Ray. I been banging my head against a wall with this. I am NOT a php guy, however, I'm getting the hang of the simple stuff.

 

Soooo... let's say there are 5 variables within the my_var_function... First things first, how do u return them? like this:

return array($name, $phone, $zip, $address, $etc) ?

 

secondly, in the 2nd file I can just call them like this?

 

$name = $class->my_var_function();

$phone = $class->my_var_function();

 

etc? that seems too easy. ;-)

Link to comment
Share on other sites

well almost :)

 

if you know which one is which you would do this

 

$vars = $class->my_var_function();  // get the array from the class and assign it to $vars
$name = $vars[0];  //name is the first value in the array "0"
$phone = $vars[1];  // phone is the second value in the array "1"

 

Ray

Link to comment
Share on other sites

eew, okay btw, I have to include the 2nd file from the 1st, it can't be the other way around... Here's what's going on, in a nutshell...

 

User registers in the CMS, which the CMS does some hash/salt jazz and throws it into a db... I need to grab those same variables it's using, and plant them into my 2nd php script so that it can do some similar, but not equal stuff, and throw it into IT'S sql db... So, basically, From page 1 (all of page 1's stuff works effortlessly, but that's because I didn't write it LOL) and then call page 2, and make it sing the same tune. :-)  I think they call this a "bridge" ?

Link to comment
Share on other sites

Well the easiest thing to do in this case would be to use sessions.

 

First you would have to start your session. **important** You need to put session_start() as the first line in the pages you want to use the sessions

 

so on the first page

<?php
session_start();
$_SESSION['name'] = $name;
$_SESSION['phone'] = $phone;
?>

 

Now as long as you have not skipped any session_starts you can get those variables from the second page

<?php
session_start()
$name = $_SESSION['name'];
$phone = $_SESSION['phone'];
?>

 

Now as long as you keep putting session_start() at the top of your pages, those values will be available to you for the users entire stay at your site :)

 

Ray

Link to comment
Share on other sites

echo the session

 

either

print_r($_SESSION);

Will return the array, sometimes a little hard to read

 

or loop through it

foreach($_SESSION as $key => $value){
echo "SESSION $key has value of $value<br>";
}

 

Ray

Link to comment
Share on other sites

yes session_start() acts like the start, continue whatever :) as long as you keep putting it at the top of your pages the values will be available throughout your pages whether first or 20th.

 

Ray

Link to comment
Share on other sites

hmm... interestingly enough... virtuemart's ps_shopper.php uses session variables but no start up top... I am currently reading on how sessions work. lol. it must be the auto-start deal??? If so, how do I get it to pass over to my php (it is NOT saving the session variables. :-(

 

Link to comment
Share on other sites

Bah. :-( No matter how hard I try, viz the barebones function/class stuff, or the session stuff, I'll never get it. :-( it's been 3 days now, I need to just hire a freelancer I suppose. :-(

 

Thanks anyways, guys. I think I give up on this one.

Link to comment
Share on other sites

would be glad to help you out. I am leaving the office in about 20 minutes but you can catch me on AIM tomorrow from 8:30am - 4:30pm EST. My screen name is craygo69.

 

I would just need the code you are using for the pages.

 

Ray

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.