Sunday, 16 April 2017

,
Just letting you guys know that now you can do this:

$newSyntax = [
  'key' => 'value',
  'anotherKey' => 'anotherValue'
];
Instead of this:
$oldSyntax = array(
  'key' => 'value',
  'anotherKey' => 'anotherValue'
);
All you need is PHP 5.4 or higher.
,

I've created a method to do five columns using bootstrap 3. It requires you to nest two columns of with col-sm-7 and col-sm-5, then inside of the col-sm-7 three columns of col-sm4 and inside the col-sm-5 with two columns of col-sm-6.
There there are two column width overrides on the outside two columns to make sure the columns end up with equal width.
<div class="col-sm-12">
    <div class="row">
      <div class="col-sm-7 five-three">
        <div class="row">
          <div class="col-sm-4">
          Column 1
          </div>
          <div class="col-sm-4">
          Column 2
          </div>
          <div class="col-sm-4">
          Column 3
          </div><!-- end inner row -->
        </div>
      </div>
      <div class="col-sm-5 five-two">
        <div class="row">
          <div class="col-sm-6">
            Col 4
          </div>
          <div class="col-sm-6">
          Col 5
          </div>
        </div><!-- end inner row -->
      </div>
    </div><!-- end outer row -->
 </div>
This is the css you need to put in after your bootstrap loads to over-ride the column width on the col-sm-7 and col-sm-5
@media  (min-width: 768px) {
    div.col-sm-7.five-three {
    width: 60% !important;
    }

    div.col-sm-5.five-two {
    width: 40% !important;
    }
}

I usually use my columns as col-sm-<some number> as I usually let things collapse after the small breakpoint. That's why I used the media breakpoint to allow the columns to go full screen after the small tablet size.
,

The world of mathematical programming does not lie solely in the hands of C programmers and other lower level languages.
Using PHP we can also calculate Pi, albeit very slowly.
$pi = 4; $top = 4; $bot = 3; $minus = TRUE;
$accuracy = 1000000;

for($i = 0; $i < $accuracy; $i++)
{
  $pi += ( $minus ? -($top/$bot) : ($top/$bot) );
  $minus = ( $minus ? FALSE : TRUE);
  $bot += 2;
}
print "Pi ~=: " . $pi;
This method of calculating Pi is slow, but it is easy to read code.
You can read more about this method here:
http://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
If you increase the $accuracy variable, Pi will be calculated more and more accurately. Depending on how fast your web server is, you can calculate the first 6 digits of Pi fairly quickly.
The time it takes to calculate each succeeding number goes up exponentially however. To calculate 20 digits of Pi using this method method could take years.
,

Drupal 7 and the Form API do not support multiple File upload as a field type in the Form API. This is how I worked around that and got multiple file upload to work as part of my own custom form.
function photos_upload_form($form, &$form_state) {
    $form = array();

    $form['file'] = array(
        '#type' => 'file',
        '#name' => 'files[]',
        '#title' => t('Upload some photos'),
        '#description' => t('JPG\'s, GIF\'s, and PNG\'s only, 10MB Max Size'),
        '#attributes' => array('multiple' => 'multiple'),
    );

    //The nid of the node to upload the photos to
    $form['nid'] = array(
        '#type' => 'hidden',
        '#value' => $nid,
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Upload'),
    );

    return $form;
}
The '#attributies' field in the file field is what allows most browsers to recognize that this field will allow multiple file uploads at once.
Also notice that the #name of the file field is listed as an array (files[]) rather than just a normal name. This will group all of your files in the $_FILES variable on form submit so that you can reference them as you'll see below.
function photos_upload_form_validate($form, &$form_state) {
    //Save multiple files
    $num_files = count($_FILES['files']['name']);
    for ($i = 0; $i < $num_files; $i++) {
        $file = file_save_upload($i, array(
            'file_validate_is_image' => array(),
            'file_validate_extensions' => array('png gif jpg jpeg'),
        ));
        if ($file) {
            if ($file = file_move($file, 'public://')) {
                $form_state['values']['file'][$i] = $file;
            }
            else {
                form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
            }
        }
        else {
            form_set_error('file', t('No file was uploaded.'));
        }   
    }
}
This form function validates that each file matches the validation criteria (that it's an image and that it has valid file extensions).
Notice that I'm using $i in filesaveload instead of the form file field shown in the first form function. This is because when you specify the '#name' of the file field as an array it will reference all uploaded files in the $FILES variable as numbered (from zero) indexes instead of string names. This is what allows us to save each file in the $FILES array.
function photos_upload_form_submit($form, &$form_state) {
    $nid = $form_state['values']['nid'];
    $node = node_load($nid);
    if (is_array($form_state['values']['file'])) {
        //Make sure we keep our previously uploaded images
        $i = empty($node->field_image) ? 0 : count($node->field_image['und']);
        foreach ($form_state['values']['file'] as $file) {
            $new_file = file_load($file->fid);
            $file_info = image_get_info($file->uri);
            $node->field_image[LANGUAGE_NONE][$i]['fid'] = $file->fid;
            $node->field_image[LANGUAGE_NONE][$i]['alt'] = '';
            $node->field_image[LANGUAGE_NONE][$i]['title'] = '';
            $node->field_image[LANGUAGE_NONE][$i]['width'] = $file_info['width'];
            $node->field_image[LANGUAGE_NONE][$i]['height'] = $file_info['height'];
            $node->field_image[LANGUAGE_NONE][$i]['uid'] = $file->uid;
            $node->field_image[LANGUAGE_NONE][$i]['filename'] = $file->filename;
            $node->field_image[LANGUAGE_NONE][$i]['uri'] = $file->uri;
            $node->field_image[LANGUAGE_NONE][$i]['filemime'] = $file->filemime;
            $node->field_image[LANGUAGE_NONE][$i]['filesize'] = $file->filesize;
            $node->field_image[LANGUAGE_NONE][$i]['status'] = '1';
            $node->field_image[LANGUAGE_NONE][$i]['timestamp'] = $file->timestamp;
            $node->field_image[LANGUAGE_NONE][$i]['rdf_mapping'] = array();
            $i++;
        }
    }
    node_save($node);
    drupal_set_message(t('Upload successful'));
}
The above form function is simply in place to save the newly uploaded files to the appropriate node that we specified the nid of in the first form function earlier. It's important to note the declaration of $i in this last function because that makes sure we don't override any previously uploaded files on the node.
,



Introduction

Sometimes I just need to type garbage. Just to clear out my mind. Using editors to type such gibberish annoys me because it clutters my project workspace (I'm picky, I know).
So I do this. Since I live in the browser, I just open a new tab and type in the url tab.
data:text/html, <html contenteditable>


You can use textarea and make it "invisible" if you want autofocus.
data:text/html, <textarea style="font-size: 1.5em; width: 100%; height: 100%; border: none; outline: none" autofocus />



Voila, browser notepad.

Sunday, 9 April 2017

,

You can update the password via mysql. You can run the following query in mysql to update password. The given hashed value is the hashed value of password: "NewPassword"


UPDATE users SET pass = '$S$DRSUIz9NFfxOXKPveQ00UTGMzsJe62LjYvVHfYJ8I8wuy4zRqVBK' WHERE uid = 1;