Web Design Forum: One for your PHP heads out there - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

One for your PHP heads out there Helpz on code! Rate Topic: -----

#1 User is offline   bobbymooreok 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 29
  • Joined: 11-December 07
  • Reputation: 0
  • Location:kingston, surrey
  • Experience:Beginner
  • Area of Expertise:Web Designer

  Posted 16 December 2008 - 06:09 PM

Hey guys,

Basically I am trying to open to merge two lots of open source code.

The first is a tool called shadow box and the other is some php display directory. Please view www.feltham-bees.co.uk/photos/pictures.php to have a gander!

My first issue was that the shadowbox was appearing for all links including directories. I have now fixed that but have two folders appear in the directory because of the echo me thinks. I was wondering if any of you chaps could point me in the right direction.

Always much love

Bobby


Quote

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="../shadowbox/style.css">
<script type="text/javascript" src="../src/adapter/shadowbox-base.js"></script>
<script type="text/javascript" src="../src/shadowbox.js"></script>
<script type="text/javascript">

Shadowbox.loadSkin('classic', '../src/skin');
Shadowbox.loadLanguage('en', '../src/lang');
Shadowbox.loadPlayer(['flv', 'html', 'iframe', 'img', 'qt', 'swf', 'wmp'], '../src/player');

window.onload = function(){

Shadowbox.init();

};

</script>


<!--style type="text/css">
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
color: #333333;
text-decoration: none;
padding: 20px;
}
img {
border: 0px;
}
a {
color: #333333;
text-decoration: none;
}
a:hover {
color: #0066FF;
}


</style>
-->
</head>
<body>
<div align="center"><img src="../Images/titles/photoalbum.jpg" align="middle" /><br /></div>
<?



// install variables

$host = "http://www.feltham-bees.co.uk/"; // the folder where index.php is located
// path for folder, file, buttons(back and home) images
$img_back="../Images/back.gif";
$img_folder="../Images/folder.gif";
$img_file="../Images/file.gif";
$img_home="../Images/home.gif";

// end of install variables


// returns the extension of a file
function strip_ext($name)
{
$ext = substr($name, strlen($ext)-4, 4);
if(strpos($ext,'.') === false) // if we have a folder element
{
return " "; // we return a string of space characters for later sort,
// so that the folder items remain on the first positions
}
return $ext; // if we have a file we return the extension - .gif, .jpg, etc.
}



// returns the files from the $path and returns them in an array
function getFiles($path) {

$files = array();
$fileNames = array();
$i = 0;
// build
if (is_dir($path)) {
if ($dh = opendir($path)) {
while (($file = readdir($dh)) !== false) {
if (($file == ".") || ($file == "..")) continue;
$fullpath = $path . "/" . $file;
//$fkey = strtolower($file);
$fkey = $file;
while (array_key_exists($fkey,$fileNames)) $fkey .= " ";
$a = stat($fullpath);
$files[$fkey]['size'] = $a['size'];
if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-";
else if ($a['size'] > 1024 && $a['size'] <= 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K";
else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb";
else $files[$fkey]['sizetext'] = $a['size'] . " bytes";
$files[$fkey]['name'] = $file;
$e = strip_ext($file); // $e is the extension - for example, .gif
$files[$fkey]['type'] = filetype($fullpath); // file, dir, etc
$k=$e.$file; // we use this string for sorting the array elements by extension and filename;
$fileNames[$i++] = $k;
}
closedir($dh);
} else die ("Cannot open directory: $path");
} else die ("Path is not a directory: $path");
sort($fileNames,SORT_STRING); // sorting
$sortedFiles = array();
$i = 0;
foreach($fileNames as $f) {
$f = substr($f, 4, strlen($f)-4); // we remove the extension we added in front of the filename for sorting
if($files[$f]['name'] !='') $sortedFiles[$i++] = $files[$f];
}// ends the foreach where we build the final sorted array
return $sortedFiles;
}


// folder navigation code
$startdir = "./";
if(isset($_GET['dir'])) {
$prev = $_GET['dir'];
$folder = $_GET['dir'];
echo "<a href=\"java script:history.go(-1)\"><img src=\"$img_back\"></a> <a href=\"$host\"><img src=\"$img_home\"></a> <br/><br/>";
} else { $folder = $startdir;} //$prev='';}
// end folder navigation code


$files = getFiles($folder);

foreach ($files as $file) {
if(strip_ext($file[name])!='.php'){
$image = $img_file;
if($file[type]=='dir') {
$image = $img_folder;
$cmd='?dir='.$prev.$file[name].'/';
echo "<a href=\"$cmd\" title=\"$file[type], $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>";
}// if the element is a directory
else $cmd=$prev.$file[name];
echo "<a rel=\"shadowbox\" href=\"$cmd\" title=\"$file[type], $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>";
}//if strip_ext
}//foreach

$indexCount = count($startdir);
Print ("$indexCount files<br>\n");
?>

</body>
</html>

0

#2 User is offline   Connetu_C 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 425
  • Joined: 12-December 08
  • Reputation: 25
  • Gender:Male
  • Location:London, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 16 December 2008 - 06:53 PM

You're missing braces here:
}// if the element is a directory
else $cmd=$prev.$file[name];
echo "<a rel=\"shadowbox\" href=\"$cmd\" title=\"$file[type], $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>";
}//if strip_ext

