blog moved to
Записки программиста (PHP, MySQL, Javascript, CSS)
Trim Text - PHP
29 октября 2008
11:27
function trim_text($str,$max_length)
{
if(strlen($str) < = $max_length)
return $str;
$str=mb_substr(strip_tags($str),0,$max_length,'utf-8');
$num_words = count(explode(' ',$str));
if($num_words==2)
return $str;
else
{
while (strlen($str) >0)
{
$pop = mb_substr($str, -1);
if ($pop!=' ')
{ //echo $str.'< br >';
$str = mb_substr ($str, 0, -1, 'utf-8');
}
else
return strip_tags($str.'...');
}
return false;
}
}
{
if(strlen($str) < = $max_length)
return $str;
$str=mb_substr(strip_tags($str),0,$max_length,'utf-8');
$num_words = count(explode(' ',$str));
if($num_words==2)
return $str;
else
{
while (strlen($str) >0)
{
$pop = mb_substr($str, -1);
if ($pop!=' ')
{ //echo $str.'< br >';
$str = mb_substr ($str, 0, -1, 'utf-8');
}
else
return strip_tags($str.'...');
}
return false;
}
}
SMARTY - change templates dynamically
28 октября 2008
15:55
By default SMARTY keeps templates files in cache_dir (in Smarty.class.php change $force_compile to true not to do that, but in that case performance will be much lower). So if you change template dynamically (like keep template's directory name in DB) you may face problem, because it will load something like half-old half-new template.
Smart way to do that is to give uniq name to each template is loaded for Smarty to separate them. You can do that with adding this in start of document:
$smarty = new Smarty();
$smarty- >compile_id = $template_name;
, where $template_name is for example name of template that you get lets say from Database.
Be sure to keep $compile_check = true;
Smart way to do that is to give uniq name to each template is loaded for Smarty to separate them. You can do that with adding this in start of document:
$smarty = new Smarty();
$smarty- >compile_id = $template_name;
, where $template_name is for example name of template that you get lets say from Database.
Be sure to keep $compile_check = true;
PHP - Fit Image Size
28 октября 2008
14:41
Function:
function force_width_height($width,$height,$max_width=250,$max_height=50) {
if ($width >$max_width) $over_width=true; else $over_width=false;
if ($height >$max_height) $over_height=true; else $over_height=false;
if ($over_height and $over_width)
{
return 'width="$max_width" height="'.($max_width*$height)/$width.'" ';
}
elseif ($over_height)
{
return 'width="'.($max_height*$width)/$height.'" height="$max_height" ';
}
elseif ($over_width)
{
return 'width="'.$max_width.'" height="'.($max_width*$height)/$width.'" ';
}
else
{
return 'width="'.$width.'" height="'.$height.'" ';
}
}
Usage (let's say we need to fit some image to width=87px and height=65px):
$site_path = '/var/www/html/domain.com'; //full site path (needed to check if file exists)
$web_path = 'domain.com';
$image_filename = '/images/image.gif'; //Filename of image
if(files_exists($site_path . $image_filename)) list($width,$height)=getimagesize($image_url);
$fixed_size_attr = force_width_height($width, $height, 87, 65);
echo '< img src="http://' . $web_path . $image_url.'" '.$fixed_size_attr.' >';
function force_width_height($width,$height,$max_width=250,$max_height=50) {
if ($width >$max_width) $over_width=true; else $over_width=false;
if ($height >$max_height) $over_height=true; else $over_height=false;
if ($over_height and $over_width)
{
return 'width="$max_width" height="'.($max_width*$height)/$width.'" ';
}
elseif ($over_height)
{
return 'width="'.($max_height*$width)/$height.'" height="$max_height" ';
}
elseif ($over_width)
{
return 'width="'.$max_width.'" height="'.($max_width*$height)/$width.'" ';
}
else
{
return 'width="'.$width.'" height="'.$height.'" ';
}
}
Usage (let's say we need to fit some image to width=87px and height=65px):
$site_path = '/var/www/html/domain.com'; //full site path (needed to check if file exists)
$web_path = 'domain.com';
$image_filename = '/images/image.gif'; //Filename of image
if(files_exists($site_path . $image_filename)) list($width,$height)=getimagesize($image_url);
$fixed_size_attr = force_width_height($width, $height, 87, 65);
echo '< img src="http://' . $web_path . $image_url.'" '.$fixed_size_attr.' >';
Import BIG file into MySQL Database
25 октября 2008
00:23
To load big txt, csv file into MySQL db just run in sql command line (or from phpMyAdmin- >SQL) something like this:
LOAD DATA INFILE 'C:/cities.txt' INTO TABLE `countries` FIELDS TERMINATED BY ','
..was really useful for me when I was trying to intergate HUGE txt file that contains Countries, Regions and cities of all over the world which size was something like 200Mb, it took ~1 minute.
Hope this short info will be useful for you as well.
LOAD DATA INFILE 'C:/cities.txt' INTO TABLE `countries` FIELDS TERMINATED BY ','
..was really useful for me when I was trying to intergate HUGE txt file that contains Countries, Regions and cities of all over the world which size was something like 200Mb, it took ~1 minute.
Hope this short info will be useful for you as well.
lightbox
24 октября 2008
13:57
List of special stuff you'll need to add lightbox effect to your images:
- jQuery (last version)
- lightbox.js
- lightbox.css
- folder lightbox with images
Instead of showing code and stuff I uploaded all files to deposit.
So you can download 100% workable version by following this link:
Good Luck
- jQuery (last version)
- lightbox.js
- lightbox.css
- folder lightbox with images
Instead of showing code and stuff I uploaded all files to deposit.
So you can download 100% workable version by following this link:
Good Luck