Archive for the ‘PHP’ Category

Code Folding That Works

Saturday, October 20th, 2007

A user comments on VIM simplefold (a folding plugin that works better than the vim defaults):

First code folding that is worth the bother

I’ve just tried it and I have nothing more to add. It works well, and I look forward to using it as one of my default tools.

Patches to CodeIgniter for Sqlite Support

Monday, September 3rd, 2007

See CodeIgniter Forums

Note: The following doesn’t quite work as well.

From Price Change Blog (in Japanese):


function list_fields($table = '')
{
	// Is there a cached result?
	if (isset($this->data_cache['field_names'][$table]))
	{
		return $this->data_cache['field_names'][$table];
	}

	if ($table == '')
	{
		if ($this->db_debug)
		{
			return $this->display_error('db_field_param_missing');
		}
		return FALSE;
	}
	$sql = "SELECT * FROM ".$this->escape($table)." LIMIT 1";
	$query = $this->query($sql);

	$retval = array();

	if ($query->num_rows() > 0)
	{
		$row = $query->row_array();
		$keys = array_keys($row);
		foreach ($keys as $val)
		{
			if (!is_int($val))
				$retval[] = $val;
		}
	}

	$this->data_cache['field_names'][$table] = $retval;
	return $this->data_cache['field_names'][$table];
}


$query = $this->query("SELECT COUNT(*) AS numrows FROM '".$this->dbprefix.$table."'");

Integrating CakePHP with PEAR:Auth

Monday, September 4th, 2006

If you have a web-site which uses PEAR::Auth, and another section of your site using CakePHP, you’d probably find it baffling that your CakePHP doesn’t remember your authentication cookie. In addition, CakePHP wouldn’t redirect properly after logging in.

The main reason is CakePHP overrides the standard PHP session environment, see cake/lib/session.php


  ini_set('session.use_trans_sid', 0);
  ini_set('session.name', CAKE_SESSION_COOKIE);
  ini_set('session.cookie_lifetime', $this->cookieLifeTime
  ini_set('session.cookie_path', $this->path);
  ini_set('session.gc_probability', 1);

The solution is to change admin/config/core.php


    define('CAKE_SESSION_SAVE', null);

The other problem you’d encounter is because the REQUEST_URI points to the base controller PHP file, and not your current URL. In your controller file, say, app/controller/blogs_controller.php


    function beforeFilter()
    {
         this->checkSession();
    }

    function checkSession()
    {
        require_once("MDB2.php");
        require_once("Auth.php");
        require_once("../../../config.php");
        $auth = new Auth("DB", $dsn, "", false);
        $auth->start();
        if (!$auth->checkAuth())
        {
            $returnURL = $_SERVER['REDIRECT_URL'];
            header("Location: /login.php?return=" . $returnURL);
            exit;
        }
   }

Creating ImageMaps with PHP Image_Graph

Friday, September 1st, 2006

Here’s the result:
Image_Canvas ImageMap Example

The code is pretty long winded. I suggest you start by copying the entire code into your page and then progressively modifying it to suit your needs. Trust me, it’s better than developing from scratch. I’ve also wrapped it all into a single function call, so you could do this:

$graph =& bargraph($datas);
$graph->done(array('name'=>'fruits'));

Here’s the code in it’s entirety
(more…)