Jump to content

Sharing A Variable Between Functions


mallen

Recommended Posts

Is there a way I can use a variable from a function and use it in aother function?

 

function get_folders () {
if ($handle = opendir('products')) {
$blacklist = array('.','..','products', 'index.php');
while (false !== ($file = readdir($handle))) {
 if (!in_array($file, $blacklist)) {
echo '<a href=/products_list.php?folder=' . $file . '&'. 'pdf=' . '/>'. strtoupper($file) . '</a>'. '<br/>';

 }
}
closedir($handle);
}
}
function get_products() {
//if ($handle = opendir('products/category1/product1')) { <<Remove this
if ($handle = opendir($file)) {  <<try this
$blacklist = array('.','..','index.php');
while (false !== ($product = readdir($handle))) {
 if (!in_array($product, $blacklist)) {
echo '<a href=/products_list2.php?folder=' . $product . '&'. 'pdf=' . '/>'. strtoupper($product) . '</a>'. '<br/>';

 }
}
closedir($handle);
}
}

Link to comment
https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/
Share on other sites

Or pass it in by reference (then it doesn't need to be returned).

 

However the answer he might be interested in is no - one cannot get a variable from another function without modifying the other function (e.g. by making it's variables global which might cause threading problems or passing in/returning variables).

 

The variable scope link above probably explains everything.

This is what Ihave so far. I am listing folders and files. Yes I know a database is a better way. My folder structure is products/category1/product1/product2/category2/product1/product2 and so on...

On the first page it lists the categories.

Then click on them and takes you to a second page to list all products for that category. But all I can do is hard code it for a category. I can't get it to assign a value to $curf

//list of functions
global $fld;
function get_folders ($fld) { //display the main cat folders
if ($handle = opendir($fld)) { //open products
$blacklist = array('.','..','products', 'index.php');
while (false !== ($file = readdir($handle))) {
 if (!in_array($file, $blacklist)) {
echo '<a href=/products_list.php?folder=' . $file . '&'. 'pdf=' . '/>'. strtoupper($file) . '</a>'. '<br/>';

 }
}

closedir($handle);

}
}

function get_cur_sub () {
global $curf;
if (!empty ($_GET['folder'])) {

$curf = $_GET['folder'];

} else {
 $curf = 'somevalue';

}
}

function get_sub_folders ($curf) {

if ($handle2 = opendir('products/category1' . $curf)) { //need to change hard coded category1 to a variable
$blacklist2 = array('.','..','products','index.php');
while (false !== ($sub = readdir($handle2))) {
 if (!in_array($sub, $blacklist2)) {
echo '<a href=/detail_page.php?folder=' . $sub . '&'. 'pdf=' . '/>'. basename(strtoupper($sub)) . '</a>'. '<br/>';
var_dump($sub);
 }
}

closedir($handle2);

}
}
//pages:
On home page:
get_folders ('products'); //get category folder

On the products_list.php page:
echo get_sub_folders($curf);

On the detail_page.php:
get_sub_folders ($curf);

The biggest issue with this thread and the last one is we don't know, and I don't think you have defined, what output you want after you click on a category? For example, your latest posted code in this thread is trying to put an &pdf=$file on the end of the 'folder'/category links. Why would you do that? The categories don't have any information about any pdf files.

 

When you click on a category, what do you want to see in front of you? A) Just a list of products, as links to a details page and the details page is where the images/pdf links would be at, or B) A list of products, with the images and pdf links next/under each product name?

After I get the proper list of folders to display I will work on the display details such as the PDF files and images.

On the index page it lists all the categories as links.

Category 1

Category 2

Category 3

 

Then if you click on one it takes you to products_list.php:

It lists:

Product 1

Product 2

 

This is the way I want it but its hard coded for category1. I need it to know what category I clicked. This is where I was trying to assigned $curf (current folder)

<?php
$root = 'products'; // the root folder containing the category/product folder tree

// list categories -
$categories = glob("$root/*",GLOB_ONLYDIR);
if(empty($categories)){
   echo "No product categories found.";
} else {
   echo "Choose a category -<br />";
   foreach($categories as $category){
       $category = basename($category);
       echo "<a href='?cat=$category'>$category</a><br />";
   }
}

// if a category is selected, list the products
$cat = isset($_GET['cat']) ? trim($_GET['cat']) : '';
if($cat != ''){
   echo "Choose a product under - $cat<br />";
   $products = glob("$root/$cat/*",GLOB_ONLYDIR);
   if(empty($products)){
       echo "No products found.";
   } else {
       foreach($products as $product){
          // code to use the $product folder name here...
       }
   }
}

Thanks that worked and it was easy to follow. I was able to modify it so it would spread out over individual pages. Now I am try to adapt this to display the images for each product. This is what I have tried but getting NULL for value of images.

// if a product is selected, list the images
$prod = isset($_GET['prod']) ? trim($_GET['prod']) : '';
if($prod != ''){

    $images = glob("$root/$cat/$prod/*.jpg",GLOB_ONLYDIR);
    if(empty($images)){
		    echo "No images found.";
    } else {
		    foreach($images as $image){
 $image = basename($image);
			    echo basename($image) . '<br/>';

		    }
    }
 var_dump($image);
}

How can I do a preg_match() to look for images only with "LARGE" in the file name?

$prod = isset($_GET['prod']) ? trim($_GET['prod']) : '';
if($prod != ''){
 $LARGE = 'LARGE';
  preg_match("/$LARGE/", $image);
    $images = glob("$root/$cat/$prod/*.{.jpg,JPG}", GLOB_BRACE);
    if(empty($images)){
		    echo "No large images found.";
    } else {
		    foreach($images as $image){

			    //echo basename($image) . '<br/>';
 echo 'Large image is'. '<img src="'. $root . '/'. basename($cat) . '/' .basename($prod).'/' .basename($image). '"  /><br/>';
		  }
    }
 //var_dump($image);
}

  Quote
How can I do a preg_match() to look for images only with "LARGE" in the file name?

 

You wouldn't. You would change the * wild-card character in the glob() search pattern to match the files you are interested in.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.