CakePHP: Get pagination page count as variable, Hide page count if only one page

Very useful tip from this guy – http://wp.headynation.com/cakephp-get-pagination-page-count-as-variable-hide-page-count-if-only-one-page/

 

If you’re using pagination in a view but you only want to display the links where there’s more than one page you can get the page count as follows:

 

$this->params['paging'][<MODEL NAME>]['pageCount']

Using a static home page on wordpress

I keep having to look this up so here’s a breakdown of what you need to do to use a static home page for a WordPress based website.

  • Log into Admin area
  • Go to – Settings – Reading
  • Check – A Static Page
  • Select Page to be used as home page
  • Go to Settings – Permalink – select Custom Structure and enter – /%postname%

NB. If you’re going to be using the Blogging facility for a News section or something similar then don’t name your page template – home.php or you’ll end up with a conflict. To display your Posts in News etc you’ll need to select the Posts Page in – Settings – Reading.

Dates in php part 2 – saving dates to mysql

Came across a problem today where I needed to save value from a jQuery datepicker in dd-mm-yy format to a mysql database date column.

Solution:

$expiry = new DateTime($expiry);
$expiry = date_format ( $expiry, ‘Y-m-d’ );

Then to display it in “normal” format when you retreive it from the db table:

$expiry = new DateTime($expiry);
echo date_format($expiry,’d-m-Y’);

Dates in php part 1

Let’s say you’re querying a MySql Database and it has a datetime field called ‘datecreated’ – you will actually end up with an error if you try to apply date_format to the output.

e.g.  Let’s say we have a record with the value – 2011-06-06 15:32:12 – in the database column

if we do this:

<?php echo date_format($row_quote['datecreated'], “d-M-Y”) ; ?>

We will get an error something like:

Warning: date_format() expects parameter 1 to be DateTime, string given

Instead we need to use the following:

<?php echo date_format(new DateTime($row_quote['datecreated']), “d-M-Y”) ; ?>

There’s some pretty useful stuff here on formatting dates.

JQuery Form Validation – .submit function

JQuery makes form validation a snap with the .submit function. The example below is fairly self explanatory. In brief we place the .submit function in the .docready function, use .after to add text after any empty form fields with the css class .required.

<html>
<head>
<style>
.error{color:#f00;}
</style>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script>
<script type=”text/javascript”>
$(function() {

$(‘#form1′).submit(function(){

if($(‘#firstname’).val() == ”){
$(‘#firstname’).after(‘<p><span>please enter a first name.</span></p>’);
$(‘#firstname’).focus();
return false;
}

if($(‘#lastname’).val() == ”){
$(‘#lastname’).after(‘<p><span>please enter a last name</span></p>’);
$(‘#lastname’).focus();
return false;
}

});

// onchange for text fields
$(‘.required’).blur(function(){
if($(‘.required’).val() !== ”){
$(‘.error’).remove();
}
});

// ends doc ready
});
</script>
</head>
<body>

<form  name=”form1″ id=”form1″ action=”index.html” method=”post”>
<p>First name: <input type=”text” name=”firstname” id=”firstname” /></p>
<p>Last name: <input type=”text” name=”lastname” id=”lastname” /></p>
<input type=”submit” value=”Submit” />
</form>

</body>
</html>

Like This!

SQL Subselects

Let’s say you’ve got three tables, Cars, Makes, Models. What if you need to get a list of cars including their Make and Model?

Here’s where subselects come in.

The Tables:

Cars
—————
id
MakeID
ModelID
Colour
ImageLink

Makes
———
id
Name

Models
———-
id
MakeID
Model

And here’s the query:

select c.id,
(select name from makes where id = c.makeid) as make,
m.model as model
from cars c
inner join models as m on c.modelid = m.id