Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Saturday, October 24, 2009

Pagination pagelinks PHP algorithm

If you are making a pagination with adjacent links and are trying to figure out how to find out which number the links should start at, the algorithm below should help.

Case 1
Not enough pages, just show them all..
if (totalPages < adjacent*2)
[1]-2-3-4

Case 2
Almost at start, just start showing from beginning
true when currentPage - adjacent < 1
1-[2]-3-4-5-6 ..
starti = 1

Case 3
Almost reached the end, start showing till reached end
true when currentPage + adjacent > totalPages-1
.. 6-7-8-9-[10]-11
starti = currentPage-adjacent*2+(totalPages-currentPage)

Case 4
Not close to end or start, show even number of adjacent links on each side
true when none of above cases match
.. 2-3-[4]-5-6 ..
starti = currentPage-adjacent;


if ($this->totalPages <= $this->adjacent*2) {
$starti = 1;
$endi = $this->totalPages;
}
else if ($this->currentPage - $this->adjacent < 1) {
$starti = 1;
$endi = $this->adjacent*2;
}
else if ($this->currentPage + $this->adjacent > $this->totalPages-1) {
$starti = $this->currentPage - $this->adjacent*2 + ($this->totalPages-$this->currentPage);
$endi = $this->totalPages;
}
else {
$starti = $this->currentPage - $this->adjacent;
$endi = $this->currentPage + $this->adjacent;
}

Wednesday, October 14, 2009

Making a whos online script with Zend_Session_SaveHandler_DbTable

1] fetch rows from zend_session table where modifiedtime > time()-900
2] unserialize the data column using this function

function DecodeSession($sess_string)
{
// save current session data
// and flush $_SESSION array
$old = $_SESSION;
$_SESSION = array();

// try to decode passed string
$ret = session_decode($sess_string);
if (!$ret) {
// if passed string is not session data,
// retrieve saved (old) session data
// and return false
$_SESSION = array();
$_SESSION = $old;

return false;
}

// save decoded session data to sess_array
// and flush $_SESSION array
$sess_array = $_SESSION;
$_SESSION = array();

// restore old session data
$_SESSION = $old;

// return decoded session data
return $sess_array;
}

3] print out the username which should be stored in the session data, it'll look
something like
echo $session['data']['Namespace']['user']['username'];

Saturday, October 3, 2009

osx leopard install apache mysql memcache php memcache

1] install ports (similar to fedora's yum)
http://www.macports.org/

Memcached


2] #sudo port install memcached libmemcached

PHP


3] #sudo port install php5-memcache
4] #sudo port install php5-mysql
5] #vi /opt/local/etc/php5/php.ini
make sure socket location is /tmp/mysql.sock

Apache


6] #cd /opt/local/apache2/modules
#sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
[activating module 'php5' in /opt/local/apache2/conf/httpd.conf]
7] #vi /opt/local/apache2/conf/httpd.conf
add these lines:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

find line: DirectoryIndex and append index.php to line

8] #vi ~/.profile
Add this line:
alias httpdstart='sudo /opt/local/apache2/bin/apachectl start'
alias httpdstop='sudo /opt/local/apache2/bin/apachectl stop'

8b] autostart
#sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

9] #source ~/.profile
10] apachectl start

11] check your http://localhost

MYSQL


12] #sudo port install mysql5-server

follow directions to setup new database
if server is not starting move /etc/my.cnf to /etc/my.cnf.old
13] autostart mysql
#sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
14] edit /etc/my.cnf change socket location to /tmp/mysql.sock

PHPMyadmin


15] #sudo port install phpmyadmin
Update Apache’s httpd.conf file to find phpmyadmin. First add the following lines to the end of the file:

# Local access to phpmyadmin installation
Include conf/extra/httpd-phpmyadmin.conf

and then create a file /opt/local/apache2/conf/extra/httpd-phpmyadmin.conf containing this text:

AliasMatch ^/phpmyadmin(?:/)?(/.*)?$ "/opt/local/www/phpmyadmin$1"


Options -Indexes
AllowOverride None
Order allow,deny
Allow from all

LanguagePriority en de es fr ja ko pt-br ru
ForceLanguagePriority Prefer Fallback






