Dynically Include Variables from another page

I have a Web page document that I am building Dynamically, depending on where you are in the country.  The document is very long and has different parts depending on where you are in the country.  I have between 10 -20 different sub documents to include in the main document depending on where you are located.

I want to keep all of the sub-documents in another file and turn each sub-document into a variable.  So in my sub-documents.php page I have
<?php
$ks = 'BLAHH BLAH BLAH';

$ny = 'BLAHH BLAH BLAH';

$tx = 'BLAHH BLAH BLAH';
etc...
?>

Open in new window


In my main document page I want to have:
<?php
include('sub-documents.php');

if($state == 'Kansas') {
$state = $ks(from sub-document page)
}
if($state == 'New York{
$state = $ny(from sub-document page)
}
if($state == 'Texas') {
$state = $tx(from sub-document page)
}

$html ='Hello and welcome.  I see you are from '.$state.'";

Open in new window

How do I make the variable within the sub-documents.php page available for use in the documents.php page
LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
It will be in the same scope and namespace that was in effect at the time of the include() function call.  You may want to change the values for those variables so you can illustrate the process in way that gives meaningful results.
0
Ray PaseurCommented:
This man page describes the process.
http://php.net/manual/en/function.include.php

Here's an example.  First, the "sub" script that gets brought in via include()
<?php // demo/temp_rgranlund_sub.php
error_reporting(E_ALL);
$abc = 'ABC';
$xyz = 'XYZ';

Open in new window

Next, the script that uses the include() statement.
<?php // demo/temp_rgranlund_main.php
error_reporting(E_ALL);

if (empty($abc)) echo "<br>ABC IS EMPTY BEFORE THE INCLUDE() STATEMENT";

// ASSIGN A VARIABLE
$xyz = "<br>THIS IS THE ORIGINAL XYZ - IT WILL GET OVERWRITTEN BY THE VARIABLE IN THE INCLUDE()";
echo $xyz;

// BRING IN A SCRIPT
include('temp_rgranlund_sub.php');

echo "<br>$abc $xyz";

Open in new window

0
rgranlundAuthor Commented:
When I do this part:
// BRING IN A SCRIPT
include('temp_rgranlund_sub.php');

echo "<br>$abc $xyz";

I get the warning: Notice: Undefined variable: abc
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Ray PaseurCommented:
Please post the EXACT scripts as you have them now.  Also, it may seem like it's a small thing, but a Notice in PHP is not the same as a Warning.  These are terms of art and have different meanings.
0
rgranlundAuthor Commented:
sub-document.php
<?php
$toca = '<div style="page-break-before:always;"></div>
A.	It is hereby agreed that for purposes of the coverage provided by this endorsement, SECTION I – COVERAGES is amended to include the following:  
ROADSIDE ASSISTANCE <br /><br />
Coverage<br /><br />
If a covered loss occurs, we will provide and pay for ground transportation for you and the insured bicycle and one (1) guest from the point of loss to any destination within 50 miles from the point of the covered loss.  We will only pay for your cost of ground transportation three (3) times in one policy year.    
What We Pay <br /><br />
We will pay for all charges for ground transportation through the use of StarrCYCLE/SPOKE Roadside Assistance for you and the insured bicycle and one (1) guest from the point of loss to any destination within 50 miles from the point of the covered loss.  
If for any reason we are not able to arrange service through StarrCYCLE/SPOKE Roadside Assistance, we will reimburse you for ground transportation for an amount not to exceed $50. 
No deductible will apply to the coverage provided by this endorsement. <br /><br />
Exclusions<br /><br />
We will not pay for the cost of ground transportation: <br /><br /> 
1.	outside of the United States of America;<br /><br />
2.	to any destination greater than 50 miles from the point of loss; and/or  <br /><br />
3.	if you are participating in any event that provides transportation assistance. <br /><br /> 
Claims<br /><br />
If you require ground transportation as the result of a covered loss, please call: <br /><br /> 
	StarrCYCLE/SPOKE Roadside Assistance HOT LINE 1 (888) 262-2699<br /><br />
All other terms and conditions of this policy remain unchanged.  
</div> ';
?>

Open in new window


