TOP

Ruby: Convert to Integer

To convert a string or other object into an integer (number) do this:

"23".to_i

This will produce 23 (as a number)

If you do something like this:

"Hello".to_i

You will get 0 returned, because Ruby can’t understand that object ans translate it into a number.

  • RSS
  • email
  • Facebook
  • Twitter
  • del.icio.us
TOP

Ruby: Convert to String

To convert a number to a string in Ruby, do this:

40.to_s
  • RSS
  • email
  • Facebook
  • Twitter
  • del.icio.us
TOP

CakePHP: Layouts

If there are things you want to appear on every page of your website, built in CakePHP, you will want to use a layout. I generally use one layout for header, footer, search, and other static elements. You can use multiples, but most of the time you won’t need more than one.

Layouts are created in the app/views/layouts folder, and are named with .ctp at the end, just like regular view files. The only difference with layouts is that they are applied to every view, as a sort of wrapper or warm blanket. This makes views truly just the content portion of your page. If you have ever used a header file in PHP, or a Master Page in ASP.NET, a layout is the same idea.

You need to name your first layout “default.ctp” as this is a special name that Cake — in it’s all knowing wisdom — will automatically pick up and use, without you telling it to do so.

Creating A Layout

Once you’ve created the file, you’ll need to add the basic contents of an HTML file, doctype, header, body, etc. It is, after all, just an HTML page with strange innards. I think I just made views equal to endtrails, but hey, this is a family blog, right? This will get you started, and I will explain after:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>sitename here - <?php echo $title_for_layout?></title>
<?php echo $html->css('styles'); ?>
<?php echo $scripts_for_layout ?> 
</head>
<body>
header html would go here
<?php echo $content_for_layout ?>
footer html would go here
</body>
</html>

You may have noticed a few things Cakeish and not ordinary HTML or PHP here, and if you haven’t well, get to noticing already!

$title_for_layout

This is the page title set in the controller action. So in app/controllers/posts_controller.php (replace posts with whatever your controller is named)

<?php
class PostsController extends AppController{
     function index(){
          // this line sets $title_for_layout
          $this->pageTitle = 'User Homepage';
     }
} 
?>

$html->css() link

This is the way to include a CSS file in the page, not unique to layouts, but useful none the less. Your css file needs to be in app/webroot/css/ and named whatever you want. In this case, $html->css(‘styles’); is linked to my css file named styles.css, so you leave off the .css when linking.

$scripts_for_layout

This acts as a pass-through, so any javascript files added to views will be loaded in the layout, so you can use them. This allows different views to have different sets of javascript files. To add a javascript file to a certain view, you’d want to use this:

$javascript->link('scriptname', false);

The false tells the script to load in the layout, not inline in the view.

If you want a javascript file to be used in all pages (that use this layout), simply add it directly to the header portion of the layout, like this:

<?php echo $javascript->link('scriptname'); ?>

$content_for_layout

This is the actual view content that will be rendered inside your layout. This is usually your content area that changes based on what page you’re on. This is the meat of the page, where the action happens. The layout is all on the outside looking in, going “wow, I’m boring, wish I did more than just held the content.” Meanwhile the content is all jumping up and down in the bouncy castle going “woo hoo!”

Using more than one layout

If you have more than one layout file, you will need to tell each controller action what layout it should be using.

To set a different layout file for an action, other than default:
In app/controllers/posts_controller.php (replace posts with whatever your controller is named)

<?php
class PostsController extends AppController{
     function index(){
          // this line sets the layout
          $this->layout = 'portal';
 
         // if not set, it uses the layout file named "default.ctp"
     }
}
?>

That, probably too simply, is a layout and how to use it.

  • RSS
  • email
  • Facebook
  • Twitter
  • del.icio.us
TOP

RubyNoob: The Genesis

Today I am starting to learn Ruby, and that so I can next learn Ruby on Rails. I have flirted with the RoR language, been a long-time fan, but today I become a first-time caller. I am tired of being on the outside looking into the red-tinted party in the hizouse. It is time for me to take up the banner of gemmed goodness.

This is the start of something new. RubyNoob will be my attempt to document my journey from Ruby Noob to somewhere past the gates of proficiency, and perhaps into expertise one day in the distant future. Join me won’t you?

I will be posting RubyNoob posts with all my notes and what I learned that day in Ruby. It may not be every day, as I may not have the time to focus on Ruby much, depending on deadlines and family stuff. Let me know what you think of what will be repeated struggle and incredulousity as I learn. If you are a Ruby guru, by all means, let me have it, tell me what I am doing wrong or what it is I need to do. I’m all ears for the best advice. Thanks, and I hope you enjoy this jaunt into the gun fight with a letter opener. Maybe you will learn along with me, and be able to write Ruby and/or Rails at the end of this.

  • RSS
  • email
  • Facebook
  • Twitter
  • del.icio.us
TOP

Ruby on Rails: Rake commands list

For a list of rake commands, fire up the command line and type:

rake -t
  • RSS
  • email
  • Facebook
  • Twitter
  • del.icio.us
TOP

WordPress: Enable New 3.0 Menus in Older Themes

Enabling 3.0 Menu Support

If you have WordPress 3.0 installed, but your theme is older and doesn’t support it, here’s how to make those fancy awesome new menus work in your theme…

  1. First, sign into the admin. This is located at www.yoursitehere.com/wp-admin
  2. Next, click on the Appearance Section.
  3. Click Editor
  4. Click Theme Functions (functions.php) on the right.
  5. Scroll down to the bottom of the file, and just before the closing tag “?>” copy this code:
    function navMenusEnable(){
    	add_theme_support( 'menus' );
    }
     
    add_action( 'after_setup_theme', 'navMenusEnable' );

    This will make sure your theme can use the menu system new in WordPress 3.0.

  6. Click Update File

