Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Dmitry Shilnikov http://sourceforge.net/projects/glossword/

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

PHP: Pagination

// Another example for creating pagination.
// Usage:
// print get_page_nav($page_current, $page_total);

	/**
	 * Creates links to pages.
	 * 
	 * @param	integer	$page_current The current page number.
	 * @param	integer	$page_total The total number of pages.
	 * @return 	string	HTML-code.
	 */
	function get_page_nav($page_current, $page_total)
	{
		$ar_pages = array();
		/* HTML-tag to select the current page */
		$str_tag = 'strong';
		/* Language strings */
		$str_page_prev = 'Предыдущая';
		$str_page_next = 'Следующая';
		/* String placed between the first and last page numbers */
		$str_more = '..';
		/* String to separate page numbers */
		$str_d = $this->sys['char_split_pages'];
		/* URL for paging */
		$str_url = 'index.php?book='.$this->gv['book'].'&page=%d';
		
		/* The number of links to pages displayed before and after the current page. 1 2 (3) 1 2 */
		$int_max = 2;
		$cnt_new = $page_current;
		$cnt_max = $page_current + $int_max;
		$cnt_min = $page_current - $int_max;
		/* Fix the maximum number of pages */
		if ($cnt_max > $page_total)
		{
			$cnt_max = $page_total;
		}
		/* First page */
		if ($cnt_min > 1)
		{
			$ar_pages[] = '<a href="'.sprintf($str_url, 1).'">1</a>';
			$ar_pages[] = $str_more;
		}
		/* For each page number */
		for ($i = 1; $i <= $page_total; $i++)
		{
			if ( ($i >= $cnt_min && $i <= $cnt_max) )
			{
				if ($i == $page_current)
				{
					$ar_pages[] = '<a href="'.sprintf($str_url, $i).'"><'.$str_tag.'>'.$i.'</'.$str_tag.'></a>';
				}
				else
				{
					$ar_pages[] = '<a href="'.sprintf($str_url, $i).'">'.$i.'</a>';
				}
			}
			$cnt_new++;
		}
		/* The last page */
		if ($cnt_max > 1 && ($page_current + $int_max) < $page_total)
		{
			$ar_pages[] = $str_more;
			$ar_pages[] = '<a href="'.sprintf($str_url, $page_total).'">'.$page_total.'</a>';
		}
		/* Links to Next/Prev pages */
		if ($page_current > 1)
		{
			$ar_pages[] = '<a href="'.sprintf($str_url, ($page_current - 1) ).'">'.$str_page_prev.'</a>';
		}
		if ($page_current < $page_total)
		{
			$ar_pages[] = '<a href="'.sprintf($str_url, ($page_current + 1) ).'">'.$str_page_next.'</a>';
		}
		return implode($str_d, $ar_pages);
	}

Translit

// Transliteration (or Translit for short).
// Converts Russian characters into Latin on-the-fly.
// Save the code as UTF-8.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Translit</title>
<script type="text/javascript">
/* Javascript functions */
function JSfunc()
{
	/* Making transliteration! */
	this.strTranslit = function(el)
	{
		new_el = document.getElementById('out');
		A = new Array();
		A["Ё"]="YO";A["Й"]="I";A["Ц"]="TS";A["У"]="U";A["К"]="K";A["Е"]="E";A["Н"]="N";A["Г"]="G";A["Ш"]="SH";A["Щ"]="SCH";A["З"]="Z";A["Х"]="H";A["Ъ"]="'";
		A["ё"]="yo";A["й"]="i";A["ц"]="ts";A["у"]="u";A["к"]="k";A["е"]="e";A["н"]="n";A["г"]="g";A["ш"]="sh";A["щ"]="sch";A["з"]="z";A["х"]="h";A["ъ"]="'";
		A["Ф"]="F";A["Ы"]="I";A["В"]="V";A["А"]="A";A["П"]="P";A["Р"]="R";A["О"]="O";A["Л"]="L";A["Д"]="D";A["Ж"]="ZH";A["Э"]="E";
		A["ф"]="f";A["ы"]="i";A["в"]="v";A["а"]="a";A["п"]="p";A["р"]="r";A["о"]="o";A["л"]="l";A["д"]="d";A["ж"]="zh";A["э"]="e";
		A["Я"]="YA";A["Ч"]="CH";A["С"]="S";A["М"]="M";A["И"]="I";A["Т"]="T";A["Ь"]="'";A["Б"]="B";A["Ю"]="YU";
		A["я"]="ya";A["ч"]="ch";A["с"]="s";A["м"]="m";A["и"]="i";A["т"]="t";A["ь"]="'";A["б"]="b";A["ю"]="yu";
		new_el.value = el.value.replace(/([\u0410-\u0451])/g,
			function (str,p1,offset,s) {
				if (A[str] != 'undefined'){return A[str];}
			}
		);
	}
	/* Normalizes a string, eю => eyu */
	this.strNormalize = function(el)
	{
		if (!el) { return; }
		this.strTranslit(el);
	}
}
var oJS = new JSfunc();
</script>
</head>
<body>

