SWFIR – great way to style images

JavaScript No Comments »

SWFIR (swf Image Replacement), it is a great way to add some style to images inside a web page escaping from the limitations imposed by the HTML but respecting the standard design concepts. This is done using JavaScript and Flash. Using swfir is simple and you don't need any advanced knowledge about Flash or JavaScript. Just download the swfir_v1.zip and unzip it, then upload the 2 files (swfir.js and swfir.swf) from it to the same directory in your website. To make it work all you need to do is include the JavaScript file and then to choose the images that will be styled and features applied to them.
The .js file need to be included in the "head" of the document:

PHP:
  1. <script type="text/javascript" src="swfir.js"></script>

I will use some styling features to the following image:

PHP:
  1. <img src="picture.jpg" alt="Here is some alt text" id="pic2" />

To call swfIR at work you have to use something like:

PHP:
  1. <script type="text/javascript">
  2. window.onload = function(){
  3.   round = new swfir();
  4.   round.specify('src', 'swfir.swf');
  5.   round.specify('border-color', '#cccccc');
  6.   round.specify('border-width', '1');
  7.   round.specify('border-radius', '10');
  8.   round.specify('shadow-blur', '5');
  9.   round.swap("#pic2");
  10. }
  11. </script>

The code above need to be situated inside the "body" section. I used some parameters (there are more that can be used/tested) to style my "pic2" image.
The result is something like this:

Example

It works fine in Firefox 2.0 and IE 6.0, unfortunately on some cumputers with SP2 failed to run in IE 6.0, nut sure why but you can write to the SWFIR team.

I will fix the errors

General 1 Comment »

I see some errors in my blog caused by the code inserted in some examples, I have to find a way to fix this, maybe using a wordpress plugin to deal with the "plain text" code. Also it will help visitors to distinguish easier the post's text from the code examples.

CakePHP, Simple Contact Form, email sending

CakePHP 13 Comments »

In this post a I will show an example of a simple contact form which sends an email to a specified address, done using CakePHP.
The form has 3 fields: name, email address and message.
Firs of all let's see the "view" for the form displaying. It looks like this (app/views/controller_name/contact.thtml):

PHP:
  1. <?php if (isset($succes)){?> // $succes is the variable which is verified to see if email was sent
  2.      <p>The message has been sent.
  3.      <br />Thank you!
  4.      </p> // Message displayed if success
  5. <?php }else{?>
  6.      <form method="post" enctype="multipart/form-data" action=”<?php echo $this->webroot;?>controllername/contact/”>
  7.      </form>
  8.  
  9. <?php if (isset($erroare)){?> // Verify if is an error and then display error message
  10.       <p>Please complete all fields!</p>
  11. <?}?>
  12.  
  13. <p>
  14.     Name: &nbsp;&nbsp;&nbsp;&nbsp;<?php echo $html->input(’Page/name’, array(’size’ => ‘25))?>
  15. </p>
  16.  
  17. <p>
  18.     Email: &nbsp;&nbsp;&nbsp;&nbsp;<?php echo $html->input(’Page/email’, array(’size’ => ‘25))?>
  19. </p>
  20.  
  21. <p>
  22.    Message: &nbsp; &nbsp; &nbsp; <?php echo $html->textarea(’Page/message’, array(’cols’ => ‘40′, ‘rows’ => ‘10))?>
  23. </p>
  24.  
  25. <p>
  26.    <?php echo $html->submit(’Send email’)?>
  27. </p>
  28. <p>
  29.    All fields have to be completed!
  30. </p>
  31. </form>
  32.  
  33. <?php }?>

Now we need a small component to verify the email address to be a valid one and also to contain the code necessary for the email sending. The component (app/controllers/components/email.php) is looking like this:

