Jump to content

Display List Of Folders And Link To Individual Pages


mallen

Recommended Posts

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();

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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 by mallen
Link to comment
Share on other sites

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 by mallen
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by TOA
Link to comment
Share on other sites

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 by mallen
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by mallen
Link to comment
Share on other sites

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 by mallen
Link to comment
Share on other sites

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:

  1. Not properly realizing what happens in these lines:
    	$dirs = 'my_desired_folder';
    $dirs = $_GET['folder'];
    


  2. 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. :)

Link to comment
Share on other sites

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 by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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