Friday, December 30, 2011

Creating Blocks using Dynamic (Coding) way

 Creating Blocks using Dynamic (Coding) way:

function modulename_block($op='list',$delta=array(),$edit=array()) {
    global $base_path;
    switch ($op) {

        case 'list':
            $blocks[0]['info'] = t('Block name 1');
            $blocks[1]['info'] = t('Block name 2');
            return $blocks;
            break;
        case 'view':
            switch ($delta) {
                case 0:
                    $blocks['subject'] = t('');
                    $blocks['content'] = drupal_get_form('reset_form');
                  break;
                case 1:
                    $blocks['subject'] = t('');
                    $blocks['content'] = drupal_get_form('login_form'); 
                  break;
                }
      return $blocks;
    break;
    }   
}

function reset_form(){
     $form['pass'] = array(
    '#type' => 'textfield',
    '#title' => t('Password'),
    '#maxlength' => 15,
  );
  $form['conform_pass'] = array(
    '#type' => 'password',
    '#title' => t('ConformPassword'),
    '#maxlength' => 15,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  ); 
  return $form;
}

function login_form(){
  $form['mailid'] = array(
    '#type' => 'textfield',
    '#title' => t('Mail Id'),
    '#maxlength' => 15,
  );
  $form['password'] = array(
    '#type' => 'password',
    '#title' => t('Password'),
    '#maxlength' => 15,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  ); 
  return $form;
}

After creating the block,assign it to the proper region.

drupal_write_record

drupal_write_record:

Save a record to the database based upon the schema.
Default values are filled in for missing items, and 'serial' (auto increment) types are filled in with IDs.

Parameters

$table The name of the table; this must exist in schema API.
$object The object to write. This is a reference, as defaults according to the schema may be filled in on the object, as well as ID on the serial type(s). Both array an object types may be passed.
$update If this is an update, specify the primary keys' field names. It is the caller's responsibility to know if a record for this object already exists in the database. If there is only 1 key, you may pass a simple string.

Return value

Failure to write a record will return FALSE. Otherwise SAVED_NEW or SAVED_UPDATED is returned depending on the operation performed. The $object parameter contains values for any serial fields defined by the $table. For example, $object->nid will be populated after inserting a new node.

Example:

$result_array = array_slice($form_state['values'],0,2);
drupal_write_record('tbl_eng_braz',$result_array); 

tbl_eng_braz - table name
$result_array - Sliced array  of the form values to be stored in to the "tbl_eng_braz" table.Only 2 fields are there in the form,so we should create the table with 2 fields and give the name for the fields as per the form.
 

Friday, December 16, 2011

For printing out strings, there are echo, print and printf. Explain the differences

For printing out strings, there are echo, print and printf. Explain the differences:

echo is the most primitive of them, and just outputs the contents following the construct to the screen. print is also a construct (so parentheses are optional when calling it), but it returns TRUE on successful output and FALSE if it was unable to print out the string. However, you can pass multiple parameters to echo, like: 

 <?php echo 'Welcome ', 'to', ' ', 'phpndrupal Blog!'; ?>
 
and it will output the string "Welcome to phpndrupal Blog!" print does not take multiple parameters. It is also generally argued that echo is faster, but usually the speed advantage is negligible, and might not be there for future versions of PHP. printf  is a function, not a construct, and allows such advantages as formatted output, but it’s the slowest way to print out data out of echo, print and printf.