should be:
}// if the element is a directory
else {
  $cmd=$prev.$file[name];
  echo "<a rel=\"shadowbox\" href=\"$cmd\" title=\"$file[type], $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>";
}
}//if strip_ext

with those braces after "else" and before the "//if" line. If you don't want to display directories at all, this bit needs changing:
if($file[type]=='dir') {
$image = $img_folder;
$cmd='?dir='.$prev.$file[name].'/';
echo "<a href=\"$cmd\" title=\"$file[type], $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>";
}// if the element is a directory

to:
if($file[type]=='dir') {
	continue;
}

so directories are skipped over and not written to the output at all.
0

#3 User is offline   bobbymooreok 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 29
  • Joined: 11-December 07
  • Reputation: 0
  • Location:kingston, surrey
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 16 December 2008 - 08:08 PM

Many thanks for your reply connectu but unfortunatly I need the directory to display at least once. I am using this as a photo album so the test folder will eventually be the subjects of the photos.

Your reply did spark the ol' noddle though and thought the only other file would be jpg and came up with the following. Let me know your ideas

EDIT: only been playing around with php in the last week or so, so bare with me :(

$files = getFiles($folder); 

foreach ($files as $file) { 
	if(strip_ext($file[name])!='.php'){ 
		$image = $img_file; 
		if($file[type]=='dir') { 
			$image = $img_folder; 
			$cmd='?dir='.$prev.$file[name].'/';
			echo "<a href=\"$cmd\" title=\"$file[type],  $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>";
		}// if the element is a directory 
			else $cmd=$prev.$file[name];
			if($file[type]=='jpg') {
			echo "<a rel=\"shadowbox\" href=\"$cmd\" title=\"$file[type],  $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>";
			}
			else echo "file not supported <br>";
	}//if strip_ext 
}//foreach

0

#4 User is offline   Connetu_C 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 425
  • Joined: 12-December 08
  • Reputation: 25
  • Gender:Male
  • Location:London, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 16 December 2008 - 08:45 PM

View Postbobbymooreok, on Dec 16 2008, 20:08, said:

Many thanks for your reply connectu but unfortunatly I need the directory to display at least once.

That's what my first suggestion was---you need to use the braces after else! At the moment you're only executing the "$cmd=$prev.$file[name];" line as the else part, and the echo line is always executed, even if the if statement before it matched. You need to use this section of code:
foreach ($files as $file) {
if(strip_ext($file[name])!='.php'){
$image = $img_file;
if($file[type]=='dir') {
$image = $img_folder;
$cmd='?dir='.$prev.$file[name].'/';
echo "<a href=\"$cmd\" title=\"$file[type], $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>";
}// if the element is a directory
else 
{// *** HERE ***
$cmd=$prev.$file[name];
echo "<a rel=\"shadowbox\" href=\"$cmd\" title=\"$file[type], $file[sizetext]\"><img src=\"$image\" /> $file[name]</a><br/>";
}// *** HERE ***
}//if strip_ext
}//foreach

I've marked the braces you've missed as "*** HERE ***" to help you. This was my first suggestion earlier though.
0

#5 User is offline   bobbymooreok 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 29
  • Joined: 11-December 07
  • Reputation: 0
  • Location:kingston, surrey
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 16 December 2008 - 09:17 PM

Ah ha, you legend.

This reminds me of my contact form, I thought I would have to specify a crazy if statement like
if 1=1
mail(blahblah etc)

for the variables to run but I didn't, they run automaticly. I'm guessing the braces seperate the attribute to run seperatly with the else.

Well thank you sir, not only did I get the answer but I'm also learnded :D

Much Love
Bobby
0

#6 User is offline   Connetu_C 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 425
  • Joined: 12-December 08
  • Reputation: 25
  • Gender:Male
  • Location:London, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 16 December 2008 - 09:48 PM

View Postbobbymooreok, on Dec 16 2008, 21:17, said:

I'm guessing the braces seperate the attribute to run seperatly with the else.

The braces group a block of code together; the code in that block must all run together. The "else" before then applies to the entire block rather than just the following line. Same happens with if:
if(2 < 1)
echo "A";
echo "B";

if(2 < 1) {
echo "A";
echo "B";
}

The first will print just "B", while the second prints nothing. In the first case, the if only applies to the echo "A" line, and not to the echo "B" line. Note I usually put the opening brace on the same line as the if or else, both to save line count and out of habit, but you can also put them on separate lines if you want, like this:
if(2 < 1)
{
  echo "A";
  echo "B";
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users