Latest |Kites |Pictures |Programming |Life
?
[filed under Programming]C# silverlight and all that shit

Joe Stevens is my geeky brother in law.

27th of July, 2009@10:50:41 PM
permanent link to article
[filed under life]Blair

Blair Julian - JOIN FACEBOOK - ok thx bye

24th of July, 2009@3:16:25 PM
permanent link to article
[filed under Programming]PHP code style
[filed under Programming]NuSphere PHPeD firefox debug toolbar

Tags: NuSphere PHPeD firefox debug toolbar

There is an open source Firefox toolbar for NuSphere PHP IDE (NuSphere PHPeD firefox toolbar). It was written by a kind chap called Ian Flory.

Firefox 3.5 beta 99 Preview is out and I use it, but the toolbar is locked to Firefox versions 3.0.* and lower. The toolbar needed an update, so a little hacking later and here is the new version:

DBGbar: phpdebugger.xpi

Fixed:

Changed the min version to 3.5.*
Changed how the toolbar flexes and fills the space - it is now fixed width.
Updated the 'options' window text.

Comments:

This Firefox add-on build script was useful.
I drag and drop the toolbar up next to the 'File, Edit, View...' menus, so when it flexes to fill all the horizontal space it really annoys me. I also drag and drop some bookmarks up here and a few Facebook buttons.

If you find this useful leave a comment :)

Open source (LGPL), flash charts (pie, bar, line, scatter, etc): open flash chart 2. Tooltips, animation and JSON friendly.

 

11th of June, 2009@9:41:27 AM
10 comments, permanent link to article
[filed under Programming]zend framework jeditable

Tags: zend framework jquery.jeditable.js JSON

jeditable does not accept JSON in response to the AJAX call. So how can you get this to work with Zend Framework? Like this:

Your view will need the the .js files:

<?php
$this->headScript()->appendFile($this->baseUrl().'/js/jquery-1.2.1.pack.js');
$this->headScript()->appendFile($this->baseUrl().'/js/jquery.jeditable.js');
?>

In your controller you'll need to add a context swtich to return JSON, not HTML:

    public function init()
    {
        parent::init();
       
        //
        // NOTE you need **format** in the URL, http://url/url/format/json/
        //
        $this->_helper->contextSwitch
            ->addActionContext('my-editable-ajax', 'json')
            ->initContext();
            ...

You action will look like this:

    function myEditableAjaxAction()
    {
        //
        // This is a serious PITA. The editable expects a single value
        // is response, not a JSON object, so turn off AUTO json
        //
        $this->_helper->contextSwitch()->setAutoJsonSerialization(false);
        $value = $this->_getParam('value', '');
       
        echo 'got-'.$value;
    }

 

The URL you need to paste into the editable config will need the JSON context switch in it:

$(".edit_area").editable("<?= $this->url(array('action'=>'my-editable-ajax', 'format'=>'json')) ?>", {
  cancel    : 'Cancel',
  submit    : 'OK',
  indicator : "Saving...",
  tooltip   : 'Click to edit...',
  placeholder : '&nbsp;'
})

Hope that helps.

 

 

13th of May, 2009@9:37:15 AM
add a comment, permanent link to article
[filed under Programming]zend framework sql error reporting adapter

This post is about Zend Framework, sql error reporting.

Problem:
If your SQL has an error in it, Zend Framework throws a Zend_Db_Statement_Mysqli_Exception, the default exception message is pretty pants and does not show the whole SQL query (it only shows the first 10 characters or so!)

Lets fix it so it shows the whole query.

I found this neat class that reports the SQL, it work by extending the Zend_Db_Adapter_Pdo_Mysql class. I use MySQLi, so I adapted it so it works for me:

class MyCompany_Db_Mysqli extends Zend_Db_Adapter_Mysqli
{
   
    public function query($sql, $bind = array()) {
        try {
            return parent::query($sql, $bind);
        } catch (Exception $e) {
           
            if( $this->getProfiler()->getEnabled() )
            {
                echo '<span style="color: red; font-size: 20px;">MyCompany_Db_Mysqli adapter Error</span>';
                echo '<div style="font-family: monospace; padding: 10px; margin: 10px; border: 2px solid pink;">';
                echo nl2br($sql);
                echo '<hr>';
                echo '<pre>';
                echo var_dump($bind);
                echo '<pre>';
                echo '</div>';
            }
            else
            {
                // no profiler
                throw $e;
            }
        }
    }
}

The whole thing is a little bit quick and dirty. I wanted to see if it worked and post a blog entry about if it did.

You need to save the file into your library code (replace MyCompany with your library directory name) in the Db directory.

To use it you want to find where you load your database adapter, I do this in my controller init() function. My init() looks like this:

class ProjectsController extends Zend_Controller_Action
{
    //
    // this is called from __construct
    //
    public function init()
    {
        parent::init();
       
        $config = new Zend_Config_Ini('/var/www/ZF-apps/config.ini', 'general');
       
        //$db = Zend_Db::factory($config->projects->db);
        $db = new Guava_1_Db_Mysqli($config->projects->db->params);
        $db->getProfiler()->setEnabled(true);
        Zend_Db_Table::setDefaultAdapter($db);
   ...
   ...

Note how I have commented out how I used to get my adapter. Also see how the params are loaded from an .ini file and used slightly differently.

How to use it:
On my staging (development) server I use the SQL profiler (the highlighted blue line), so if the profiler is enabled then I'd like to see the SQL errors. If I am not profiling (on the live server) then I just raise the error like normal.

You'll want to add your own logic into the adapter so it emails off the error report (or pasts it into an RSS feed or something)

 

3rd of April, 2009@8:04:05 AM
1 comments, permanent link to article
[filed under Programming]XUL Firefox add on sidebar open close events

Tags: Firefox 3.x add-on sidebar.

What this post is about

I am coding a FIrefox add-on. It is fun. I am making a sidebar that you can drag and drop stuff onto. I need to know when the user has opened it or closed it (so I can persist the data) so after a lot of googling I figured it out.

The code

This is how you can watch the open and close events of a sidebar. In your sidebar javascript file put this in:

var skratch = {
    onLoad: function(e) {
        alert('load');
    },
    onUnload: function(e) {
        alert('close');
    }
}

window.addEventListener("load", function(e) { skratch.onLoad(e); }, false);
window.addEventListener("unload", function(e) { skratch.onUnload(e); }, false);

Hope that helps someone.

Use this with 'save to file' to persist data in your add-on - happy days. Happy fuckin days, man.

Here is some save to file goodness (note: the code is GPL):

http://code.flickr.com/ ... /uploadr/file.js?rev=501

25th of March, 2009@12:08:13 PM
4 comments, permanent link to article
[filed under Programming]MySQL compare sub query null

Hey, this is cool. I had a query like so:

SELECT stuff
FROM table
WHERE
col = ( SELECT col FROM table_2 WHERE id IN (1,2,3) )

And the sub query was returning NULL sometimes. So I wanted the rows from table where col is null. But the syntax to do that would be:

SELECT stuff
FROM table
WHERE
col IS ( SELECT col FROM table_2 WHERE id IN (1,2,3) )

Now this query isn't going to work when the sub query returns a number. The answer is to use this funky operator thatsolves this problem <=>, the null safe equality operator:

SELECT stuff
FROM table
WHERE
col <=> ( SELECT col FROM table_2 WHERE id IN (1,2,3) )

Smooth :-)

28th of October, 2008@12:00:28 PM
8 comments, permanent link to article
[filed under Programming]Zend Framework MySQL DB Pagination Tutorial

Tags: Zend Framework 1.7 Pagination MySQL SQL Pages Paged Page Next Prev Previous

Just a quick how-to-make a MySQL table paginatior in Zend Framework.

You'll need these as a reference, zend.paginator.usage and zend.db.select.

First in your controller make a link to MySQL and make an SQL select() object (I find this disgusting syntax, but hey ho)... So, in the following code I am in my controllers action method:

public function myTableAction()
{

    $db = Zend_Db::factory( $this->config->db );
       
    $select = $db->select()
            ->from(array('p' => 'people'), array('id','name'))
            ->order('name');
   
    //echo $select->__toString();
    $this->view->paginator = Zend_Paginator::factory($select);

    $this->view->paginator->setCurrentPageNumber($this->_getParam('page'));
}

So you have passed the MySQL syntax object to the paginator. This will add the LIMIT to the SQL and run it. The LIMIT syntax is built up from the page you are on and how many items per page you are displaying, this information is grabbed from the URL.

Next you need to make your view show this data and the:<prev 1 2 3 4 5 next >HTML. The paginatior object we passed to the view in the above code (the second to last line) is an iterator, so we can just loop over it:

<table class="my_table">
  <thead>
    <tr class="table_head">
      <th>People</th>
    </tr>
  </thead>
  <tfoot>
    <tr class="table_foot">
      <td colspan="4"><?= $this->paginationControl($this->paginator, 'Sliding', 'partials/search-pagination.phtml'); ?></td>
    </tr>
  </tfoot>
 
  <tbody>
    <?= $this->partialLoop('partial-loops/people.phtml', $this->paginator) ?>
  </tbody>
</table>

OK, so the bit that builds the table is the partialLoop, we pass our pagination object that contains our table rows to the partialLoop iterator. This builds each <tr> row from the paginatior, the paginatior got the data from the MySQL table (after it manipulated your SQL adding a LIMIT 10,10) and got the item count from your table (it altered your query adding a COUNT() and re-queried the DB)

The loop partial looks like this (partial-loops/people.phtml):

<tr>
    <td><?= $this->name ?></td>
</tr>

Note how we have access to the two columns from MySQL as object properties. BTW I keep my view .phtml files in views/scripts and my partials in views/scripts/partials and my loop partials in views/scripts/loop-partials. You don't have to follow what I do, just make sure your paths make sense.

The style of the:<prev 1 2 3 4 5 next >HTML is in the partials/search-pagination.phtml view, this you can copy from this page (zend.paginator.usage -- Example Paginations Controls (at the end of the page)) and save it in the correct script view directory. Mine is a modified version (partials/search-pagination.phtml):

<?php if ($this->pageCount): ?>
<div class="pagination">

<?php if (isset($this->previous)): ?>
  <a href="<?= $this->url(array('page' => $this->previous)); ?>">&laquo; PREV</a> -
<?php endif; ?>

<?php
    /* Page links */
   
    foreach ($this->pagesInRange as $page): ?>
    <a href="<?= $this->url(array('page' => $page)); ?>" <?php if($page == $this->current): ?>id="selected"><?php endif; ?><?= $page; ?></a>
<?php endforeach; ?>

<?php if (isset($this->next)): ?>
 - <a href="<?= $this->url(array('page' => $this->next)); ?>">Next &gt;</a>
<?php endif; ?>

</div>
<?php endif; ?>

That's it really. You'll need a bit of CSS to style the links.

For more info take a look at the Zend Framework wiki: Zend_Paginator

Job done.

 

28th of October, 2008@10:15:03 AM
17 comments, permanent link to article
[filed under Programming]Zend Framework Menus Navigation

Tags: Zend Framework, menus, controller, action, navigation, template, menu.phtml, actionStack, setResponseSegment, renderScript

This is how to put menu logic in your web page.

OK, from the get go, this is hard. You may need to read it a couple of times or do more research.

What we want is a web page with a menu system (a list of items) on the left of the page. The menu will be different for each controller and action.

What usually happens in ZF is the ApplicationController class is created and the method indexAction is called. indexAction then uses the model to get data and passes info to the view (for example using $this->view->data = 99; ). The view is then rendered as a string, which is appended to the Layout content.

It took me a while to figure that out.

To make the menu we need a seperate controller, the MenuController will decide what to show on the menu. The the menu view will render it and we capture the output of the view into a layout variable. The menu HTML is used later in the layout.phtml template.

In our application controller we push the "menu" onto the actionStack which makes ZF run MenuController::applicationAction next.


ApplicationController.php
class ApplicationController extends Zend_Controller_Action
{
    public function __construct(...)
    {
        //...
        // this tells the framework to run the MenuController after this
        $this->_helper->actionStack('application', 'menu');
    }
}

MenuController.php
class MenuController extends Zend_Controller_Action
{
    public function applicationAction
    {

        // we don't want to append the menu to the end
        // of the layout content, so:
        $this->_helper->viewRenderer->setResponseSegment('menu');
        $this->view->menu = array('x', 'y', 'z');
    }

   
    public function anotherAction
    {

        $this->_helper->viewRenderer->setResponseSegment('menu');
        $this->view->menu = array('a', 'b', 'b');
    }

}

layout.phtml (/layouts/)
<html><head></head><body>

<?= $this->layout()->menu; ?>

<?= $this->layout()->content; ?>

</body></html>

application.phtml (in /views/scripts/menu/)
<ul>
<?php foreach( $this->menu as $m ): ?>
  <li><?= $m ?></li>
<?php endforeach; ?>
</ul>


another.phtml (in /views/scripts/menu/)
<ul>
<?php foreach( $this->menu as $m ): ?>
  <li><?= $m ?></li>
<?php endforeach; ?>
</ul>

The MenuController changes the layout response segment to 'menu', all the HTML in the menu view is saved to this variable.

This HTML to make the menu is then merged into the web page in layout.phtml.

More notes on Zend Framework Menu Navigation.

10th of September, 2008@10:08:48 AM
21 comments, permanent link to article
Server Grind [0.2474 seconds]