other:
http://trac.macports.org/wiki/howto/MAMP
http://2tbsp.com/content/install_apache_2_and_php_5_macports

Monday, January 19, 2009

Getting Zend Debugger to work with XAMPP Leopard with Zend Studio

1] Copy the Zend Debugger PHP Extension from Zend Studio to your webserver's PHP extension dir.


#cp '/Applications/Zend/Zend Studio for Eclipse - 6.1.0/plugins/org.zend.php.debug.debugger.macosx_5.2.14.v20080602/resources/php5/ZendDebugger.so' /Applications/xampp/xamppfiles/lib/php/php5/extensions/no-debug-non-zts-20060613/ZendDebugger.so


2] edit XAMPP's php.ini to include the extension (#sudo mate /Applications/xampp/etc/php.ini)


;extension=ZendDebugger.so
zend_extension=/Applications/xampp/xamppfiles/lib/php/php5/extensions/no-debug-non-zts-20060613/ZendDebugger.so
zend_debugger.allow_hosts=127.0.0.1
zend_debugger.expose_remotely=always

Wednesday, September 17, 2008

Setup Eclipse PDT a PHP IDE with Zend Debugger easily!

So, Dreamweaver was a good editor, but if you have a big PHP project, you will need some more firepower. I decided to install Eclipse PDT a PHP IDE with the Zend Debugger. I used WAMP as my webserver and firefox as my external browser.

I haven't used it yet but I have a feeling it will help speed up development :)

Here are the steps:

Step 1] Download and install Eclipse PDT 1.0.3 (Release) (example: C:\Program Files\PDT\)
http://download.eclipse.org/tools/pdt/downloads/release.php?release=R20080603

Step 2] Download and install WAMP or any server with PHP.
http://www.wampserver.com/en/download.php

Step 3] Download and install Zend Debugger (put the ZendDebugger.dll in your C:\wamp\php\ folder)
http://downloads.zend.com/pdt/server-debugger/
Put the dummy.php in your PHP project's webroot.

Step 4] open up C:\wamp\php\php.ini
change: implicit_flush = Off to implicit_flush = On
change: output_buffering=4096 to output_buffering = Off
at the bottom append:
[Zend]
zend_extension_ts = "C:\wamp\php\ZendDebugger.dll"
zend_debugger.allow_hosts=127.0.0.1/32, 192.168.0.0/255.255.0.0
zend_debugger.expose_remotely=always

Step 5] Install Subclipse by opening PDT goto Help->Find and Install Updates(optional)
Add remote server with URL: http://subclipse.tigris.org/


--------- Extra steps to start debugging with firefox (optional, sorry its a little rough, but these are just general steps) ------------
Step 1] Start PDT, goto Window->Preferences->General->Web Browser, select use external web browser, select firefox
Step 2] Make a new project, make a new file called index.php, right click on the file, goto run as, open run as dialog.
Step 3] Point WAMP to the new project (ex: http://helloworld)
Step 4] Go back to PDT, under PHP Web Page, create new. Under Name: write in Test index.php. Under Server Bugger use: Zend Debugger. On PHP server, make a new one pointing to http://helloworld Under file, manually type in index.php. URL should be left at auto generate.
Step 5] Goto the common tab, check Debug and Run under 'display in favorites menu'. Click apply settings.
Step 6] Edit index.php to do something like echo 'hello world';
Step 7] At the top toolbar if you click the debug arrow you should see test index.php in your favorites, click it. Firefox should popup while PDT is connected to the zend debugger.

Monday, August 4, 2008

Getting CakeAMFPHP w/ CakePHP 1.1 centralized database sessions working

CakePHP makes it a snap to create centralized database sessions.

However, If you wanted to use Flash and CakeAMFPHP with CakePHP's sessions, you'll find that it might not be working. Here are some fixes+workarounds to get it running.

Step 1] open /app/vendors/vendors/cakeamfphp/amf-core/app/Gateway.php
comment out line 93

//$this->filters['auth'] = 'authenticationFilter';


The auth filter was breaking the sessions for some reason.

Step 2] Open /app/config/core.php
change the CAKE_SESSION_SAVE to:

define('CAKE_SESSION_SAVE', 'database');


change CAKE_SESSION_COOKIE to your php cookie sessions name(look in phpinfo()):
for example mine is:

define('CAKE_SESSION_COOKIE', 'PHPSESSID');


change CAKE_SECURITY to low:

define('CAKE_SECURITY', 'low');


Step 3] create the sessions table in your $default(in database.php) database by importing the file at: /app/config/sql/sessions.sql


Step 4] open /cake/libs/session.php and comment out:
on line 132:

/*if (Configure::read('Session.checkAgent') === true) {
if (env('HTTP_USER_AGENT') != null) {
$this->_userAgent = md5(env('HTTP_USER_AGENT') . CAKE_SESSION_STRING);
}
}*/


on line 311:

if (/*Configure::read('Session.checkAgent') === false || $this->_userAgent == $this->read("Config.userAgent") && */$this->time <= $this->read("Config.time")) {


on line 541:

if (/*Configure::read('Session.checkAgent') === false || $this->_userAgent == $this->read("Config.userAgent") && */$this->time <= $this->read("Config.time")) {


the useragent works with the browser but we are connecting using flash to amf to for some reason this was breaking.



You are done, try playing around with the Session methods to see if your
sessions are being stored and retrieved from the database. For a tutorial on CakePHP sessions take a look here.



---edit----
still having problems? Try turning off cookies in your cake gateway.

Open /app/webroot/cake_gateway.php:

insert this at the top:

ini_set('session.use_trans_sid', 1); //make it so we can use cookie-less sessions
ini_set('session.use_cookies', 0);


open /cake/libs/session.php:

find all occurences of ini_set('session.use_trans_sid', 0);
and change them to

ini_set('session.use_trans_sid', 1);



in your delegate when you call the gateway it must pass in a PHPSESSID example:


//cookieless sessions
if (session_id != null) {
appendurl = '?PHPSESSID='+session_id;
}

var gateway:String="http://cakeamfphp/cake_gateway.php"+appendurl;

Tuesday, June 24, 2008

How to Install CakeAMFPHP

Introduction

CAKEPHP
Are you trying to create an enterprise Flex app? You'll be needing to do some heavy lifting serverside. Chances are, you'll be needing some help... thats where CakePHP comes in. If you've programmed in PHP before, you've probably heard of CakePHP, a rapid development PHP framework. Using CakePHP allows us to stay MVC oriented, resulting in code with better readability and objects with looser coupling. Scalability. Baby!

AMFPHP
If you've ever tried to send data back and forth between flash and php chances are you've used AMFPHP. With AMFPHP you can do stuff like for example sending in custom value objects(public var fooGuy:UserVO) to your PHP script and back. This means less work, no converting objects to arrays and arrays back to objects.

CAKEAMFPHP
Wanna combine the best of both worlds? This is where CakeAMFPHP comes in. I'll be telling you have to install CakeAMFPHP with CakePHP 1.1 in this post.


Installation

Step 1.

Grab the latest CakePHP here: http://cakeforge.org/frs/download.php/591/cake_1.1.19.6305.tar.bz2

Step 2.

Extract this anywhere you'd like.
The directory structure should look like:

cake_1.1.19.6305/app
cake_1.1.19.6305/cake
cake_1.1.19.6305/docs
cake_1.1.19.6305/vendors

Step 3.

Grab the latest CakeAMFPHP here: http://cakeforge.org/frs/download.php/267/cakeamfphp.0.6.0.tar.bz2

Step 4.

Unzip CakeAMFPHP into a temporary directory.
The directory structure should look like:

cakeamfphp/webroot
cakeamfphp/views
cakeamfphp/vendors

Step 5.

Next cut and paste the webroot, views, and vendors directories into cakePHP's app folder (cake_1.1.19.6305/app). You are doing it right if cakeamfphp/webroot matches with cake_1.1.19.6305/app/webroot.

Step 6.

Configure your cakePHP installation (/app/config/core.php).


----------------------
the amfbrowser is located in the amfbrowser directory. If localhost points to your cakeAMFPHP project then http://localhost/amfbrowser will point you to the amfbrowser.

Now that the files are in place, take a look at amfBB for examples of how to use the cake_gateway.php to invoke cakePHP's controller methods.


You might notice that cakeAMFPHP break's cakePHP's session component. Next time, I'll teach you how to get cakePHP's sessions working with cakeAMFPHP :P