<p>введите текст:</p>
<textarea onkeyup="oJS.strNormalize(this)" style="height:10em;width:100%" id="in"></textarea>
<p>результат:</p>
<textarea style="height:10em;width:100%" id="out"></textarea>

</body>
</html>

PHP: multibyte function to get string length

	/**
	 * Get string length, multibyte.
	 *
	 * @param   string  $t Any string content
	 * @param   string  $encoding Charset encoding
	 * @return  int     String length
	 */
	function mb_strlen($t, $encoding = 'UTF-8')
	{
		/* --enable-mbstring */
		if (function_exists('mb_strlen'))
		{
			return mb_strlen($t, $encoding);
		}
		else
		{
			return strlen(utf8_decode($t));
		}
	}

PHP-function to optimize a CSS-file

	/**
	 * Converts a CSS-file contents into one string
	 *
	 * @param    string  $t Text data
	 * @param    int     $is_debug Skip convertion
	 * @return   string  Optimized string
	 */
	function text_smooth_css($t, $is_debug = 0)
	{
		if ($is_debug) { return $t; }
		/* Remove comments */
		$t = preg_replace("/\/\*(.*?)\*\//s", ' ', $t);
		/* Remove new lines, spaces */
		$t = preg_replace("/(\s{2,}|[\r\n|\n|\t|\r])/", ' ', $t);
		/* Join rules */
		$t = preg_replace('/([,|;|:|{|}]) /', '\\1', $t);
		$t = str_replace(' {', '{', $t);
		/* Remove ; for the last attribute */
		$t = str_replace(';}', '}', $t);
		$t = str_replace(' }', '}', $t);
		return $t;
	}

Glossword WAMP source code (NSIS)

// NSIS (http://nsis.sourceforge.net/)
// Glossword WAMP (http://sourceforge.net/projects/glossword/)
// Apache, MySQL and PHP are stored in archive usr.exe, directory /usr/local
// phpMyAdmin is in archive htdocs.exe, directory /htdocs/phpmyadmin
// Additionaly you need files with phrases: English.nsh and Russian.nsh
// ------------------------------------------
// English.nsh:
// LangString SECT_01 ${LANG_ENGLISH} "Glossword ${PRODUCT_VERSION}"
// LangString TXT_02 ${LANG_ENGLISH} "Thank you for installing Glossword.\r\nFor news and updates go to http://sourceforge.net/projects/glossword/"
// LangString DESC_SecGw ${LANG_ENGLISH} "Glossword program core files."
// ------------------------------------------
// install.bat
// @echo on
// cls
// SET ipath=%1
// cd "%ipath%/usr/local/apache2/bin"
// httpd.exe -k install -n Apache2_GW
// httpd.exe -k start -n Apache2_GW
// cd "%ipath%/usr/local/mysql5/bin"
// mysqld-nt.exe --install MySQL50_GW --defaults-file="%ipath%/usr/local/mysql5/bin/my-custom.cnf"
// net start MySQL50_GW
// ------------------------------------------
// uninstall.bat
// @echo off
// cls
// SET ipath=%1
// cd "%ipath%/usr/local/apache2/bin"
// httpd.exe -k stop -n Apache2_GW
// httpd.exe -k uninstall -n Apache2_GW
// net stop MySQL50_GW
// cd "%ipath%/usr/local/mysql5/bin"
// mysqld-nt.exe --remove MySQL50_GW
// ------------------------------------------
// unpack.bat
// @echo off
// usr.exe -y
// htdocs.exe -y
// del usr.exe
// del htdocs.exe
// del unpack.bat


Now you can create your own WAMP package.

; Glossword Desktop edition: Apache, MySQL, PHP
; Written by Dmitry Shilnikov (c) 2002-2007
; tty01@rambler.ru
;--------------------------------
;Include Modern UI

!include "MUI.nsh"

