Forum Moderators: coopster

Message Too Old, No Replies

Bag-O-Tricks for PHP III

useful PHP code snippets continued

         

Nick_W

12:43 pm on Aug 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



two previous threads
Php bag of tricks [webmasterworld.com]
Bag-O-Tricks for PHP II [webmasterworld.com]

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> &gt; ";
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> &gt; ";
}
}
//
// 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]

jatar_k

4:58 pm on Aug 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Since Nick had this little gem and we have a bunch of members that weren't around when we did the previous threads. It is about time we went back at it.

thanks Nick for letting me chop up your post, nice script too ;)

Giljorak

6:59 pm on Aug 8, 2003 (gmt 0)

10+ Year Member



Hello

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> &gt; ";

with

if ($oldval){
$op.=" &gt; <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.

Nick_W

7:02 pm on Aug 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld!

Can you post the full modified function?

Nick

Giljorak

7:12 pm on Aug 8, 2003 (gmt 0)

10+ Year Member



I changed it so that I could use it as an include as I haven't wrapped my head around functions yet.

<?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> &gt; ";
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.=" &gt; <a href=\"/$oldval$val/\">$val</a>";
$oldval.="$val/";
}else{
$op.="<a href=\"/$val/\">$val</a>";
$oldval="$val/";
}


}
}
print "$op";
?>

joeym

6:10 am on Aug 23, 2003 (gmt 0)

10+ Year Member



re: the image upload and resizing script

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]

ShawnR

7:53 am on Aug 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi joeym & welcome to WebmasterWorld!

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.

Birdman

11:22 am on Aug 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello joeym, and welcome to WW!

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

joeym

5:42 pm on Aug 23, 2003 (gmt 0)

10+ Year Member



Thanks for the welcome! This place seems spot on!

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]

jatar_k

5:58 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no you have to create the dir and set the permissions to 777

I also edited msg30 in the Bag O' Tricks II thread to reflect that change to the col names.

joeym

6:18 pm on Aug 23, 2003 (gmt 0)

10+ Year Member



yeah, thats what I thought. Unfortunately, I already tried that too. It's that damn line 26...lol

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]

lorax

6:54 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's one I just discovered (and slapped my head 'cause I could have used this at least a hundred times before now).

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.

joeym

11:07 pm on Aug 27, 2003 (gmt 0)

10+ Year Member



hmmm..i am still messing with this image upload code, but still have not been able to make it work.

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.

Hester

1:16 pm on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a simple way to speed up scripts considerably.

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

In the first link I left the variable off so the default page appears. I didn't want to add it to the link as it makes the address unnecessarily long. (The page is always accessed from the default link.)

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>";

I used the variable $value several times in a set of small loops. To speed the page up, all I did was assign the variable at the top of the page like this instead of leaving it blank:

if ($value == "") $value = "default";

Then I amended my code like this:

if ($value == "default") echo "<h1>Default page</h1>";
if ($value == "yellow") echo "<h1>Yellow page</h1>";
if ($value == "red") echo "<h1>Red page</h1>";

I was amazed at the speed increase.

Hester

1:18 pm on Sep 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick way to convert a date in UK format to US date format, so it can be converted into a UNIX timestamp, is to simply reverse the day and month.

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

This splits the UK date into chunks then rearranges the first 2 to make it a US style date. Then it gets the UNIX timestamp ready for further processing.

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]