Blog

Embed Drupal node form anywhere

Simple 1 line of code to embed a Drupal node form anywhere in your site although I would recommend doing it the Drupal way which is creating a custom module for this.

print drupal_render(node_add('NODE_TYPE'));

Where NODE_TYPE is your content type (machine name).

If the code above returns an error that says the function node_add() is undefined, add the following code before it.

if (!function_exists('node_add')) {
  module_load_include('inc', 'node', 'node.pages');
}

Display Drupal menu local task tab according to node content type

By default every node should have the menu tab (MENU LOCAL TASK) of View and Edit.

If your module wants to add its own tab but only restricted to content type of "foo", then use the following code snippet.

XYZ below is your page callback for the new custom tab. Basically it calls another function to validate user's permission but we manipulate it to check the node type (via access callback).

Themer-friendly menu link CSS classes for Drupal theme

What this code does is to give themers an easier way to style individual menu items in Drupal. For example, if you have a menu item title "Company Profile" and you want this link to be red colour, the following code (to be placed inside template.php file of your theme) will provide menu CSS class of <li class="... menu-company-profile">

Style your Drupal theme according to day or night time

Here's a simple snippet to allow you to style your theme differently for day or night time. Place the following code into your theme's template.php file.

Malaysia's URL shortener service RD.MY powered by Drupal

Homepage of RD.MY

After securing my Malaysia country-level domain name KT.MY, I felt that it would also be fun to create my very own URL shortener service since its relatively easy to do especially with Drupal.

Project Euler - Problem 11

Question

In the 20 x 20 grid below, four numbers along a diagonal line have been marked in red (bold).

Project Euler - Problem 10

Question

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

Solution

$sum = 2;
for ($i = 2; $i < 2000000; $i++) {
  if ($i % 2 != 1) {
    continue;
  }
  $d = 3;
  $x = sqrt($i);
  while (($i % $d != 0) && $d < $x) {
    $d += 2;
  }
  if ((($i % $d == 0 && $i != $d) * 1) == 0) {
    $sum += $i;
  }
}
echo $sum;

Time taken to execute: 30.5757 second.

Answer

142913828922

Project Euler - Problem 9

Question

A Pythagorean triplet is a set of three natural numbers, a a² + b² = c²

For example, 3² + 4² = 9 + 16 = 25 = 5².

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Project Euler - Problem 8

Question

Find the greatest product of five consecutive digits in the 1000-digit number.

Project Euler - Problem 7

Question

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

Solution

$last = 0;
$count = 0;
$max = 10001;
for ($i = 3; $count < $max - 1; $i++) {
  if ($i % 2 != 1) {
    continue;
  }
  $d = 3;
  $x = sqrt($i);
  while (($i % $d != 0) && $d < $x) {
    $d += 2;
  }
  if ((($i % $d == 0 && $i != $d) * 1) == 0) {
    $last = $i;
    $count++;
  }
}
echo $last;

Time taken to execute: 0.5800 second.

Pages

Recent posts

Recent comments