Thursday, March 31, 2011

How to implement simple and easy search functionality on an index page

Here's how to implement simple and easy search functionality on an index page:

1. Let's say your controller looks like this:
public function actionIndex()
{
  $dataProvider=new CActiveDataProvider('Model');
  $this->render('index',array(
  'dataProvider'=>$dataProvider,
  ));
}
2. Change it to this:
public function actionIndex()
{
    $criteria = new CDbCriteria();

    if(isset($_GET['q']))
    {
      $q = $_GET['q'];
      $criteria->compare('attribute1', $q, true, 'OR');
      $criteria->compare('attribute2', $q, true, 'OR');
    }

    $dataProvider=new CActiveDataProvider("Model", array('criteria'=>$criteria));

    $this->render('index',array(
      'dataProvider'=>$dataProvider,
    ));
}
The above will read in the "q" (for query) parameter, and use the compare function to create the sql to search a few attributes for that value. Note the use of the 'OR' operator.

3. In your index view, add this:
<form method="get">
<input type="search" placeholder="search" name="q" value="<?=isset($_GET['q']) ? CHtml::encode($_GET['q']) : '' ; ?>" />
<input type="submit" value="search" />
</form>
The above creates a form that will submit to itself using the querystring. It displays a search input box, which is a text input box with a "cancel" command. It works in most browsers and defaults to a text field in the rest. When the user hits the search button, the form is submitted and the data is filtered by the search value.

Wednesday, March 30, 2011

How to hide index.php in the url in a Yii website

to hide "index.php" from the url on a website, add an .htaccess file to your web root, with this text:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

and in /protected/config/main.php, set:


'urlManager'=>array(
'urlFormat'=>'path',
   'showScriptName'=>false,
   'caseSensitive'=>false,

Friday, March 25, 2011

Adventures in CSS: a seal of approval

Here's how to make a nice "seal of approval" using only cascading style sheets. This only works in REAL browsers for now (webkit/firefox), but there are other options for IE

Approved!


it should look like this image:




Here we have a simple div tag:

<div class="seal">Approved!</div>

And we apply some css styles to it:

1. to make it a circle, apply border-radius:50%;
2. to rotate the div, use -webkit-transform: rotate(20deg);
3. give the background a nice radial gradient with: -webkit-gradient(radial, 50% 0%, 1, 50% 0%, 200, from(#B00000), to(#600000));
4. give it a subtle shadow with: box-shadow: 2px 2px 2px gray;

Here's the full CSS:



.seal
{
    height:200px;
    width:200px;
    text-align:center;
    color:white;
    font-family:Trebuchet MS;
    font-size:xx-large;
    line-height:200px; /*center text vertically*/
    background-color:#600000; /*fallback for other browsers*/

    border-radius:50%;
    -moz-border-radius:50%;

    -webkit-transform:rotate(20deg);
    -moz-transform: rotate(20deg);

    background: -webkit-gradient(radial, 50% 0%, 1, 50% 0%, 200, from(#B00000), to(#600000));
    background: -moz-radial-gradient(50% 0%, cover, #B00000, #600000);

    box-shadow: 2px 2px 2px gray;
    -moz-box-shadow: 2px 2px 2px gray;
}


Wednesday, March 16, 2011

How to override Yii's block radio button list labels and make them inline

By default, Yii radio buttons, radiobuttonlists, and checkboxlists look dumb. The label is on a different line, which is not a great design.

Here's how to fix it:

In your form style sheet (form.css), set:

input[type=radio] + label, input[type=checkbox] + label { display:inline !important; }

that's it, you're done.

What it does is says anytime you have a radio button with a label element directly after it, make that label inline instead of block, meaning that there won't be a line break.

I would also recommend changing Yii's default checkbox label positioning to before the label. 

Friday, March 11, 2011

How to change a file extension in NetBeans

I had a .html file that I wanted to change to .php, but NetBeans doesn't let you change the extension with the Rename or Refactor menu option.

To change the file extension in NetBeans, right-click the file and choose Properties. In the Properties window, under Extension, change the value, and hit Close. 

Wednesday, March 9, 2011

Yii: How to set a value in the parent layout from a child view

Say you have a search box at the top of your page, but you want to make it contextual to the area that you're in. For example, on the contacts view, you want the search button to say "Search Contacts". Your search button is on the layouts/main.php page. Here's how:

open protected/components/Controller.php

add a variable, such as "searchButtonArea"

in layouts/main.php, add that value to the search buttons, such as <?="Search $this->searchButtonArea"?>

then, in your contact views, set $this->searchButtonArea = "Contacts";

Or, add a property to the ContactsController, $searchButtonArea='Contacts';

Saturday, March 5, 2011

How to get debug information about your Yii application quickly

<?php

var_dump(Yii::app());

phpinfo();

?>
will tell you a lot about your current Yii app. This will show your database connection passwords, so be careful.