Retrofit Your Theme Files

  1. While in this screen click Header (header.php) on the right
  2. Find the line (in almost all themes) where it looks like this:
    <?php wp_list_pages('title_li='); ?>

    It may not match this exactly, but you will see the wp_list_pages() function in there, with some random stuff in between the (). That is fine, don’t worry. All you do is place 2 slashes in front of the wp_list_pages() portion, like this:

    <?php //wp_list_pages('title_li='); ?>

    This disables the old pages menu. You do this with slashes so that if anything goes wrong or you don’t like the new feature, you can always just go back and take the slashes out, and it will work like normal again.

  3. Just above the line you just added slashes to, add this code:
    <?php wp_nav_menu( 'sort_column=menu_order' ); ?>
  4. Click Update File

The final code in the header should look like this:

<?php wp_nav_menu( 'sort_column=menu_order' ); ?>
<?php //wp_list_pages('title_li='); ?>

You should now be able to go to Appearance then Menus and create custom menus that will automatically show up in your site. Cool huh?

WordPress 3.0 rocks for this and a ton of other reasons. Let me know if you’d like to see more on WordPress programming. I enjoy it, and I hope I can help you enjoy it too.

  • RSS
  • email
  • Facebook
  • Twitter
  • del.icio.us
TOP

CakePHP: HTML Link

The HTML helper is pretty useful, here’s how you write a link in a view file:

<?php 
echo $html->link('click me',
     array(
          'controller'=>'users',
          'action'=>'index'
     )
);
 ?>

The parameters in this are:

  • title (what you actually click on)
  • link location array(controller, action, id (if needed for edit))
  • (optional) attributes array
    • RSS
    • email
    • Facebook
    • Twitter
    • del.icio.us
    TOP

    ASP.NET C#: DataTable Compute

    If you want to perform a sum or count on the records in a DataTable, you can, very simply.

    You’ll have to have a DataTable with something in it, like results from a database, then you do this:

    // my DataTable is full of customer data and is named "dt"
    object newCustomerCount = dt.Compute("count(id)","CustomerID > 100");

    Parameters are both strings, (expression,filter)

    The Compute function will return as an object, so it is a good idea to have your variable be an object too. The problem is that you often need an integer (number) or a text string to put into a label or textbox. Here’s how you get it there:

    // this casts to an integer
    int var1 = (int) newCustomerCount;
     
    //this converts to a string
    lblNewCustomerTotal.Text = newCustomerCount.ToString();

    As my ASP.NET instructor Dana taught us, everything derives from OBJECT, and everything has a ToString() method. Moo Dana, good form.

    • RSS
    • email
    • Facebook
    • Twitter
    • del.icio.us
    TOP

    MySQL: Create a table via code

    To hand-code a table, or create a bunch of tables in a script, use this:

    CREATE TABLE `tablename`(
         `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
         `name` varchar(255) NOT NULL
    )

    You may note the “backticks” which are the little symbol on the same key as the tilde “~”, and are used to note the tablename and fieldnames.
    INT(11) tells the database that this is an integer no longer than 11, makes sense, right? “NOT NULL” means nulls are not allowed. “AUTO_INCREMENT” means this is the id field that will automatically add one to itself every time you create a record. Lastly “PRIMARY KEY” tells us that this will be the field the database tries to keep in order via indexing.

    You can add as many fields as you want, of different types, as you can see with the name field shown above. For a more complete reference on creating tables, visit the MySQL Create Table page (version 5.1, pretty common, but not everywhere yet).

    • RSS
    • email
    • Facebook
    • Twitter
    • del.icio.us
    TOP

    SQL: LIKE Searches

    There is no pause in there, “LIKE!…searches!” This is a no teeny-bopper zone (unless you want to program stuff, then by all means)! When you know only part of what you’re looking for in a SQL SELECT statement, especially for AJAX auto-complete queries, you will need to employ the all-consuming power of the “LIKE Search” also known as the “Contains Search” by minds whom SQL eats for breakfast (sorry if that’s you).

    Regular SELECT query that finds an exact result:

         SELECT * FROM Customers WHERE CustomerName = 'Microsoft'

    I guess you would dub this a literal search. This produces one result: Microsoft

    A SELECT query that finds a bunch of matching but ambiguous (kinda fuzzy, hazy) results:

         SELECT * FROM Customers WHERE CustomerName LIKE 'Micro%'

    This is more of a wider search for possibilities. This produces more results: MicroCenter, MicroSoft, MicroDerm Abrasionists, MicroBats, MicroMachines, inc. See how this would be helpful in an AJAX auto-complete?

    The magic in the LIKE search is that % (percent sign). It tells SQL to give me anything after my “Micro.” As you can see above, it brings you a bunch of search results like a dog with way too many bones. You can also use LIKE ‘%Micro%’ to find things with stuff on both ends. Sometimes this is way more efficient and useful.

         SELECT * FROM Customers WHERE CustomerName LIKE '%Micro%'

    Produces aMicrowave, Microsoft, MicroCenter, armpitMicro, etc.

    Armpit? Do you see what just happened there, wow, it must be late or something.

    • RSS
    • email
    • Facebook
    • Twitter
    • del.icio.us