mallen Posted December 21, 2012 Share Posted December 21, 2012 (edited) 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); } } Edited December 21, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/ Share on other sites More sharing options...
deoiub Posted December 21, 2012 Share Posted December 21, 2012 global $var_name; http://php.net/manual/en/language.variables.scope.php Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1400783 Share on other sites More sharing options...
mds1256 Posted December 21, 2012 Share Posted December 21, 2012 (edited) pass it in as an argument and / or return it from a function e.g. $myVar = 3; function myFunction($myVar) { if($myVar > 1) { //do something } } Edited December 21, 2012 by mds1256 Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1400787 Share on other sites More sharing options...
SofWare Posted December 21, 2012 Share Posted December 21, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1400792 Share on other sites More sharing options...
mallen Posted December 21, 2012 Author Share Posted December 21, 2012 (edited) I was trying something like this but get a message of bool(false) outside of the function I have : global $file; if ($handle= opendir($GLOBALS['file'])) Edited December 21, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1400798 Share on other sites More sharing options...
mallen Posted December 26, 2012 Author Share Posted December 26, 2012 (edited) 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); Edited December 26, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401327 Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2012 Share Posted December 26, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401335 Share on other sites More sharing options...
mallen Posted December 26, 2012 Author Share Posted December 26, 2012 (edited) 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) Edited December 26, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401337 Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2012 Share Posted December 26, 2012 <?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... } } } Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401338 Share on other sites More sharing options...
mallen Posted December 27, 2012 Author Share Posted December 27, 2012 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); } Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401529 Share on other sites More sharing options...
mallen Posted December 27, 2012 Author Share Posted December 27, 2012 Got the images to display with GLOB_BRACE $images = glob("$root/$cat/$prod/*.{jpg,gif,png}", GLOB_BRACE); Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401534 Share on other sites More sharing options...
mallen Posted December 27, 2012 Author Share Posted December 27, 2012 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 Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401611 Share on other sites More sharing options...
PFMaBiSmAd Posted December 27, 2012 Share Posted December 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401622 Share on other sites More sharing options...
mallen Posted December 28, 2012 Author Share Posted December 28, 2012 (edited) I tried both ways and can't get the syntax correct. $images = glob("$root/$cat/$prod/.{LARGE}", GLOB_BRACE); or this $images = glob("$root/$cat/$prod/.LARGE {.jpg,.jpeg}", GLOB_BRACE); Edited December 28, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401852 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2012 Share Posted December 28, 2012 It would probably help if you posted an example of the file name(s) you are trying to match. Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401858 Share on other sites More sharing options...
mallen Posted December 28, 2012 Author Share Posted December 28, 2012 I would like to select only the image with 'LARGE' in the name. 1234.jpg 4567_LARGE.jpg 4567_small.jpg Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401877 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2012 Share Posted December 28, 2012 $images = glob("$root/$cat/$prod/*_LARGE.{jpg,JPG}", GLOB_BRACE); Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401886 Share on other sites More sharing options...
mallen Posted December 28, 2012 Author Share Posted December 28, 2012 Thanks that worked. Quote Link to comment https://forums.phpfreaks.com/topic/272252-sharing-a-variable-between-functions/#findComment-1401895 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.