PHP:
  1. <?php
  2.  
  3. class EmailComponent extends Object
  4. {
  5.  
  6.     function email($email, $name, $message)
  7.     {
  8.       $f_mail = "name@domain.com";
  9.       $f_name = "WebMaster domain.com";
  10.       $f_message ="\nName : $name";   // start building the message
  11.       $f_message .="\n\nE-mail : $email";
  12.       $f_message .="\n\nMessage \n : $message";
  13.  
  14.       $headers = "From: www.domain.com <administrator@domain.com>\n";
  15.       $headers .= "X-Sender: <admin @domain.com>\n";
  16.       $headers .= "X-Mailer: PHP\n";
  17.       $headers .="Reply-To:$f_mail\n\n";
  18.      
  19.       $subject = "Message from your website domain.com";
  20.      
  21.       mail("$f_mail", "$subject", "$f_message",$headers); // send the email
  22.  
  23.     }
  24.  
  25.  
  26.     function validMail($email) // this function checks to see if the email supplied by the visitor is a valid one
  27.     {
  28.         $pattern="^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$";
  29. // the regex for an email addres
  30.      
  31.      if(eregi($pattern, $email))
  32.         $ll=1;
  33.      else
  34.         $ll=0;
  35.      return $ll;
  36.     }
  37. }
  38.  
  39. ?>

And now becomes the logic part, the controller (app/controllers/controller_name.php):

PHP:
  1. <?php
  2.  
  3. class NameController extends AppController {
  4.  
  5.     var $uses = null;
  6.     var $components = array('Email');
  7.  
  8.     function contact(){
  9.  
  10.         $this->pageTitle = 'Contact';
  11.  
  12.         if (!empty($this->data))
  13.         {
  14.  
  15.           $email = $this->data['Page']['email'];
  16.           $name = $this->data['Page']['name'];
  17.           $message = $this->data['Page']['message'];
  18.  
  19.           $error = 0;
  20.  
  21.            // start filter vars
  22.           if(strlen(trim($email))<3)
  23.           {
  24.             $error=1;
  25.           }
  26.  
  27.           if(strlen(trim($message))<4)
  28.           {
  29.             $error=1;
  30.           }
  31.  
  32.          // verify email address
  33.           if( (strlen(trim($email))<3)||($this->Email->validMail($email)==0))
  34.           {
  35.             $error=1;
  36.           }
  37.  
  38.           if ($error==0)
  39.           {
  40.  
  41.             $this->Email->email($email, $name, $message)//here is the email sent
  42.             $this->set('succes', 'The message was sent <br />Thank you!');
  43.             // setting "succes" variable for displaying if email was sent.
  44.  
  45.           }else
  46.           {
  47.             $this->set('error', 'Please complete all fields');
  48.             // this is in case of error
  49.           }
  50.  
  51.       }else{
  52.        
  53.       }
  54.  
  55.   }
  56.  
  57. }?>

 

About Linkbaiting

SEO No Comments »

One of the most “productive” way to attract visitors to your website / webblog relying on your content is to use a technique called linkbaiting.
Everything you need to do, to be able to use this feature, is to ensure that you wrote a good (unique) content on your website, a content that people will like and their attention will be caught in such manner that they will post a link towards your website.
This content you have is also called a possible “hook” for the visitor that will point a link to you. Through time there were some attempts to categorize this “hooks”, most encountered are:
- Informational Hooks – described as a content full with good information that can be usefull
- News Hooks – content related with fresh informations about some domains
- Hummor Hooks – funny content including jokes, pictures, movies
- Evil Hooks – means writing some bad things about a well known product / blogger / website, that sometimes can attract a lot of audience.
- Tool Hooks – creating a useful online tool that people will link to it.

HasOne association type

CakePHP 2 Comments »

A powerful feature in Cake is the relational data association provided by the model definition. There are four types of associations:
- hasOne
- hasMany
- belongsTo
- hasAndbelongsToMany

After we define the association between 2 models Cake will automatically fetch the data between the “glued” models. For example if the Car model is related to the Image model and is using a hasOne association type when we will retrieve the data using findAll for example in a controller there will be retrived both Car and Image records: $this->Car->findAll()
To use this types of relations correctly is indicated to use naming conventions like:

- Foreign Keys: singular-model-name_id. For example, a foreign key in the "images" table pointing back to the Car a given Image belongs to would be named "car_id".
- Tables name should be at plural
- Model names should be singular form of the tables names.

Example of association and querying data with hasOne between two models Car model and Image model in this cases:

Location: app/models/car.php

PHP:
  1. class Car extends AppModel
  2.   {
  3.     var $name = 'Car';
  4.     var $hasOne = array(Image' =>               
  5.        array('className' => 'Image',
  6.        'conditions' => '',
  7.        'order' => '',
  8.        'dependent' => true,
  9.        'foreignKey' => 'car_id'
  10.        )
  11. // the hasOne array is what Cake is using to create the association between this 2 models
  12.     );
  13.   }

When we will retrieve data using find() or findAll() calls upon Car model we should see our associated Image model data as well.

PHP:
  1. $car = $this->Car->read(null, '20');
  2. print_r($car);
  3.  //will  output:
  4.    (
  5.    [Car] => Array
  6.         (
  7.          [id] => 20
  8.          [name] => Ferrari
  9.          [model] => Testarosa
  10.          [year] => 1977
  11.          [status] => sold
  12.           )
  13.          [Image] => Array
  14.          (
  15.          [id] => 34
  16.          [title] => sqwertmnx312.jpg
  17.          [width] => 200
  18.          [height] = 150
  19.          )
  20.   )

 

Saving data in CakePHP

CakePHP 2 Comments »

To save in your model using CakePHP you only have to supply that data to a specific model using the save() method.
The data needs to be in the following form:

PHP:
  1. (
  2.     [ModelName] => Array
  3.         (
  4.             [yourfieldname] => 'value'
  5.             [anotherfieldname] => 'value'
  6.         )
  7. )

To post data in this form to a controller for example is very easy using HTML helpers that are implemented in Cake. Everything we need to care about is that form elements are looking like: data[Modelname][fieldname] .

For example to acces a certain variable in a controller we will use;

PHP:
  1. $name_var = $this->data[‘Modelname’][‘formvariable’];

The input form in the view will look something like this:

PHP:
  1. $html->input('Modelname/formvariable')

In a form construction using helpers is best to respect the model structure using to give names to form elements the same names like fields we want to fill in the database.
Using this convention data sent from forms will be formatted automatically and will look putted in $this->data within you controller, so saving data becomes very easy.

Example of saving data:

PHP:
  1. function add() // action add
  2.     {
  3.         if (!empty($this->data))   // checking to see if $this->data is not empty
  4.         {
  5.             if ($this->Car->save($this->data)) // is data is saved returns true
  6.             {
  7.                 $this->flash('Your post has been saved.','/posts'); //displaying message with link
  8.             }
  9.         }
  10.     }

In the example above we are saving data in the Car model.
If you want to validate your data you can easily do this using your model.
Example:

PHP:
  1. class Car extends AppModel
  2. {
  3.     var $name = Car';
  4.     var $validate = array(
  5.         'name'  => VALID_NOT_EMPTY,
  6.         'model'   => VALID_NOT_EMPTY
  7.         'year'   => VALID_NOT_EMPTY
  8.    );
  9. }

Cake will check variables: name, model and year not to be empty.
In on of the next posts I will describe how easy is to save data in multiple related models.

Retrieving data in CakePHP

CakePHP 7 Comments »

A function to retrieve data in CakePHP is the findAll() model function.
It has the following syntax:

PHP:
  1. findAll($conditions, $fields, $order, $limit, $page, $recursive);
  2. string $conditions;
  3. array $fields;
  4. string $order;
  5. int $limit;
  6. int $page;
  7. int $recursive;

For example to retrive all the data from a table you will use following:

PHP:
  1. $this->set($var, $this->Modelname->findAll());

With the “set function” the $var value will be send to the view. In the case above all the fields of the table will be retrieve.

