Kohana configuration

Posted by chaabant on Dec 9, 2011

When doing a PHP project you will need some kind of configuration since we are not using a CMS like WordPress or Joomla , In Kohana we have to do it manually …

Kohana make this process easy , here’s the steps for having a configuration file and using it in our application .

First Step :

Add a new file (site.php) inside the following directory (application -> config -> site.php)

past the following code inside that file :

1
2
3
4
5
<?php defined('SYSPATH') or die('No direct script access.');
return array(
    'name'     => 'PHP Tutorials',
    'tag_line' => "Let's talk about kohana!"
);
<?php defined('SYSPATH') or die('No direct script access.');
return array(
    'name'     => 'PHP Tutorials',
    'tag_line' => "Let's talk about kohana!"
);

Now to use that configuration we could use the following requests :

1
2
3
4
// full array
$site_config_1 = Kohana::$config->load('site');
// items inside the array
$site_config_2 = Kohana::$config->load('site.name');
// full array
$site_config_1 = Kohana::$config->load('site');
// items inside the array
$site_config_2 = Kohana::$config->load('site.name');

$site_config_1 would return the full array that we have in our configuration

while

$site_config_2 would only return PHP Tutorials

We could even go farther and add an array inside the array in the configuration file in that case if we wanted to access a specific child we would call it using site.name.child …

Hope this post helped you , if you have any question let me know .


Kohana Tutorials – ORM

Posted by chaabant on Dec 9, 2011

I will be starting new tutorials on Kohana , btw I’m new to Kohana so those posts are more like my learning path using Kohana .

Let start …

KOHANA is a PHP framework , I will make a post later discussing why i choose Kohana and not Yii or Zend of CodeIgniter or phpCake etc…

ORM Stand for : Object-relational mapping

First you need to activate the ORM in KOHANA :

uncomment the ORM module inside the bootstarp.php file available inside the application folder .

1
2
3
4
5
6
7
8
9
10
11
12
13
// in my case I'm using all modules so i uncommented all the modules .
Kohana::modules(array(
     'auth'        => MODPATH.'auth',       // Basic authentication
     'cache'       => MODPATH.'cache',      // Caching with multiple backends
     'codebench'   => MODPATH.'codebench',  // Benchmarking tool
     'database'    => MODPATH.'database',   // Database access
     'image'       => MODPATH.'image',      // Image manipulation
     'orm'         => MODPATH.'orm',        // Object Relationship Mapping
     'unittest'    => MODPATH.'unittest',   // Unit testing
     'userguide'   => MODPATH.'userguide',  // User guide and API documentation
     'tableau'     => MODPATH.'tableau', // Generating html tables .. addon added not included in kohana
   
    ));
// in my case I'm using all modules so i uncommented all the modules .
Kohana::modules(array(
	 'auth'        => MODPATH.'auth',       // Basic authentication
	 'cache'       => MODPATH.'cache',      // Caching with multiple backends
	 'codebench'   => MODPATH.'codebench',  // Benchmarking tool
	 'database'    => MODPATH.'database',   // Database access
	 'image'       => MODPATH.'image',      // Image manipulation
	 'orm'         => MODPATH.'orm',        // Object Relationship Mapping
	 'unittest'    => MODPATH.'unittest',   // Unit testing
	 'userguide'   => MODPATH.'userguide',  // User guide and API documentation
     'tableau'     => MODPATH.'tableau', // Generating html tables .. addon added not included in kohana

	));

The second step is to configure your database connection in Kohana go to (/modules/database/config/database.php)

Edit the file to add your database connection (hostname , database , user and password)

The third step is to add the class declaration inside your model (application/classes/model/)

In our example we will add a new class called : user , the filename should be user.php

for now it will only be a declaration :

1
2
3
4
5
6
<?php defined('SYSPATH') or die('No direct access allowed.');
  class Model_User extends ORM {
 
  // for this basic example we will keep this class empty 
}  
?>
<?php defined('SYSPATH') or die('No direct access allowed.');
  class Model_User extends ORM {

  // for this basic example we will keep this class empty
}
?>

As you can see it should always start with Model then underscore then first letter is capital , the name of the table should be the same as the class that we declared (in my case i have a table called “user“)

Finally in our controller we could do the following requests :

1
2
3
4
5
6
7
8
9
$users = ORM::factory('user');
 
// will return the number of users in my table
$users->count_all()
 
// add a new user to the table
$users->firstName = "John";
$users->lastName  = "Doe" ;
$users->save();
$users = ORM::factory('user');

// will return the number of users in my table
$users->count_all()

// add a new user to the table
$users->firstName = "John";
$users->lastName  = "Doe" ;
$users->save();

How to check if checkbox is checked in mootools

Posted by chaabant on Sep 6, 2010

Hello , what is the query to test if a checkbox is checked in mootools ?

