Category Archives: PHP

Quick tip: How to run php script like application

It’s really quick tip:
Sometimes you need to make a shell script which need to be executed either on regular basis, or like infinite loop.

You always can run it by:

/usr/bin/php /path/to/script.php

but if you add a shebang line, the script can be run as standalone application.

Example:

#!/usr/bin/php -q
<?php
//Your php code goes there
?>

Then you can run it like:

/path/to/script.php

Hope this helps

How to check your php project (many files in a folder) for parse errors

Hey hadn’t wrote anything for a while here, but this is so brilliand and convenient, so I couldn’t resist to share it.

It’s about to check your project for obvious errors before releasing the changes on the production server. I found this answer in StackOverflow, but the solution is for SVN repository and while currently I am working with snv it suits me, but looking forward to migrate to Git, I was thinking “Wasn’t it nice to check the all php files within a folder?”

So here is the solution:

for i in $(find . -iname \*.php ) ; do php -l $i ; done | grep "Parse error"

This should be executed in terminal (at least in Ubuntu it is straight forward) and it print all files which had Parse errors within the folder. I think it’s a must for any php developer who release their code in production server. I already put it as alias in my .bashrc, but I will add it in my “build” script too.

Probably it could be optimized, but for me is pretty much enough.

Prestashop :: adding text blocks in the template

Here you can see how you can add html and text on various places of your template.

Presta is very good written e-commerce platform and class overwriing is piece of cake. So, here is the code:

1. Create a file under {presta_dir}/override/classes and name it Tools.php.
2. Copy and paste that code in the new file:

<?php
/**
 * Tools override class
 */

class Tools extends ToolsCore{
    public static function getPage($link_rewrite = null){
        global $cookie;
        $page = Db::getInstance()->getRow("SELECT * FROM "._DB_PREFIX_."cms_lang WHERE id_lang='".(int)($cookie->id_lang)."' AND link_rewrite LIKE '".$link_rewrite."'");
        return $page['content'];
    }
}

Note: if you already have this file just copy only the function and place it inside the Tools class.

3. That’s it ๐Ÿ™‚ Go and create a new CMS page under Prestashop Admin (Tools->CMS). Add a nice “friendly url” and put your your text in the body.
4. How to use the function in the template: Go to your template file files and insert the following code:

{Tools::getPage('your-seo-friendly-url-cms-page');}

Don’t forget to delete the cache.

Conclusion: The code is not 100% MVC, but it’s working straight away. As benefit your texts will be multilingual, and especially when you need to put them on places where there is no hooks, This would save a lot of time and effort.

Hope this helps.

Error: invalid XML tag syntax and Extplorer

A client of mine uses standalone version of Extplorer as Client section of his website. Because they are Studio for Printing materials, it’s normal, that their “production” is quite large as file size.

Recently they asked me to increase the max_upload_filesize to more than 4-5Gb. In the php.ini I’ve set max_upload_filesize=60G, but when someone try to login, it hangs and in the Firebug console it says “invalid XML tag syntax”.

The solution was to replace 60G with 6000G which is equivalent to ~6Gb. After that change the extplorer start working normally.

Hope this will help someone.

Quick tip: WordPress – How to make Contact Form 7 working with qTranslate

On my recent project I had to create multilingual site and we decided to add multiple languages using qTranslate plugin.

The site of course has “Contact us” page and we decided to use Contact Form 7 plugin because it’s rich and flexible plugin for creating contact forms.

So far so good, but we detected that when the language is different than the default one, the form submission is redirecting to the default language page and because the forms are different they didn’t handle properly the errors as well as the thankyou message.
Continue reading

Simple value getter and setter for CakePHP

This snipped I’ve wrote, because I think it’s convenient to have something like this in cake.

Put this in AppModel (/youur_cake_project/app/app_model.php)

<?php
class AppModel extends Model {
    /**
     * handy method for getting and setting value in a model.
     * For setter: it's not validating anything, so bear in mind...
     * Usage:
     *   Getter:
     *    $this->MyModel->id = 5;
     *    $this->MyModel->value('name'); //retun value of column name for the id 5
     *   Setter
     *    $this->MyModel->id = 5;
     *    $this->MyModel->value('name', 'BlaBla'); //set the name of row with id 5 to BlaBla
     */

      public function value($field, $value = null){
    if(func_num_args() == 1){ //getter
        $result = $this->field($field);
        if($result){
        return $result;
        }
    } else { //setter
        if($this->saveField($field, $value)){
        return true;
        }
    }
    return false;
     }
}?>

Getter:

$this->YourModel->id = 5; //set the id of the model
$this->YourModel->value('name');//return the value of the row with id 5

Setter:

$this->MyModel->id = 5; //set the id of the model
$this->MyModel->value('name', 'BlaBla'); //set the name of row with id 5 to BlaBla

As you can see it’s not a rocket science, and it’s uses cake’s functions for this, but I thought it’s convenient to have such function.

CakePHP saveAll() quick tip

Saving data with Model->saveAll() is really time savior. There is something specifics while saving multiple records of a single model.

The data provided from the form should be in the numerically indexed array of records like this:

Array
(
    [Article] => Array(
            [0] => Array
                (
                            [title] => title 1
                        )
            [1] => Array
                (
                            [title] => title 2
                        )
                )
)

Your code for saving this data with saveAll() should look like this:

$this->Article->saveAll($data['Article']);

Ok, but if you pass only

$this->Article->saveAll($data);

it won’t work. So if you want to save multiple records of a single model provide the proper data. It took me 1/2 hour to detect that this is the problem.

Of course this is written in the CookBook, but somebody need to read it carefully ๐Ÿ˜‰