Richard Castera - Explorations in Software Development
Richard Castera - Explorations in Software Development
Browsing Tag
php
Scripts

A PHP function for the Fibonacci sequence

October 11, 2011 No Comments

I was recently asked as a test to write a function that outputs the Fibonacci sequence in PHP. Here it is:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
function fib() {
    $current = 1;
    $previous = 0;
    $evaluation = 0;
    while($evaluation < 100) {
        $evaluation = ($current+$previous);
        $current = $previous;
        $previous = $evaluation;
        echo $evaluation . '<br>';
    }
}
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Magento – Get the Total Price of items currently in the Cart

June 10, 2010 12 Comments

Ever wanted to get the total price of items in your Magento cart? Here you go:

PHP
1
<?php echo $this->helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()); ?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Drupal – Check if a User has a specific role

March 4, 2010 1 Comment

Here is a quick way to determine if a user has a specific role:

PHP
1
2
3
4
5
6
7
8
<?php
  // Bring the user object into scope.
  global $user;
  // Check to see if $user has the administrator user role.
  if (in_array('administrator', array_values($user->roles))) {
    // Do something.
  }
?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Drupal – Adding Javascript to your module

March 3, 2010 1 Comment

When creating your own Drupal module, you may need to add some styling or Javascript to improve the usability of your module. Here is how to do it.

PHP
1
drupal_add_js(drupal_get_path('module', 'MODULE_NAME') . '/common.js');

Similarly you can add CSS to your module as well

PHP
1
drupal_add_css(drupal_get_path('module', 'MODULE_NAME') . '/styles.css');
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Drupal – Use hook_form_alter() to set redirect path on the form

March 2, 2010 2 Comments

One popular use of this hook is to change the destination of a form submission. Here is how it is accomplished:

PHP
1
2
3
4
5
6
7
8
9
10
11
<?php
function YOURMODULE_form_alter($form_id, &$form) {
  switch ($form['#id']) {
    case 'node-form':
       if ($form['type']['#value'] == 'story') {
         $form['#redirect'] = 'new/url';
       }
     break;
  }
}
?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

WordPress – List Scheduled Posts

February 28, 2010 2 Comments

If you ever wanted to show you readers posts that are scheduled to be published, here’s how to do it.

PHP
1
2
3
4
5
6
7
8
<?php
$result = new WP_Query('post_status=future&order=DESC&showposts=5');
if ($result->have_posts()) {
    while ($result->have_posts()) : $result->the_post(); ?>
        <?php the_title(); ?>
    <?php endwhile;
}
?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Magento – Check if a User is logged in

February 23, 2010 6 Comments

You may want to check if a user is logged in with Magento, possibly to display a link or promotional item. Here’s how to do it:

PHP
1
2
3
4
5
6
7
8
<?php
if ($this->helper('customer')->isLoggedIn()) {
    echo("Anonymous user");
}
else {
    echo("Authenticated user");
}
?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Magento – How to Display the Product SKU

February 23, 2010 4 Comments

Magento is a pretty flexible platform. That flexibility comes with a price though; You have to be knowledgeable of the functions that are available to you to utilize. Finding or knowing them is a challenge though. Here is one of those such functions that you may need to display the product Sku on a category page.

PHP
1
<?php echo($_product->getSku()); ?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Magento – How to run a SQL query against the database

August 23, 2009 7 Comments

In order to run a SQL query against the Magento database, you first need a resource model then a database connection.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$db = Mage::getResourceSingleton('core/resource')->getConnection('core_write');
$result = $db->query('SELECT 'entity_id' FROM 'catalog_product_entity');
if (!$result) {
    return FALSE;
}
$rows = $result->fetch(PDO::FETCH_ASSOC);
if (!$rows) {
    return FALSE;
}
print_r($rows);
?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Magento – Get the Current Category

August 3, 2009 16 Comments

Need to get the current category loaded? No problem! Just use this useful little snippet below. It will return an object of the current category.

PHP
1
<?php $currentCategory = Mage::registry('current_category'); ?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Page 1 of 3123»

About me

Hello, my name is Richard Castera. I have more than 12 years of experience architecting, implementing, leading and launching large scale, high performance software products in a fast-paced agile environment.

Popular Posts

Magento – Display new products on the home page

May 20, 2009

How to Fix “Internet Explorer Cannot Open the Internet Site- Operation Aborted” Error

May 9, 2009

PHP – Convert Array to Object with stdClass

July 5, 2009

Categories

  • Home Automation
  • Resources
  • Scripts
  • SEO
  • Thoughts
  • Web Server

Tags

Apache Apple Bash CSS Drupal Free home automation htaccess Java Javascript Magento PHP raspberry pi SEO SQL thoughts Wordpress

© 2019 copyright Richard Castera // All rights reserved