i need this to add the checkbox ” I HAVE READ AND AGREED TO THE FOLLOWING TERMS AND CONDITIONS ” before a submit button .

The answer to this question is very simple …

all what you need to do is test the following :

1
2
3
4
5
6
7
8
9
10
11
12
window.addEvent('load', function() {
 
// Checkbox ID = termOfUsage
// Button ID =  informationsButton
 
 $('termOfUsage').addEvent('click',function(event) {    
     val = $('termOfUsage').checked;
     
     if(val) {   $('informationsButton').set('disabled',false);  }
       else {   $('informationsButton').set('disabled',true);    }
   });
 });  
window.addEvent('load', function() {

// Checkbox ID = termOfUsage
// Button ID =  informationsButton

 $('termOfUsage').addEvent('click',function(event) {
     val = $('termOfUsage').checked;

     if(val) {   $('informationsButton').set('disabled',false);  }
       else {   $('informationsButton').set('disabled',true);    }
   });
 });  

PHP SPLIT

Posted by chaabant on Jan 4, 2009

hi..can anyone please solve my problem?

i have text input and a submit button.

The user will insert sentence/question/any text in the text input.Then, when he/she press the submit
button, the input will be keep in the database(mysql). After that, i want to split the input(let say a sentence) into words(array).

Here is an example:

Input: Differentiate between array and arraylist.

After splitting:

=>Differentiate
=>between
=>array
=>and
=>arraylist
=>.

I wonder how mysql handle the input after splitting it?is it in a field or each index of array(each word) in the difference field?because after splitting it, i want to match the input(which is now in form of words not a sentence) with a library i made in mysql.

Let say, in my library there are 3 fields:weight1,weight2,weight3.The word ‘what’ was in field weight1 in my
library.

So, the question is: how can i split the input into words and then match it with my library so that i can state the input is in which weight(weight1/weight2/weight3).

Please help me.
Thank you.


Jquery : setInterval or setTimeout

Posted by chaabant on Dec 31, 2008

I was doing a live counter for some of my sites … so to make it “LIVE” I’ve used the Great Jquery library (if you dont know what’s jquery is … i will make a post soon about it and some JQUERY For dummies post)

What i have is simple :

- count.php // the counter basically it’s just a php file where i update the database
- index.php // the main page where i show the counter .

so this is what i wrote on the index.php

1
2
3
4
5
6
7
8
9
10
11
12
// The call to the count.php page , Using Post Method and sending 1 as a variable
function update() {
 
$.post("count.php", {count:1},
  function(data){
   $("#counter").html(data);});
}
 
// The code that call the update function each seconds 
  $(document).ready(function() {
    setInterval("update()", 1000);
  });
// The call to the count.php page , Using Post Method and sending 1 as a variable
function update() {

$.post("count.php", {count:1},
  function(data){
   $("#counter").html(data);});
}

// The code that call the update function each seconds
  $(document).ready(function() {
    setInterval("update()", 1000);
  });

So after writing this code , everything was working fine and the counter was live and the update worked .

I asked in few forums to see if there was any problem with my code since i’m new to jquery

here are some of the answers i got :

From a Sitepoint member hexburner

You could use an Ajax request with the if Modified header.

Or you could just use the load function.

The one second interval creates a huge amount of requests, which I wouldn’t recommend…

Nobody is really looking to the counter every second, so you can increase the interval without upsetting anybody.

What really concerns me is what happens if counter.php could not be loaded within the one second interval.

Imagine someones internet connection breaks down right after they visit your website.

The page counter.php could not be loaded, and after 30 seconds you’ll have 30 open connections…

So don’t use setInterval with ajax requests, but rather use setTimeout after the ajax request completed or returned an error.

I would recommend something like below, but you could use one of the other ajax functions of jQuery, whichever suits you best.

1
2
3
4
5
6
7
jQuery(document).ready(loadCounter);
 
function loadCounter () {
    $("#counter").load("count.php", {count:1}, function(responseText, textStatus, XMLHttpRequest){
        setTimeout(loadCounter, 5000);
    });
}
jQuery(document).ready(loadCounter);

function loadCounter () {
    $("#counter").load("count.php", {count:1}, function(responseText, textStatus, XMLHttpRequest){
        setTimeout(loadCounter, 5000);
    });
}

I tested his code and everything was fine and working .

The only concern i’m still having is the server issue , since the site is on a shared account … here is what another person from Google jquery group answered ”

If this is what you are after, and you are getting that data from a database, then a request must be sent every time to that database.

Seems to be pretty taxing, especially if traffic ramps up.

Joe

he’s right , in worse case what i will do is one of the followings :

- Make the call every 5 seconds instead of 1
- Make the call only on click call

Any other ideas for live counter ?

what about the live chat i think it’s the same idea behind …