Reputation: 1820
I am having a problem where I have a class bm_products_filter
which I am not able to get to see a variable from my index.php file.
I have 2 files: index.php and bm_products_filters.php
I have a query in index.php which I want to echo in bm_products_filters.php
(there are some database functions here (they are working as I can correctly echo the variable in index.php)
$product_count .= "SELECT COUNT(p.products_id) as total
from
" . TABLE_PRODUCTS . " p
left join " . TABLE_SPECIALS . " s
on p.products_id = s.products_id
left join " . TABLE_MANUFACTURERS . " m
on p.manufacturers_id = m.manufacturers_id
join " . TABLE_PRODUCTS_DESCRIPTION . " pd
on p.products_id = pd.products_id
join " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
on p.products_id = p2c.products_id
where
p.products_status = '1'
and pd.language_id = '" . (int) $languages_id . "'
and p2c.categories_id = '" . (int)$current_category_id . "'";
$product_count_query = tep_db_query($product_count);
$product_count_tally = tep_db_fetch_array($product_count_query);
global $test_me;
$test_me = $product_count_tally['total'];
?>
The variable I am trying to echo is $test_me
.
The class bm_products_filter
is called (indirectly I guess) from index.php like this:
<?php echo $oscTemplate->getBlocks('boxes_column_left'); ?>
The getBlocks
function looks at all the PHP files in the folder "boxes" and finds the ones that are for the left column (which bm_products_filters.php is as it is being shown).
Now here is where I am totally wrong for sure but I don't know how to do it.
In bm_products_filters.php I am just trying to do:
echo $test_me;
but I am getting nothing. I thought that declaring it global
might work but that did not work. I think I need to somehow call it with a ->
I really don't understand scope very well though.
Hopefully I gave enough information here? Let me know if it needs more.
UPDATE:
this is what I tried to do but it does not working:
//index.php
$product_count .= "SELECT COUNT(p.products_id) as total
from
" . TABLE_PRODUCTS . " p
left join " . TABLE_SPECIALS . " s
on p.products_id = s.products_id
left join " . TABLE_MANUFACTURERS . " m
on p.manufacturers_id = m.manufacturers_id
join " . TABLE_PRODUCTS_DESCRIPTION . " pd
on p.products_id = pd.products_id
join " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
on p.products_id = p2c.products_id
where
p.products_status = '1'
and pd.language_id = '" . (int) $languages_id . "'
and p2c.categories_id = '" . (int)$current_category_id . "'";
$product_count_query = tep_db_query($product_count);
$product_count_tally = tep_db_fetch_array($product_count_query);
$test_me = $product_count_tally['total'];
function product_filter_count() {
global $test_me;
return $test_me;
}
//bm_products_filter.php
echo product_filter_count();
I am getting a call to undefined function error. if I echo the function in index.php it works. Why isn't it visible from within the other function?
Upvotes: 1
Views: 1915
Reputation: 568
in a lot of languages, if you declare a variable outside of a function/method, they automatically have global scope, allowing them to be accessed by any methods within the same class (java for example)
php does not grant global scope like that, so you have a couple of options.
you can use the global keyword"
<?php
$test_me = "hello";
function test()
{
global $test_me;
}
test();
?>
or you can pass it as a parameter to the function
<?php
$test_me = "hello";
function test($global_var)
{
$local_var = $global_var;
}
test($test_me)
?>
if you have variables you want to use in multiple classes/files, i would put them in a separate file and use an include or require statement then in the files that need them. that way, you don't have to include a bunch of php code you don't need in the new file, just the variables.
Upvotes: 0
Reputation: 4907
You need to add global $test_me;
in whatever function/method that you want to have access to it.
You can give an entire class access to the value at the time of instantiation by adding global $test_me;
to the constructor and setting that value to a property.
You can pass it by reference by adding a &
before the variable when you set it to a property.
class testclass {
public gTestMe;
public function __construct() {
global $test_me;
$this->gTestMe = &$test_me;
}
}
I am actually not 100% sure on the pass-by-reference part. I will test to confirm but it should work. Confirmed It does work.
I don't really recommend using global variables like this as it is not generally good practice. There are many better ways to accomplish similar functionality that are more maintainable.
Upvotes: 1
Reputation: 1163
you should try to pass it in as session variable
or in the link as parameter
.if you use as session variable
just clear
the session value
once you committed the transaction in your final page
.
Upvotes: 0