Get the Width – Height of a picture
0//$picture gathers the picture full address
if($picture !=”"){
$showImage = $picture;
$infoImage = getimagesize(PATHUPLOADEDIMAGES.$showImage);
$infoW = $infoImage[0];
$infoH = $infoImage[1];if ($infoH>$infoW){
if ($infoH>133) {
$setHeight = ” height =\”130\”";
}
}else{
if ($infoW>133) {
$setHeight = ” width =\”130\”";
}
}
Date Format in php
0This code will create an object called $date where methods could be called as per the example below:
$date = new DateTime($date_variable);
echo $date->format('D');
echo $date->format('d');
echo $date->format('M');
echo $date->format('y');
Adding Days Or Months To a Date
0few tips on how you can add time span to a date in php:
$date = date("Y-m-d"); // set date as TODAY
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");// could be +2 months etc.
echo date("Y-m-d", $date); //final display of TODAY +1 month
Please note that the date will be displayed in YYYY-MM-DD format.
For other formats please refer to:
Remove First or Last Characters From a String
0The next short example, shows how could be possible to remove some characters from a string without hassle.
$var = "The Quick Brown Fox";
$var = substr($var,0,-4);
#now $var = "The Quick Brown";#to remove JUST the last character use this:
$var = "The Quick Brown Fox";
$var = substr($var, 0, -1);
#now $var = "The Quick Brown Fo";#to remove JUST the first character use this:
$var = "The Quick Brown Fox";
$var = substr($var, 1);
#now $var = "he Quick Brown Fox";
The example above could be amended as per Your taste.
MySql Database Dump Function
0Here is a function I’ve found on the internet [Thanks to David Walsh original article].
The function allows to create a database dump that will be saved onto a specific folder and offers you the chance to download immediately.
here is the code:
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
#THIS Variable could be amended [please note that the file will be saved in the directory where the function is called.
#BE sure that directory has write permissions
$fileName = 'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql';
//save file
$handle = fopen($fileName,'w+');
fwrite($handle,$return);
#The following lines are optional. Those lines allow you to download the file immediately
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$fileName.'"');
#The Follow Line MUST be amended to suit your directory path
readfile('http://www.website.com/directory/'.$fileName);
fclose($handle);
}
The Function will be triggered by a simple call:
backup_tables('mysqlhost','username','password','databasename','optional table to backup');
Please note that all of the above are Variables and you must fill in the specific details in order to have the function working correctly.
e.g.: backup_tables('192.1.1.1','mysql_user','user123','mysql_database');
This will backup all the database called "mysql_database".
The original article [as per link above] has been modified without asking permission.
We do apologies to the author if we have breached any copyright.
Error Handling and Debug Logs
0Debug features can be activated in a way to display all errors and warnings to the browser:
// Report all PHP errors
ini_set('error_reporting', E_ALL);// Set the display_errors directive to On
ini_set(‘display_errors’, 1);Or it could be useful not to be aware of the 100% of warnings and display just the relevant ones:
// Report simple running errors
ini_set(‘error_reporting’, E_ALL ^ E_NOTICE);// Set the display_errors directive to Off
ini_set(‘display_errors’, 0);// Log errors to the web server’s error log
ini_set(‘log_errors’, 1);
A further [and smarter] use of these errors is stacking them onto a log file or an email.
// Destinations
define(“ADMIN_EMAIL”, “nobody@stanford.edu”);
define(“LOG_FILE”, “/my/home/errors.log”);// Destination types
define(“DEST_EMAIL”, “1″);
define(“DEST_LOGFILE”, “3″);/* Examples */
// Send an e-mail to the administrator
error_log(“Fix me!”, DEST_EMAIL, ADMIN_EMAIL);// Write the error to our log file
error_log(“Error”, DEST_LOGFILE, LOG_FILE);
A very very good example of this is as per following:
// Destinations
define(“ADMIN_EMAIL”, “email@domain.com”);
// ***** please note that the log file must be stored into a place that has write permission
// ***** and so, a good way to gather information re where to store this file is:
// ***** create a folder, assign permission as per CHMOD 0777, fill in the following Definition using a physical address
define(“LOG_FILE”, “/my/home/errors.log”);
// Destination types
define(“DEST_EMAIL”, “1″);
define(“DEST_LOGFILE”, “3″);# Parameters:
# $errno: Error level
# $errstr: Error message
# $errfile: File in which the error was raised
# $errline: Line at which the error occurredfunction my_error_handler($errno, $errstr, $errfile, $errline){
switch ($errno) {
case E_USER_ERROR:
// Send an e-mail to the administrator
error_log(“Error: $errstr \n Fatal error on line $errline in file $errfile \n”, DEST_EMAIL, ADMIN_EMAIL);// Write the error to our log file
error_log(“Error: $errstr \n Fatal error on line $errline in file $errfile \n”, DEST_LOGFILE, LOG_FILE);
break;case E_USER_WARNING:
// Write the error to our log file
error_log(“Warning: $errstr \n in $errfile on line $errline \n”, DEST_LOGFILE, LOG_FILE);
break;case E_USER_NOTICE:
// Write the error to our log file
error_log(“Notice: $errstr \n in $errfile on line $errline \n”, DEST_LOGFILE, LOG_FILE);
break;default:
// Write the error to our log file
error_log(“Unknown error [#$errno]: $errstr \n in $errfile on line $errline \n”, DEST_LOGFILE, LOG_FILE);
break;
}// Don’t execute PHP’s internal error handler
return TRUE;
}// Use set_error_handler() to tell PHP to use our method
$old_error_handler = set_error_handler(“my_error_handler”);
This could help a lot to determine what’s what on your website!
[courtesy of http://www.stanford.edu]
Live Clock Javascript
0The following script allows you to have a fully functional, neat and easy-to-integrate live clock. 100% javascript, light weight and fully customizable…
Hope that will suits you
function init (){
timeDisplay = document.createTextNode ( "" );
document.getElementById("clock").appendChild ( timeDisplay );
}
function updateClock (){
var currentTime = new Date ( );
var currentHours = currentTime.getHours ( );
var currentMinutes = currentTime.getMinutes ( );
var currentSeconds = currentTime.getSeconds ( );
// Pad the minutes and seconds with leading zeros, if required
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
// Choose either "AM" or "PM" as appropriate
var timeOfDay = (currentHours<12) ? "AM" : "PM";
// Convert the hours component to 12-hour format if needed currentHours = (currentHours>12) ? currentHours - 12 : currentHours;
// Convert an hours component of "0" to "12"
currentHours = ( currentHours == 0 ) ? 12 : currentHours;
// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
// Update the time display
document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
the call for these functions has to be done into the <body> tag as per following:
<body onload="updateClock(); setInterval('updateClock()', 1000 )">
finally place the span where do you want to display the clock.
e.g.:
<span id="clock"> </span>
Show/Hide elements in one click and 3 lines of code.
2This script will allow you to have an “FAQs” system visualization in few seconds.
The script is the following:
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'none')
e.style.display = 'block';
else
e.style.display = 'none';
}
The HTML code to call the javascript is the following:
<a href="javascript:toggle_visibility('answer1')">Click me</a> <span id="answer1" style="display:none"><p>Thank You</p></span>
New Theme installed
0I’ve planned to change the look of my blog since when it was launched.
Unfortunately, so far, I haven’t got time to browse the 3godzillions of different themes around.
But, i should say in honesty, I’d rather have something that let the content stands out more than the graphics elements…
…but I am just a “programmer”…
Select a random row with MySQL:
0This line of code will help picking a random row from a table of your database. One MySQL line that saves a lot of hassles.
SELECT column FROM tableORDER BY RAND()LIMIT 1