; replace in file with count of changes
!include "FileFunc.nsh"

;--------------------------------
;Custom variables

!define PRODUCT_NAME "Apache, MySQL, PHP"
!define PRODUCT_VERSION "Apache/2.2.4, MySQL 5.0.41-community-nt, PHP 5.2.3 for Windows"
!define DIR_SRC "."
!define THIS_DIR_INSTALLTO "Glossword-WAMP"

;--------------------------------
;General

Name "${PRODUCT_NAME}"
OutFile "glossword-wamp.exe"

;Folder selection page
InstallDir "$PROGRAMFILES\Glossword-WAMP"

;--------------------------------
;Interface Settings

!define MUI_ABORTWARNING
!define MUI_UI ${NSISDIR}\Contrib\UIs\modern.exe
!define MUI_ICON ${DIR_SRC}\install\gw_new.ico
!define MUI_UNICON ${DIR_SRC}\install\gw_remove.ico
!define MUI_FINISHPAGE_TEXT $(TXT_02)

;--------------------------------
;Pages

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

;Uninstaller pages
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

;--------------------------------
;Languages

!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "Russian"

;--------------------------------
;Installer Sections

Section "$(SECT_01)" SecGw

SetDetailsPrint textonly
DetailPrint "Installing server files..."
SetOverwrite ifnewer

DetailPrint "htdocs..."
CreateDirectory "$INSTDIR\htdocs"
CreateDirectory "$INSTDIR\htdocs\glossword"

SetOutPath "$INSTDIR"
File "usr.exe"
File "htdocs.exe"
File "unpack.bat"
File "install.bat"
File "uninstall.bat"
File "localhost.url"
File "phpinfo.url"
File "phpmyadmin.url"
File "news.url"
File "glossword-wamp224_5041_523.txt"

ExecWait '"unpack.bat"'

SetOutPath "$INSTDIR"
; change configuraiton files
DetailPrint "httpd.conf..."
ClearErrors
FileOpen $0 "$INSTDIR\usr\local\apache2\conf\httpd.conf" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop1:
FileRead $0 $2
IfErrors done1
StrCmp $2 "# Created by install$\r$\n" 0 +2
StrCpy $2 "# Created by glossword-amp.exe$\r$\n"
StrCmp $2 "DocumentRoot $\"E:/usr/httpdocs/svn/Glossword Desktop/htdocs$\"$\r$\n" 0 +2
StrCpy $2 "DocumentRoot $\"$INSTDIR\htdocs$\"$\r$\n"
StrCmp $2 "ServerRoot $\"E:/usr/httpdocs/svn/Glossword Desktop/usr/local/apache2$\"$\r$\n" 0 +2
StrCpy $2 "ServerRoot $\"$INSTDIR\usr\local\apache2$\"$\r$\n"
FileWrite $1 $2
Goto loop1
done1:
FileClose $0
FileClose $1
Delete "$INSTDIR\usr\local\apache2\conf\httpd.conf"
CopyFiles /SILENT $R0 "$INSTDIR\usr\local\apache2\conf\httpd.conf"
Delete $R0
;
DetailPrint "my-custom.cnf..."
ClearErrors
FileOpen $0 "$INSTDIR\usr\local\mysql5\bin\my-custom.cnf" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop2:
FileRead $0 $2
IfErrors done2
StrCmp $2 "basedir=$\"../mysql5/$\"$\r$\n" 0 +2
StrCpy $2 "basedir=$\"$INSTDIR\usr\local\mysql5\$\"$\r$\n"
StrCmp $2 "datadir=$\"../mysql5/data/$\"$\r$\n" 0 +2
StrCpy $2 "datadir=$\"$INSTDIR\usr\local\mysql5\data\$\"$\r$\n"
FileWrite $1 $2
Goto loop2
done2:
FileClose $0
FileClose $1
Delete "$INSTDIR\usr\local\mysql5\bin\my-custom.cnf"
CopyFiles /SILENT $R0 "$INSTDIR\usr\local\mysql5\bin\my-custom.cnf"
Delete $R0
;
DetailPrint "php.ini..."
ClearErrors
FileOpen $0 "$INSTDIR\usr\local\php5\php.ini" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop5:
FileRead $0 $2
IfErrors done5
StrCmp $2 "extension_dir = $\"../../php5/ext$\"$\r$\n" 0 +2
StrCpy $2 "extension_dir = $\"$INSTDIR\usr\local\php5\ext$\"$\r$\n"
StrCmp $2 "session.save_path = $\"c:/temp$\"$\r$\n" 0 +2
StrCpy $2 "session.save_path = $\"$TEMP$\"$\r$\n"
FileWrite $1 $2
Goto loop5
done5:
FileClose $0
FileClose $1
Delete "$INSTDIR\usr\local\php5\php.ini"
CopyFiles /SILENT $R0 "$INSTDIR\usr\local\php5\php.ini"
Delete $R0
;
DetailPrint "install.bat..."
ClearErrors
FileOpen $0 "$INSTDIR\install.bat" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop3:
FileRead $0 $2
IfErrors done3
StrCmp $2 "set ipath=%1$\r$\n" 0 +2
StrCpy $2 "set ipath=$INSTDIR$\r$\n"
FileWrite $1 $2
Goto loop3
done3:
FileClose $0
FileClose $1
Delete "$INSTDIR\install.bat"
CopyFiles /SILENT $R0 "$INSTDIR\install.bat"
Delete $R0
;
DetailPrint "uninstall.bat..."
ClearErrors
FileOpen $0 "$INSTDIR\uninstall.bat" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop4:
FileRead $0 $2
IfErrors done4
StrCmp $2 "set ipath=%1$\r$\n" 0 +2
StrCpy $2 "set ipath=$INSTDIR$\r$\n"
FileWrite $1 $2
Goto loop4
done4:
FileClose $0
FileClose $1
Delete "$INSTDIR\uninstall.bat"
CopyFiles /SILENT $R0 "$INSTDIR\uninstall.bat"
Delete $R0
ExecWait '"install.bat"'
;nsExec::ExecToStack '"install.bat"'

