mallen Posted December 18, 2012 Share Posted December 18, 2012 I am trying to develop a page that will display a list of categories of products. And then when the user clicks on the category it passes to another page that will display a list of products for that category. I have folders for each category and inside the folder is a folder for each product. This way I can just dump files into the folders to make updates. So far I have it displaying the list of category folders I will use as my navigation. I need help getting the second page done. I need it to display the list of products and then a link to an individual page. I have written out the pseudo code what I am trying to do. function get_folders () { //list category folders as list of links 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 .'>'. strtoupper($file) . '</a>'. '<br/>'; } } closedir($handle); } } function display_category () { //display list of products based on category //pseudo code here $file = $newCategory; for each folder name that equals $newCategory { echo = '<a href=/single_product.php?folder=' . $newCategory .'>'. strtoupper($newCategory) . '</a>' ; } } echo get_folders(); Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/ Share on other sites More sharing options...
Muddy_Funster Posted December 18, 2012 Share Posted December 18, 2012 sounds like you want to be using glob() Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400066 Share on other sites More sharing options...
mallen Posted December 18, 2012 Author Share Posted December 18, 2012 Thanks. I looked at glob() and came up with this. But I had to hard code each product folder. I need it to be dynamic. I plan to have it display a thumbnail, list of files to download and list of pdf files for each product. function get_folders () { //list category folders as list of links 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 .'>'. strtoupper($file) . '</a>'. '<br/>'; } } closedir($handle); } } function display_files_jpg () { //display list of jpg images foreach(glob('products/*/product1/*.jpg') as $image) { echo basename($image) . "<br />"; } } function display_files_pdf () { //display list of pdf files //pseudo code here //$file = $newCategory; foreach(glob('products/*/product1/*.pdf') as $pdf) { echo basename($pdf). "<br />"; } } echo display_files_pdf(); echo display_files_jpg(); echo get_folders(); Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400075 Share on other sites More sharing options...
mallen Posted December 18, 2012 Author Share Posted December 18, 2012 (edited) I tried changing just the second funtion as a test using GET thinking since the 'folder' will be passed in the URL but didn't work. I think becuase product is a subfolder of category. function get_folders () { //list category folders as list of links 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 .'>'. strtoupper($file) . '</a>'. '<br/>'; } } closedir($handle); } } function display_files_jpg () { //display list of jpg images foreach(glob('products/*/product1/*.jpg') as $image) { echo basename($image) . "<br />"; } } function display_files_pdf () { //display list of pdf files $dirs = $_GET["folder"]; foreach(glob('dirs', GLOB_ONLYDIR) as $pdf) { echo basename($pdf). "<br />"; } } echo display_files_pdf(); echo display_files_jpg(); echo get_folders(); Edited December 18, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400083 Share on other sites More sharing options...
mallen Posted December 19, 2012 Author Share Posted December 19, 2012 I used var_dump() to check that 'folder' has a value. But I need to figure how to make the folder directory a daynamic value. It doesn't return any images or files. Anyone have ideas how I can accomplish this? Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400319 Share on other sites More sharing options...
NomadicJosh Posted December 19, 2012 Share Posted December 19, 2012 In your original code, which function was working? Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400321 Share on other sites More sharing options...
mallen Posted December 19, 2012 Author Share Posted December 19, 2012 Get_folders() and display_files_jpg() are working. Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400323 Share on other sites More sharing options...
mallen Posted December 19, 2012 Author Share Posted December 19, 2012 When this part runs the value that gets passed in the URL and displays on the page is only "category2" or "category1". It won't display the images and files for each. $dirs = $_GET['folder']; foreach(glob('dirs', GLOB_ONLYDIR) as $pdf) Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400326 Share on other sites More sharing options...
mallen Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) Anyone? I have searched over and over for a solution. All I can find is examples of static directories. Mine has to be based on a variable. Maybe something on the second page that is where "folder = $_GET["folder"] Thanks. Edited December 19, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400388 Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 When this part runs the value that gets passed in the URL and displays on the page is only "category2" or "category1". It won't display the images and files for each. $dirs = $_GET['folder']; foreach(glob('dirs', GLOB_ONLYDIR) as $pdf) Try this: $dirs = $_GET['folder']; foreach(glob($dirs, GLOB_ONLYDIR) as $pdf) You're not using the value of $dirs, you're using the literal string 'dirs' in glob Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400392 Share on other sites More sharing options...
mallen Posted December 19, 2012 Author Share Posted December 19, 2012 Thanks. I had something like that. I tried what you suggested and got "Notice: Undefined index: folder in...." Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400394 Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 (edited) Well, you have to make a GET request to the page to set it. You can fill it with dummy data for now like this $dirs = 'my_desired_folder'; or $_GET['folder'] = 'my_desired_folder'; $dirs = $_GET['folder']; but to work with $_GET, the url would need to contain something like '&folder=something'. Have a look at the manual for $_GET Edited December 19, 2012 by TOA Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400397 Share on other sites More sharing options...
mallen Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) Yes this function echos a URL that includes the variable. Do I have to declare the $dirs ='some_value'; at the top of the page? function get_folders () { //list category folders as list of links if ($handle = opendir('products')) { $blacklist = array('.','..','products', 'index.php'); while (false !== ($file = readdir($handle))) { if (!in_array($file, $blacklist)) { echo '<a href=/DuraGuard/products_list.php?folder=' . $file .'>'. strtoupper($file) . '</a>'. '<br/>'; } } closedir($handle); } } Edited December 19, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400398 Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 Do I have to declare the $dirs ='some_value'; at the top of the page? No, that's to take the place of where you grab the get variable, to mimick that. It's just to find out if the $_GET is coming through. Can you post an entire script? I'm still not entirely sure what you're doing. At the time you call this function, $_GET['folder'] must have a value set. The error is telling you it's not. Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400404 Share on other sites More sharing options...
mallen Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) The PDF function is a change to the way I displayed the images. When I get this working I will change that function too. error_reporting(E_ALL); ini_set("display_errors", 1); function get_folders () { //list category folders as list of links 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 .'>'. strtoupper($file) . '</a>'. '<br/>'; } } closedir($handle); } } function display_files_jpg () { //display list of jpg images foreach(glob('products/*/product2/*.jpg') as $image) { echo basename($image) . "<br />"; } } function display_files_pdf () { //display list of pdf file $dirs = 'my_desired_folder'; $dirs = $_GET['folder']; foreach(glob($dirs, GLOB_ONLYDIR) as $pdf) { echo basename($pdf) . "<br />"; } } echo display_files_pdf(); echo display_files_jpg(); echo get_folders(); Edited December 19, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400407 Share on other sites More sharing options...
TOA Posted December 19, 2012 Share Posted December 19, 2012 What url do you use to access this page? Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400414 Share on other sites More sharing options...
mallen Posted December 19, 2012 Author Share Posted December 19, 2012 ..localhost/index.php to access the page and it displays the links. I click on them and it takes me to /products_list.php?folder=category2 Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400419 Share on other sites More sharing options...
mallen Posted December 19, 2012 Author Share Posted December 19, 2012 Ok I put all the function in a separate include file. On index page I have: get_folders (); On the products_list.php?folder= I have display_files_jpg (); display_files_pdf (); Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400460 Share on other sites More sharing options...
mallen Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) Here are my functions. See I am trying to switch out the$cat variable and also the 'folder' I used the var_dump() and it is showing a value. function get_folders () { //list category folders as list of links 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 .'>'. strtoupper($file) . '</a>'. '<br/>'; } } closedir($handle); } } function display_files_jpg () { //display list of jpg images $cat = $_GET["folder"]; foreach(glob('products/*/$cat/*.jpg') as $image) { echo basename($image) . "<br />"; } var_dump($cat); } function display_files_pdf () { //display list of pdf files $_GET['folder'] = 'some_value'; $dirs = $_GET["folder"]; foreach(glob($dirs, GLOB_ONLYDIR) as $pdf) { echo basename($pdf) . "<br />"; } var_dump($dirs); } Edited December 19, 2012 by mallen Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400466 Share on other sites More sharing options...
Christian F. Posted December 20, 2012 Share Posted December 20, 2012 twistedvengeance: That has nothing at all to do with this thread. Why would you post it? Mallen: It seems as if though you haven't thought through this properly, and thus you're not quite grasping the logic of the problem you want to solve. Programming has to be a conscious exercise, where every choice you make must be well founded in complete understanding of the tools and problems at hand. Thus I would actually recommend that you step a bit back, and try to write down a step-by-step list of what you want the code to do. Be as detailed as you can, but preferably only use a couple of words per item on the list. One verb and one noun is the optimum, constantly asking yourself "what do I need to do what I just wrote". When you can't get any more detailed, have a look at the PHP manual to see if you find a function for what you want to do on each step. If you can't, then chances are that you still can go a bit more detailed on that item. Anyway, your current problem is rooted in mostly two factors: Not properly realizing what happens in these lines: $dirs = 'my_desired_folder'; $dirs = $_GET['folder']; Not handling the missing GET parameter (properly). In the first point you're first setting the $dirs variable to contain "my_desired_folder", but in the very next line you then overwrite it with whatever is in the $_GET['folder'] index. If there is nothing there, you overwrite the previous content with nothing (NULL). Which means that when you execute the glob () function, you're trying to read the contents of the folder '' (empty string). Which, naturally enough, doesn't return anything since the folder doesn't exists. With the second point you need to ask yourself, what happens if $_GET['folder'] isnotset? (Yes, that's a major hint. ) Another hint is that in PHP "not" is !, and if you pursue the PHP manual you should be able to figure out the rest too. Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400566 Share on other sites More sharing options...
mallen Posted December 20, 2012 Author Share Posted December 20, 2012 Thanks Christian. I tried what was suggested in post#12 but still no luck. $_GET['folder'] = 'my_desired_folder'; $dirs = $_GET['folder']; Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400593 Share on other sites More sharing options...
mallen Posted December 20, 2012 Author Share Posted December 20, 2012 Is this close? function display_files_pdf () { //display list of pdf files if (!empty($_GET['folder'])) { $dirs = $_GET['folder']; } else { $dirs = 'my folder'; } foreach(glob($dirs, GLOB_ONLYDIR) as $pdf) { echo basename($pdf) . "<br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400599 Share on other sites More sharing options...
Christian F. Posted December 20, 2012 Share Posted December 20, 2012 (edited) Yep, that's a lot closer. Now you only need to echo the results of the glob () as links. Though, I'd probably fetch the directory name outside of the function, and send it as a parameter. That way you can reuse the function, without having to change it, if you want to use it somewhere else. Or if you decide you need to change the URL/base folder, at some point in the future. Then it'd look like this: if (!empty ($_GET['folder'])) { $dir = $_GET['folder']; } else { $dir = "default folder"; } $dirList = list_directories ($dir); And the function itself would look like this: function list_directories ($directory) { // Initialize the list variable, for string concatenation inside the loop. $list = ''; // Loop through the sub directories of the given directory and build the list. foreach (glob ($directory, GLOB_ONLYDIR) as $dir) { $list .= basename ($dir)."<br />\n"; } // Return the completed list to the calling function. return $list; } As you can see I also changed the name of the function, to more accurately describe what it does. Edited December 20, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400604 Share on other sites More sharing options...
mallen Posted December 20, 2012 Author Share Posted December 20, 2012 Warning: Missing argument 1 for list_directories(), called in /products_list.php on line 16 and defined in functions.php on line 38 Notice: Undefined variable: directory in functions.php on line 43 Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400606 Share on other sites More sharing options...
mallen Posted December 20, 2012 Author Share Posted December 20, 2012 Not sure where I should put thie part of the code. Should it be on the page where I call the functions? if (!empty ($_GET['folder'])) { $dir = $_GET['folder']; } else { $dir = "default folder"; } $dirList = list_directories ($dir); Quote Link to comment https://forums.phpfreaks.com/topic/272135-display-list-of-folders-and-link-to-individual-pages/#findComment-1400620 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.