Accessing Multiple Databases in CodeIgniter


Multiple Databases In Codeigniter

Here is the general transcript from the video, however the video includes a lot more:

Hello, this is Jedidiah Reeser from JedidiahMedia.com. Today, I’m going to show you how to connect to multiple databases in CodeIgniter.

Step 1
I am going to add the secondary database details in the config folder. I will open database.php in the config folder which is found in the application folder. I will copy and paste the database settings and change default to second_db.

Step 2
Next, I will go to the controller and add the code to load the model.

 $this->load->model('example','', TRUE);

Step 3
Then I will go to the model and make it connect to the second database. To do this, I will add a private variable in the class. In the constructor function I will load the second database. I will create a function to run a query using the second database.


class Example extends Model
{
  private $second_db;

  function Example()
  {
      parent::Model();
      $this->second_db = $this->load->database('second_db', TRUE);
  }

function get_latest()
{
    $info = '';
    $query = $this->second_db->query('SELECT * FROM wp_posts limit 3');

    if ($query->num_rows() > 0 ){
      $info .= '<ul>';
       foreach ($query->result() as $row)
        {
            $info .= '
            <li>
              <h3>'.$row->post_title.'</h3>
            <br />
            <div>'.$row->post_excerpt.'</div>
           </li>
            ';
        }
        $info .= '</ul>';
    }

    return $info;

    }
}

Step 4
After that I will open the controller and send the information to the view.

 $this->load->model('example','', TRUE);

 $data['posts'] = '
     <div id="blogposts">
    <h2>Recent Posts</h2>
    ';
    $data['posts'] .= $this->example->get_latest();

    $data['posts'] .= '</div>';

    $this->load->view('welcome_message', $data);

Step 5
Then, I will go to the view and display the information.

    <?=$posts;?>

Thanks for watching! I hope that you have found this video tutorial helpful. For more tips or information, please visit my sites at: jedidiahreeser.com and jedidiahmedia.com. And a special thanks to Krystal Reeser for helping me create this video.

Buy Me a Coffee

Page Peel jQuery Plugin

Here is a jQuery plugin that gives you a page peel look, with lots of options. Can be used for an ad, with a click-able link. Top and bottom peels available.

http://www.smple.com/pagePeel/

Example:

page-peel-21

page-peel-example

Thanks to John McMullen for posting his page peel plugin on his website at: http://smple.com/. Check out John’s website for more plugins and tutorials!

Buy Me a Coffee

Format Phone Numbers with PHP

Using PHP, this little bit of code parses and formats numbers by putting parentheses and a hyphen into the phone number. This code handles 10 digit phone numbers and even eleven digits (with the country code).

Example: Changes this 1234567890  into this (123) 456-7890

function formatphone($phone){
if (empty($phone)) return "";

if (strlen($phone) == 7) {
    sscanf($phone, "%3s%4s", $prefix, $exchange);
} else if (strlen($phone) == 10) {
    sscanf($phone, "%3s%3s%4s", $area, $prefix, $exchange);
} else if (strlen($phone) > 10) {
    if(substr($phone,0,1)=='1') {
        sscanf($phone, "%1s%3s%3s%4s", $country, $area, $prefix, $exchange);
    } else {
        sscanf($phone, "%3s%3s%4s%s", $area, $prefix, $exchange, $extension);
    }
} else {
    return $phone;
}

$out = "";
$out .= isset($country) ? $country.' ' : '';
$out .= isset($area) ? '(' . $area . ') ' : '';
$out .= $prefix . '-' . $exchange;
$out .= isset($extension) ? ' x' . $extension : '';
return $out;
}
I would like to thank Justin Cook for posting his code on justin-cook.com. By changing a few lines of Justin’s code, I was able to get it to work for me. To view the original code or to see Justin’s blog, visit: http://www.justin-cook.com/wp/2006/11/17/parse-and-format-phone-numbers-with-php/. Justin describes his blog as “A web, technology, programming and SEO blog by Justin Cook.” Nice work, Justin!

Buy Me a Coffee

FavIcon Generator

Today, I am covering another gift to programmers from Dynamic Drive.com. Dynamic Drive’s FavIcon Generator creates a 16×16 image to fit on the address bar and can entice your surfers to bookmark your website.