Examples using findAll()

To sort the data by a certain field we can use:

PHP:
  1. $this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field’));

For ascending:

PHP:
  1. $this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field ASC’));

Descending:

PHP:
  1. $this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field DESC’));

Only first 10 results:

PHP:
  1. $this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field DESC’, 10));

The second results page:

PHP:
  1. $this->set($var, $this->Modelname->findAll(null, null, ‘Modelname.field DESC’, 10, 2));

Putting some conditions in the query:

PHP:
  1. $this->set($var, $this->Modelname->findAll(‘Modelname.id> 1’, null, ‘Modelname.field DESC’, 10 ));

 

Elements in CakePHP

CakePHP 1 Comment »

Elements are small blocks of code that can be reused in different pages of a layout.
Elements need to be uploaded in /app/views/elements/ directory and have the .thtml extension.
By default elements haven't acces to any data, if you want to give it access to any data you have to send it as a parameter in an array.

Example without parameters:

PHP:
  1. <?php echo $this->renderElemet('element_name'); ?>

Example of calling an element with parameters:

PHP:
  1. <?php echo
  2. $this->renderElement('element_name', array("text" => "Here is text for example."));
  3. ?>

This pieces of code need to be inserted in your view.

Changing the default layout in CakePHP

CakePHP No Comments »

If you want to change the default welcome layout of the CakePHP framework you have to overidde it, you can do this by creating a default.thtml file in the app/views/layouts directory. Once you have done that you have only to specify to Cake to insert the controller view code in to the right place (our default layout in this case). You can to this by including the $content_for_layout and optionally $title_for_layout variables.

You can customize as you want your template by uploading images and styles in the webroot directory app/webroot which becomes your site root from now on.

Example:

PHP:
  1. <head>
  2. <title><?php echo $title_for_layout; ?></title>
  3. <body>
  4. <!--Here some html code -->
  5. <div id="header">
  6. <div id="menu">...</div>
  7. </div>
  8. <?php echo $content_for_layout; ?>
  9. <!-- Here is the rest of the code -->

After this is done you will see that cake changed the default layout with the one of yours and also the content of the page is cake successfully installation text. Now we have to insert in the new layout our own content. A quick way to do this is creating a controller in app/controllers named as we want but according to cake rules it has to be a plural, for example sites_controller.php . Inside of the controller we will have following code:

PHP:
  1. class SitesController extends AppController {
  2.     var $uses = array();
  3.     function index(){
  4.     // here will be your code;
  5.     }
  6. }

var $uses = array(); I use this because I don’t want Cake engine to look for a model regarding this controller. If you omit this line you will get a “missing model” error.

Browsing the http://example.com/sites/index it should reveal a page having your layout applied but with an error of ”missing view”. To get rid of that error you have to create a file called index.thtml in the app/views/sites/ (you have to create the directory also) file that will be the view for your index action of your Sites controller.
If you want visitors to point directly to a certain action of a controller when they enter your website you can do that using “Route Configuration”. Routing is like a mod_rewrite with the help of which you can map URLs to controllers actions and params and back. Routes are configured in app/config/routes.php .
You can find more information about this at the Chapter Configuration on Cake’s Manual.

In our case our routing rule should look like this:

PHP:
  1. $Route->connect('/', array('controller' => 'sites', 'action' => 'index'));

‘/’ – represents the URL we want to map, the domain itself in this case;
‘sites’ – is the controller we wish to invoke
‘index’ – the action we wish to invoke

Now when we point on http://example.com/ Cake engine should display the following page http://example.com/sites/index (sites controller with the index action).

Started learning CakePHP

CakePHP No Comments »

I've started again learning CakePHP framework and I decided to write as I learn some practical examples of using this development tool. If you know something about Cake, I invite you to read this following posts. I know they are not with a solid documentation behind but maybe you can use some things from here.

Theme designed by N.Design Studio
Entries RSS Comments RSS Log in