#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76

    Listing of folders creating links to detail pages


    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 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?
    PHP Code:
    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(); 
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    a simple answer: Don't do this. Use a database to store your categories, products etc.

    Searching through images to determine the categories and products is pretty much the most inefficient and fragile approach I could think of. And where do you store the data, anyway?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    I have something like this that was done in .NET and it needs to be done in PHP. I store the files like this. Each product folder has a large and small image and a few files for download.

    PRODUCTS
    >>Category1
    >product1
    >Product2
    >> Category 2
    >product1
    >product2
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    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.
    Last edited by mallen; December 19th, 2012 at 12:43 PM.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    So? Just insert a variable in place of "product1".

    But this approach still doesn't make sense as I already tried to tell you. Just create a database with some simple tables. This will not only make you get rid of all those file traversing problems but also create a solid and more professional foundation for your program.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    If it was only that easy to replace it with a variable. That's what I am trying to do. I can't get the syntax right. Also I know this whole thing would be easier to have a database and that is how all my other sites are done. Bedsides that its the issue, for other reasons I need it to work this way with folders. The .NET version I have uses folders and all it has to do is list folder names, images, and files. No other requirements.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    I got it to work. I was missing the slash / on line 6. Now I have to work on other values.
    Code:
    function list_directories () {
    if (!empty ($_GET['folder'])) {
                    $dir = 'products/' . $_GET['folder'];
     
    } else {
                    $dir = 'somevalue';
     
    }
    $images = glob($dir . "/*.jpg");
      
    foreach($images as $image)
    {
    echo basename($image);
    }
    }

IMN logo majestic logo threadwatch logo seochat tools logo