Upload an image in one of these formats: gif, jpg, png, or bmp. Click “create icon” and view the preview image. Then, follow the instructions for inserting the generated code in your webpage.

Check out the FavIcon Generator at the Dynamic Drive!

http://tools.dynamicdrive.com/favicon/

Buy Me a Coffee

Corner Ribbon Banner Generator II

Yesterday, I blogged about a diagonal ribbon banners and how they are useful. I also gave an example of a web-tool that you can use to create diagonal banners.

Today, I found an additional web-tool that also creates diagonal ribbon banners that I recommend you take a look at. This webpage works similarly to the first one: you enter in information, pick out some colors, and the webpage generates code for you to enter into your blog.

quickribbon.com

quickribbon.com

I wanted to share this tool with you because the banner it generates is very sharp-looking and you can also choose to add a URL link or hyperlink to be embedded in the banner. Check it out!

http://www.quickribbon.com/

My other blog article on diagonal ribbon banners can be found here:

http://jedidiahreeser.com/blog/?p=25

Buy Me a Coffee

Corner Ribbon Banner Thing – Generator

Here is a pretty quick and nifty way to add a quick announcement or to draw attention to a certain update on your website or blog -

Use this tool to generate code for a diagonal ribbon in the corner of you website: http://www.websiteribbon.com/

All you have to do is go to the website, enter your message, choose font size and color, choose banner type, click “generate” and then place the generated code in your webpage.

Here is an example that I made using this cool little tool:

Create your own at: http://www.websiteribbon.com/

Create your own at: http://www.websiteribbon.com/

Enjoy!

Buy Me a Coffee

PHP is for the PowerHungryProgrammer

PHP is a great code that I frequently suggest to other programmer friends to use on various types of projects. However, some programmers still shy away from this versatile and powerful code. For those of you who are unfamiliar with PHP or are not sure if it should be considered for you next project, consider this:

  • PHP is compatible with Unix, Linux, and Windows.
  • PHP allows you to organize the code the way you want (start imagining how this could be a good thing)
  • PHP has a strong community and has many pre-built open source applications that are waiting for you to intergrate into your project (saving you millions of years sitting in front of your computer)
  • Connects easily to a variety of databases
  • Its OpenSource, so you got freedom!
  • PHP is great for start-ups and side projects because of low-cost, though this should be appealing to everyone.
  • PHP code is hidden from web surfers (only html is visible)
  • PHP can create .pdf’s and excel files on the fly
  • PHP was used to program other awesome and powerful projects (facebook.com, youtube.com, wikipedia.com*)

In conclusion, the correct time to consider using PHP is when you are hungry to create powerful websites!

Check out more great reasons to use PHP at Krystian Hoffman’s blog:

Buy Me a Coffee

BlogOrlando 2008




BlogOrlando 2008

Originally uploaded by hyperlinkguerrilla

BlogOrlando 2008 was an awesome event. Just what I needed to motivate me to get back to regularly blogging.

Nice to hear from some other motivated bloggers out there!

http://www.blogorlando.com/

Can you find me in the picture?

Buy Me a Coffee

Edit photos without downloading software

Use picnik.com for fast photo manipulation on the go. No need to download software. All the basics features and more are in your tool chest.

Use Picasa Web Albums or Flickr? No need to upload from your computer! Just sign in.

Easy to use. Learn to navigate in seconds if you are familiar with similar applications.

Me on picknik:

Try Picknik.com !

Buy Me a Coffee

Colorzilla

While colorzilla is an old favorite for me, I know some of my programming friends still aren’t using this great little tool. Colorzilla is an add-on to the Firefox browser.

I use colorzilla’s “eyedropper” to select a color from a particular point in my browser. Next, I can take that color, adjust the color, and then use my new color in my code. If I’m making an image in Inkscape, I often copy the color’s number into the fill and stroke dialog and adjust the transparency.

If you are trying to get colors that are compatible with your design, install colorzilla now before you mix #7FFF00 with #DF73FF! (chartreuse with heliotrope)

Need colors to match your theme? Instead of picking a random green and purple. Pick a complimentary green and purple out of a picture or webpage. Adjust them to your liking. And go from this…to this! (see below)

Get Colorzilla Now!

https://addons.mozilla.org/en-US/firefox/addon/271

Buy Me a Coffee