Shawn
Feb 15, 2004, 12:22 PM
a thought a thread containing useful PHP code bits would be useful.
I'll start out with a useful way to assign strings using the heredoc syntax:
$text=<<<EOF
<html>
blah blah blah
$someVar
blah blah blah
</html>
EOF;
You can also echo heredoc strings this way too.
To view arrays, use print_r($array). Including <pre> tags is good for viewing large arrays; i.e.,
echo '<pre>'; print_r($your_array); echo '</pre>';
Shawn
Feb 15, 2004, 12:42 PM
This is a way to create a menu bar like the one above (i.e., Mind-Brain.com Forum -> Information Technology -> Web Programming -> PHP Pearls), and which I've now added to the top (on the left) of non-Forum pages.
//
// $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";
Shawn
Feb 15, 2004, 12:53 PM
Here's a PHP Countdown script:
the $bday variable contains the date to countdown to.
at the moment it is set to (23, 59, 0, 4, 17, 2003)
(which is 11:59pm, 17 April, 2003)
numbers are:
HOUR (24 hr), MINUTE, SECOND, MONTH, DAY, YEAR
SO, for example, you wanted to countdown to new year
2006, it would be
$bday = mktime(0, 0, 0, 1, 1, 2006);
<?
// change this for your date to countdown to
$bday = mktime(23, 59, 0, 4, 17, 2003);
print "Your countdown date is " . date("j F Y, g.i a", $bday) . ".<BR>";
print "You visited this page at " . date("j F Y, G.i:s", time());
$diff = $bday - time();
$days = ($diff - ($diff % 86400)) / 86400;
$diff = $diff - ($days * 86400);
$hours = ($diff - ($diff % 3600)) / 3600;
$diff = $diff - ($hours * 3600);
$minutes = ($diff - ($diff % 60)) / 60;
$diff = $diff - ($minutes * 60);
$seconds = ($diff - ($diff % 1)) / 1;
print "Only $days days, $hours hours, $minutes minutes and $seconds seconds until your countdown date!";
?>