Forum Moderators: coopster
I tried to add to the bag of tricks jobbie but couldn't. So'll i'll just post this here...
Dynamic Breadcrumbs Function
This is a dead simple dynamic breadcrumbs function. You can feed it a url if you wish (I use this for when someone hit's the 'email this page' link and I want the breadcrumbs to refer to [i]where they came from rather than where they are..) or just call it and it will return breadcrumbs from where you are back to the home page.
function breadcrumbs($path="") {
global $title; // you page should have this var somewhere on it
//
// $excArray should contain all files that should NOT be
// included in your bookmarks.
// Ex. index.php, success.php etc etc
//
$excArray=array(
'index.php',
'pages',
'comment.php',
'bookmark,php',
'success.php',
'comment',
'mailit.php',
'general',
);
if(!$path) {
$dirs=explode('/',$_SERVER['PHP_SELF']);
} else {
$urlArray=parse_url($path);
$dirs=explode('/',$urlArray['path']);
}
//
// Home Page
// Change the title and link text to suit
//
$op.="<a href=\"/\" title=\"Your Title Here\">Home</a> > ";
foreach($dirs as $key => $val) {
//
// the numeric bit here can be removed if you do not have numeric
// directories (I use them instead of dynamic urls)
//
if(!is_numeric($val) && $val!= "" &&!in_array($val,$excArray)) {
$op.=" <a href=\"/$val/\">$val</a> > ";
}
}
//
// Unless you are using numeric directories instead of dynamic urls
// you can just remove these 3 lines
//
if(!is_numeric($val)) {
$op.=$title;
} else $op.="Product List or whatever you need it to say";return $op;
}
Nick
[edited by: jatar_k at 4:56 pm (utc) on Aug. 8, 2003]
Thanks for the great script. I have been needing one of these, as my sight is getting bigger all the time.
I modified your code a little, I hope you don't mind :) . I was having a problem with with incorrect urls being used on pages more than 1 folder deep.
There is probably a better way to do this but I am still in the process of learning PHP.
I have this as a url for a page:
www.blah. com/one/two/three/index.php
Your code would output the following:
Home > one > two > three >
Home points to: www.blah. com -- this is correct
one points to: www.blah. com/one -- this is correct
two points to: www.blah. com/two -- this is not correct it should be www.blah .com/one/two
three points to: www.blah. com/three -- this is not correct it should be www.blah .com/one/two/three
so this is what I did to correct it:
I replaced
$op.=" <a href=\"/$val/\">$val</a> > ";
with
if ($oldval){
$op.=" > <a href=\"/$oldval$val/\">$val</a>";
$oldval.="$val/";
}else{
$op.="<a href=\"/$val/\">$val</a>";
$oldval="$val/";
}
Doing it this way I also removed the trailing '>'.
I am running PHP 4.3.3RC3-dev via Apache 2.0.47 on RH7.3.
Thanks again for the wonderful script,
Tim
P.S.
I've been lurking around for a couple of weeks and finally decided to post. This place has so much awesome information, thank you.
<?php
//
// $excArray should contain all files that should NOT be
// included in your bookmarks.
// Ex. index.php, success.php etc etc
//
$excArray=array(
'index.php',
'index.html',
'listing.php',
);
if(!$path) {
$dirs=explode('/',$_SERVER['PHP_SELF']);
} else {
$urlArray=parse_url($path);
$dirs=explode('/',$urlArray['path']);
}
//
// Home Page
// Change the title and link text to suit
//
$op.="<a href=\"/\">Home</a> > ";
foreach($dirs as $key => $val) {
//
// the numeric bit here can be removed if you do not have numeric
// directories (I use them instead of dynamic urls)
//
if($val!= "" &&!in_array($val,$excArray)) {
if ($oldval){
$op.=" > <a href=\"/$oldval$val/\">$val</a>";
$oldval.="$val/";
}else{
$op.="<a href=\"/$val/\">$val</a>";
$oldval="$val/";
}
}
}
print "$op";
?>
It was exactly what I was looking for! I plan on integrating it with a few forms I have on a site I am building, once I get it working. I seem to be having a problem with the directory that the images are supposed to be copied to. I have changed the permissions of the directory, deleted the directory, and removed the trailing slash and still have not got it to work.
The closest I came was after I removed the trailing slash, and it creates a file with what the directory name is supposed to be.
Any help would be appreciated! I think the one causing problems is the second error.
Here is the error(s) I am getting. I think once I get the directory problem fixed, the rest will take care of themselves.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /vhost/site.com/home/www/html/php/photos/loademup.php on line 21
Warning: Unable to create '/vhost/site.com/home/www/html/photos/': Is a directory in /vhost/site.com/home/www/html/php/photos/loademup.php on line 26
Warning: Unable to move '/tmp/phpDAcwaN' to '/vhost/site.com/home/www/html/photos/' in /vhost/site.com/home/www/html/php/photos/loademup.php on line 26
wussy.jpg did not upload!
Warning: getimagesize: Unable to open '/vhost/site.com/home/www/html/photos/' for reading. in /vhost/site.com/home/www/html/php/photos/loademup.php on line 35
Warning: imagecreatefromjpeg: Unable to open '/vhost/site.com/home/www/html/photos/' for reading in /vhost/site.com/home/www/html/php/photos/loademup.php on line 49
Warning: imagecopyresized(): supplied argument is not a valid Image resource in /vhost/site.com/home/www/html/php/photos/loademup.php on line 51
Warning: imagejpeg: unable to open '/vhost/site.com/home/www/html/photos/thumbs/' for writing in /vhost/site.com/home/www/html/php/photos/loademup.php on line 52
[edited by: jatar_k at 5:53 pm (utc) on Aug. 23, 2003]
[edit reason] generalized [/edit]
I assume you are refering to the Image Upload code in "Bag-O-Tricks for PHP II". I'd suggest the problem is not related to the directory creation, but rather in the database query, so try to see what is in the database, and put debug statements around that area of the code.
I just looked at the script again and noticed a couple typos in the first section that creates the db table.
CREATE TABLE `pics` (
`pid_id` VARCHAR NOT NULL ,
`pic_name` VARCHAR( 150 ) ,
`descrip` TEXT,
PRIMARY KEY ( `pid_id` )
)
They should, of course, read 'pic_id' :o
If you used that code, it could be your problem. I will try to clean the script up a bit and then repost it here.
Birdman
In my late night madness last night I didn't even notice that when I created the DB, but that took care of the fetch_array problem.
I still have this problem tho. I even tried changing the permissions to 777 on the html folder to see if that worked, but no dice.
Warning: Unable to create '/vhost/site.com/home/www/html/photos/': Is a directory in /vhost/site.com/home/www/html/php/photos/loademup.php on line 26
Now, should the "photos" direcory already be created with 777 permissions for the script to work, or does teh script create it itself?
I also noticed in the error that there is a : after the directory path, might this be a probelm or does that not matter?
Thanks again!
[edited by: jatar_k at 5:54 pm (utc) on Aug. 23, 2003]
[edit reason] generalized [/edit]
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $uploaddir . $new_pic)) { here is what I have in my code...can't even get the error. =\
<form action="view.php" method="post">
<?php
include("config.php"); $uploaddir = '/vhost/site.com/home/www/html/photos/';
$tot = count($userfile);
$num = 0;
for($q=0;$q<$tot;$q++){ if ($_FILES['userfile']['name'][$q] == "") continue;
$num = $num + 1;
$sql = "SELECT pic_id FROM pics ORDER BY pic_id DESC LIMIT 1";
$result = mysql_query($sql); while($i = mysql_fetch_array($result)){
$new_id = $i[pic_id] + 1;
$new_pic = "$new_id.jpg";
} if (move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $uploaddir . $new_pic)) {
$sql = "INSERT INTO `pics` ( `pic_id` , `name` , `descrip` )VALUES ('$new_id', NULL , NULL);";
mysql_query($sql);
} else {
print "<strong>{$_FILES['userfile']['name'][$q]}</strong> did not upload!";
} [edited by: jatar_k at 9:21 pm (utc) on Aug. 23, 2003]
[edit reason] generalized [/edit]
Did you know you can create arrays in a HTML <form>s? Seriously! Check it out [us2.php.net].
I discovered it while I was working out a way to collect a series of <input>s into an array so I could iterate through the array and compare them with some query results.
Using <input type="checkbox" name="MyArray[]" value="$value"> made it so simple.
now i think it might not be the directory that is causing the problem, but it is not adding the $new_pic to the end of the directory when it is trying to copy it there.
I will keep messing with it and see if i can get it to work.
I made a page that differs depending on a variable passed in the address. I found that if I checked the variable was blank several times the page took longer to create. Whereas if I set the variable to a default word, the speed jumped from 1.5 seconds to 0.5. (This matched the speed of the page when the variable was defined already by the address.)
Example - the link might be any of these:
test.php?a=page
test.php?a=page&value=yellow
test.php?a=page&value=red
The code used was like this:
if ($value == "") echo "<h1>Default page</h1>";
if ($value == "yellow") echo "<h1>Yellow page</h1>";
if ($value == "red") echo "<h1>Red page</h1>";
if ($value == "") $value = "default";
if ($value == "default") echo "<h1>Default page</h1>";
if ($value == "yellow") echo "<h1>Yellow page</h1>";
if ($value == "red") echo "<h1>Red page</h1>";
Example:
date given = "19/12/01" (UK date for 19th December 2001)
list ($day, $month, $year) = split ("/", "19/12/01");
$newdate = $month."/".$day."/".$year;
$unixdate = strtotime ($newdate);
$ukdate = date("d/m/y", $unixdate);
To convert back to a UK date format use the last line of code above. Note how you can use the slash symbol with date().
_________________________________________________________
PHP Bag of Tricks 2004 - Part 4 [webmasterworld.com]
[edited by: jatar_k at 4:39 pm (utc) on Nov. 24, 2004]