December 27, 2007

Get the ID generated from the previous INSERT opereration

Filed under: MySQL, PHP — Soleer @ 4:43 pm

With the PHP function mysql_insert_id( ), you can obtain the previous index generated from an insert operation.

The need for this capability came about when we were generating a MySQL database for invoice storage.  Our database contains an INVOICE table for customer billing information, and an INV_ITEM table for individual items that are included in each order.

 The INVOICE table uses inv_id as an AUTO INCREMENT primary key, and the INV_ITEM uses inv_id as an index value, so the invoice items may be associated with the correct invoice.  All of this worked great, but we needed to send an email with order information, and an order number corresponding to the inv_id so we could access the order easily from the database if need be. 

The answer was to use the mysql_insert_id( ) function immeadiately after the insert statement, which returns the generated ID, which was inv_id in our case.  We then stored the returned ID to a string, and used it where necessary in the rest of our code.

$sql = “INSERT INTO invoices (”.$key_string.”) VALUES (”.$value_string.”)”;
$result = mysql_query($sql) ;
$invoice_id = mysql_insert_id();
if (!$result) {
      die(’Invalid query: ‘ . mysql_error());
}

The result of the previous code was that our values were inserted into the database as required, with the inv_id generated, and then stored into the $invoice_id variable for further use in the code.

December 17, 2007

PHP 4.3: Remove a specific key from an array

Filed under: PHP — Soleer @ 10:43 pm

PHP 5+ provides the built-in function array_diff_key() which can be used to delete a certain key from an array.  Although I’m not proud of it, some of our customers’ sites are still running versions prior to PHP 5.  This is how we delete a specific key from an array in PHP 4+ (4.3).

 Let’s say that I have an array which contains the following:

$my_array = array(”key1″=> “apple”, “key2″ => “orange”, “key3″ => “banana”);

If I wanted to remove the key “key1″, and it’s associated value from the array, there is an array function array_diff_assoc() in PHP 4.3+ which compares 2 arrays, and deletes any key/value combinations from the first array which are present in the second, and returns the resulting array.  The problem is, that the key & value must be the same in both arrays.  This is the statement we used to delete the desired key:

$my_new_array = array_diff_assoc($my_array,array(”key1″=> $my_array["key1"]));

Because we are reading the value of the key, from the desired array, we can be assured that the key/value combinations will match in both arrays, and the result will be a returned array, matching the original $my_array, but with “key1″ => “apple” removed!

 You could also create a function to accomplish this:

function array_remove_key($target_array, $key){

          return array_diff_assoc($target_array,array($key => $target_array[$key]));

}

December 10, 2007

Drupal: Custom Latest Posts

Filed under: Drupal, PHP — Soleer @ 10:29 pm

We’re currently developing a Drupal community website for an up-and-coming society.

On the community front page we wanted to display a list of the most recent forum posts. This can easily be accomplished using the Drupal API, and the forum_block() function, with the parameters ‘view’, and the 0 for most active posts, and 1 for newest post (i.e. forum_block(‘view’,0).

The problem in our situation was that we wanted to be able to customize the output beyond the standard API capabilities. In this case, it is a small customization, but this small customization involved digging deep into the Drupal PHP code. We wanted to display who posted the forum topic.

Here’s what we did:

<?php

/*Display Most Active Forum Posts*/

//sets variable to display 3 most active posts
variable_set(’forum_block_num_0′, ‘3′);

//SQL databse call, copied from forum_block(), added: n.uid which is the database value storing the user id of the poster
$sql = db_rewrite_sql(”SELECT n.nid, n.title, n.uid, l.comment_count, l.last_comment_timestamp FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 AND n.type = ‘forum’ ORDER BY l.last_comment_timestamp DESC”);

//Get database results, and limit to number of posts defined by forum_block_num
$result = db_query_range($sql, 0, variable_get(’forum_block_num_0′, ‘5′));

//if the result isn’t empty, display the forum topics
if (db_num_rows($result)) {

     //for every row in the database, display the desired output
     while ($node = db_fetch_object($result)) {

           //create a user object, with the uid from the posting
           $user->uid = $node->uid;

           //take the partial user object, load a full user object, and get the username
           $user_name = user_load($user)->name;

           //create a link to the forum node, add ‘posted by’ with poster’s username
           $items[] = l($node->title, ‘node/’. $node->nid, $node->comment_count ? array(’title’ => format_plural($node->comment_count, ‘1 comment’, ‘@count comments’)) : ”).’ posted by: ‘.$user_name;
     }

//format the resulting list and return as content
     $content = theme(’node_list’, $items, $title);
}
echo $content; //return the formatted list of recent topics
?>

December 8, 2007

Our New Site is LIVE

Filed under: Soleer News — Soleer @ 11:51 am

As you might have noticed, we just performed a total overhaul of our site.

The navigation and layout should be more-or-less the same, but the overall site aesthetic is dramatically different, and should provide a better platform for future expansion.

We plan on adding new capabilities including upgrading our portfolio, which is currently unavailable, but will be online shortly.

A Blog section has also been added, which you’ve probably noticed since this is a blog post :-) Now we can share our expertise and updates with the rest of the world.

December 5, 2007

External Wordpress Function Integration

Filed under: PHP, WordPress — Soleer @ 4:11 pm

We recently came across the need to display recent posts in an area that wasn’t part of our wordpress blog. This is how we solved the problem:

I’m sure this is a common need for many people who have a index (home/front) page, where they would like to display teasers of recent blog entries. Most of the websites we develop have needs beyond those provided by the standard WordPress Capabilities. WordPress is a great blogging platform, but for most of our projects we like to restrict its use to simply blogging.

Wordpress includes a Recent Posts Plugin, which provided the functionality we needed. It creates a function called “mdv_recent_posts().” After activating this plugin, all you have to do within your theme is call this function, and it will return nicely formatted recent posts.  (This part works great)

The problem is that we were attempting to do this from our front page which wasn’t a part of the blog.  The blog was contained in its own directory.

The Solution:

So, to do this fix, you’ll need to edit the php code of the specific page.  In our case it was ‘index.php’.

In the head region, you’ll want to add:

<?php
     define(’WP_USE_THEMES’, false);
     require(’../blog/wp-blog-header.php’);
?>

The first ‘define’ statement lets WordPress known that we are handling the page design ourselves, and it doesn’t need to theme it.  The second ‘require’ statement includes the wp-blog-header( ) function, which then includes all of the necessary WordPress variables and functions.

Now that our page has access to the insides of Wordpress, all we need to do is call the function.  In the area of the page that you want to include the recent posts block, simply include the php statement:

<?php mdv_recent_posts( ); ?>

…and voila!  Now you can access Wordpress’s widgets and functions on the pages of your website that aren’t part of Wordpress.

Who we are

Explore our history and character.

If you've ever been on a blind-date, odds are great that you know how disastrous "jumping before looking" can be. Feel free to do some online stalking here before you contact Soleer.

What We Do

"Girls only want boyfriends who have great skills." -Napoleon Dynamite

While we aren't looking to be your boyfriend and might not have "nunchuku skills," we do offer a number of other services and solutions to assist your organization.

What We’ve Done

As we say in Texas, "This ain't our first rodeo." Take a look at a sampling of some of our masterpieces, from logo design and brochure creation to webpage design and online community development.