SetOverwrite off

CreateDirectory "$SMPROGRAMS\${THIS_DIR_INSTALLTO}"
CreateShortCut "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\Glossword at localhost.lnk" "$INSTDIR\localhost.url"
CreateShortCut "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\Glossword development news.lnk" "$INSTDIR\news.url"
CreateShortCut "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\phpinfo().lnk" "$INSTDIR\phpinfo.url"
CreateShortCut "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\phpMyAdmin.lnk" "$INSTDIR\phpmyadmin.url"
CreateShortCut "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\Apache Monitor.lnk" "$INSTDIR\usr\local\apache2\bin\ApacheMonitor.exe"
CreateShortCut "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\Remove Glossword WAMP.lnk" "$INSTDIR\uninstall.exe"
CreateShortCut "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\Release notes.lnk" "$INSTDIR\glossword-wamp224_5041_523.txt"

WriteUninstaller "$INSTDIR\uninstall.exe"
SectionEnd


;--------------------------------
;Descriptions

!include "${DIR_SRC}\install\English.nsh"
!include "${DIR_SRC}\install\Russian.nsh"

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${SecGw} $(DESC_SecGw)
!insertmacro MUI_FUNCTION_DESCRIPTION_END

;--------------------------------
; Functions

Function .onInit
!insertmacro MUI_LANGDLL_DISPLAY
FunctionEnd

;--------------------------------
;Uninstaller Section

Section Uninstall

SetOutPath "$INSTDIR"
ExecWait '"uninstall.bat"'

SetShellVarContext current
Delete "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\Remove Glossword WAMP.lnk"
Delete "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\Apache Monitor.lnk"
Delete "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\Glossword at localhost.lnk"
Delete "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\Glossword development news.lnk"
Delete "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\phpinfo().lnk"
Delete "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\phpMyAdmin.lnk"
Delete "$SMPROGRAMS\${THIS_DIR_INSTALLTO}\Release Notes.lnk"
RMDir /r "$SMPROGRAMS\${THIS_DIR_INSTALLTO}"
RMDir /r "$INSTDIR\usr\local\apache2"
RMDir /r "$INSTDIR\usr\local\php5"
RMDir /r "$INSTDIR\usr\local\mysql5\bin"
RMDir /r "$INSTDIR\usr\local\mysql5\share"
RMDir /r "$INSTDIR\htdocs\phpmyadmin"
Delete "$INSTDIR\install.bat"
Delete "$INSTDIR\uninstall.bat"
Delete "$INSTDIR\glossword-wamp224_5041_523.txt"
Delete "$INSTDIR\localhost.url"
Delete "$INSTDIR\phpinfo.url"
Delete "$INSTDIR\phpmyadmin.url"

;RMDir /r "$INSTDIR"
SetAutoClose true

SectionEnd
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS