Parse error: syntax error, unexpected $end in /app/views/layouts/default.ctp – CakePHP

I just encountered an unexpected error while creating a portable development server on a USB stick using the latest version of XAMPP portable. This particular application runs on the CakePHP framework and was copied directly from another functioning Windows based machine.

After scratching my head a few moments I realized that the default configuration for XAMPP’s php.ini may be different.

The fix is easy. Go into your php.ini for your php installation and change the following from ‘OFF’ to ‘On’:

short_open_tag = on

Then simply go and restart your apache to apply the changes.

Specify Stylesheets for Different Media Types with $html helper

I’m expanding my CakePHP experience and now I’ve delved into displaying the application on multiple devices and of course, print.

Print stylesheets can be tricky to design, but it took me a while to figure out how to have cake automatically add them to the template using the $html helper.

<?php 
	//you specify the file name first, then the media types at the end.
	echo $html->css('simple', null, array('media'=>'print'));
	//
	
	echo $html->css('regular', null, array('media'=>'screen')); 
	echo $html->css('simple', null, array('media'=>'print')); 
?>

This will output something like this:

<link rel="stylesheet" type="text/css" href="/css/simple.css" media="print" />

CakePHP: Join results from multiple tables

Have you ever wanted to expand on your pre-set model queries with some table joins or other fancy footwork? You can join your model query to another table or tables like so:

				
$market = $this->Market->find('list',
	array(
		'joins' => array(
		array(
			'table' => 'companies_markets',
			'alias' => 'CompaniesMarkets',
			'type' => 'left',
			'foreignKey' => 'CompaniesMarkets.market_id',
			'conditions'=> array('CompaniesMarkets.market_id = Market.market_id')
		)
	),
	'conditions' => array(
			'CompaniesMarkets.company_id' => $company
			),
			'fields'=>array('Market.market_id','Market.market_name')
	)					
);

What’s we’re doing here is specifying the ‘joins’ option along with a slew of other pieces to designate what to join, how, and by which field. The ‘conditions’ simply sets which fields I want to check for a certain value and which fields I want to use for this list.

How To: CakePHP form input without labels

One of the great things about CakePHP is how easy it is to get very common tasks accomplished in a short amount of time. Form building is no exception, but there are many options to specify – and at times those options are not immediately apparent.

The standard input code syntax appears like this:

input(string $fieldName, array $options = array())

To have your form inputs not show labels, you can specify ‘label’=>false in the input options.

For example:

<?php
echo $form->create(‘User’, array(‘action’ => ‘login’, ‘class’=>’header-login’));
echo $form->input(‘username’, array(‘label’ => false));
echo $form->input(‘password’, array(‘label’ => false));
echo $form->end(‘Login’);
?>

This will show your form inputs without the label elements.