main-page.php
<?php
include( 'forms/documents.php' );
echo $toca; (THIS WORKS)

$check_alt  = 	"SELECT meta_value, meta_key
					FROM wp_woocommerce_order_itemmeta
					WHERE meta_key = 'State'
					AND order_item_id = :order_id ";
 
		$checking_alt = $pdo->prepare($check_alt);		
		$checking_alt->execute(array(':order_id' => $order_id)) or die(print_r($checking_alt -> errorInfo()));
		$res_alt = $checking_alt->fetchAll();	
				
				if ($res_alt) {
					
					foreach ($res_alt as $rows)  {
						if($rows->meta_key == "Statel") { 
							$st = $rows->meta_value;
						}
if ( $st == "10") {
	$toc = $toca;
} else {
	$toc = '';
}

echo $toc;
?>

Open in new window


The Warning comes at main -page.php on line 21.
0
Ray PaseurCommented:
That cannot possibly be an EXACT copy of the scripts, unless you have never run them.  The main-page script will fail for a parse error on line 3.  There's a reason why we ask for EXACT copies - because we cannot help you if we get extraneous information or abstractions that hide the true problems.

Please post the EXACT scripts as you have them now.
0
rgranlundAuthor Commented:
It's really really big.  I just want to include variable from another page.
0
Ray PaseurCommented:
The design pattern shown here teaches exactly how to do it.  I expect that the reason your "really really big" script failed is a logic error that leads to a variable not getting set.  That is one of the reasons we, as an industry, are moving away from big complicated scripts to object-oriented programming, with smaller, simpler scripts.  It's very difficult to debug big scripts and almost impossible to test them correctly.  And without rigid enforcement of programming standards, it's really easy to get tripped up by the logic errors.

Let me try to show you an example of the value of programming standards.  The two standards illustrated here are (1) indent each control structure 4 spaces and (2) line up braces for each control structure.  Here is the original "main" script from this post.  Look at it and see if you can tell why it's going to fail.

<?php
include( 'forms/documents.php' );
echo $toca; (THIS WORKS)

$check_alt  = 	"SELECT meta_value, meta_key
					FROM wp_woocommerce_order_itemmeta
					WHERE meta_key = 'State'
					AND order_item_id = :order_id ";
 
		$checking_alt = $pdo->prepare($check_alt);		
		$checking_alt->execute(array(':order_id' => $order_id)) or die(print_r($checking_alt -> errorInfo()));
		$res_alt = $checking_alt->fetchAll();	
				
				if ($res_alt) {
					
					foreach ($res_alt as $rows)  {
						if($rows->meta_key == "Statel") { 
							$st = $rows->meta_value;
						}
if ( $st == "10") {
	$toc = $toca;
} else {
	$toc = '';
}

echo $toc;
?>

Open in new window

Now look at the same code, but with the programming standards applied.  It's easy to see in an instant that the control structures are incomplete, leading to a parse error or logic failure at run time.

<?php // demo/temp_rgranlund_main.php
error_reporting(E_ALL);

include( 'forms/documents.php' );
echo $toca; // (THIS WORKS)

$check_alt 
= 
"
SELECT meta_value, meta_key
FROM wp_woocommerce_order_itemmeta
WHERE meta_key = 'State'
AND order_item_id = :order_id
"
;

try
{
    $checking_alt = $pdo->prepare($check_alt);
    $checking_alt->execute(array(':order_id' => $order_id));
    $res_alt = $checking_alt->fetchAll();
}
catch(PDOException $e)
{
    trigger error($e->getMessage(), E_USER_ERROR);
}

if ($res_alt)
{
    foreach ($res_alt as $rows)
    {
        if($rows->meta_key == "Statel") // THIS CANNOT OCCUR BECAUSE OF THE QUERY
        {
            $st = $rows->meta_value;
        }
        if ( $st == "10")
        {
            $toc = $toca;
        }
        else
        {
            $toc = '';
        }

        echo $toc;
?>

Open in new window

Executive summary: I would recommend you adopt a coding standard and adhere to it.  You will not have "mystery problems" like this missing variable if you're meticulous about neatness and adherence to standards.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.