A potpourri of Web Developmemt, Linux, and Windows tidbits and observations

Passing Variables – Passing Objects in PHP

Posted by dale | PHP | Wednesday 24 March 2010 11:41 am

We’ve looked at passing variables, because of the stateless nature of html and the Internet. Every internet page that opens starts at ground zero with no prior knowledge of anything that came before it. Any information that page needs has to be passed to the page.

We’ve done this in a number of ways: with html forms using get and post, with cookies, with sessions, and with includes to pass variables to functions. We’ve discussed these methods in previous posts. What about objects?

Objects can be passed to another page, but there’s a couple of gotcha’s that need to be accounted for first. Objects as you know consist of properties, or variables, and methods described in a class. The class describes the structure of any instance of an object created from that class. There’s more information in objects to convey, or pass to another file or function, then with a simple variable.

If we passed the object instance like any other variable, the receiving file would not know anything about the object or its class, and the receiving file would just think it was some sort of bit stream coming to it. What we need to do is to encode the object information along with the object instance before we pass the object and decode it when we get to the new location.

Step number one is to make sure that the class is in scope, or visible, to the object instance we are going to pass. This way the class information is available to be passed. Let’s make a class and instance and pass it.

First the class…

class carwash {

    private $carmake;
    private $caryear;

    public function getCarmake () {
        return $this->carmake;
    }

    public function setCarmake ($carmake) {
        $this->carmake = $carmake;
    }

    public function getCaryear () {
        return $this->caryear;
    }

    public function setCaryear ($caryear) {
        $this->caryear = $caryear; 

    }

    public function washcar () {
        echo "scruba, dub, dub, scruba, dub, dub";
        echo " I'm feeling cleaner, Thank you.";
    }
}

Even though normally we will keep our classes in separate files, for the sake of brevity lets keep it in the same file and make an instance of the class that we will pass.

    $cartopass = new carwash();

    $cartopass->setCarmake("Buick");
    $cartopass->setCaryear(1999);

We have our object instance, and we have set its parameters to a 1999 Buick. What we want to do is send all of the above variables and class information over to the next file in the form of a long encoded string. We do this with serialization and use a session variable to store the byte encoded string. This is done with one line like so…

    $_SESSION['encoded_cartopass'] = serialize($cartopass) ;

Don’t forget to call session_start() at the top of the file. We now have a session variable that is available to us in the receiving page, so let’s go there…

The first thing we need to do is unpack, or unencode, the object. Let’s do that.

// First let's get the variable from the session

$encoded_cartopass = $_SESSION['encoded_cartopass'];

// Now let's unpack it

$cartopass = unserialize($encoded_cartopass);

// Note we could have done this in one step, like so

$cartopass = unserialize ( $_SESSION['encoded_cartopass'] );

Now we can use the object in the new page. Here’s an example:

    echo "Your car is a " . $cartopass->getCarmake ();
    echo "Made in ". $cartopass->getCaryear() . ". ";

// and we can use the class method  

    $cartopass->washcar();

The output on the screen would read. (I put in the html line breaks that don’t print out in the code in this post).

    Your car is a Buick
    Made in 1999.
    scruba, dub, dub, scruba, dub, dub
    I'm feeling cleaner, Thank you.

The Kindle Reader for the Windows PC

Posted by dale | Software,Windows | Saturday 20 March 2010 5:04 pm

I had an interesting discussion today at work. One of my co-workers arrived in the office with a couple of O’Reilly textbooks, and said they are on my bookshelf, if you’d like to use them as a reference. He said he was tired of looking up solutions to his coding problems on Google, fixing his problem, and then going on to the next problem without really learning anything. He wanted more information, thus the textbooks appearing on his bookshelf.

I had to admit, I was in the habit of not looking things up in texts, and just trying to find the answer to my one problem on the Internet. It usually works. I don’t know if you want to call this being lazy, or being efficient, maybe a little of both.

This led to electronic books and readers now available on line that can answer your problem and at the same time go beyond and give you the background information for future problem solving. We both agreed that we don’t like reading books on line and prefer a text in our hands. There’s something about the smell and feel of books in your hand that makes you treasure them, and enjoy the read.

The Kindle Reader for the Windows PC

Following our discussion, I downloaded the “new” free Amazon Kindle reader for the PC, and a free Kindle book from Amazon to check out the reader. This is currently just a Window’s application, and it is in “beta” which means there are some bugs. They ask you to put in your Amazon account id and password which automatically links the reader to your account. Now with one click of the mouse you can buy a Kindle book and download it to your PC reader.

There’s not a lot of free Kindle books available, but enough to check out the reader. You can find them on Amazon by searching for Kindle books, and a list of books are returned. A “Sort By” box will appear in the upper right side of the search results window, click on it, and sort by “Price low to high.” All the free books rise to the top of the pile. Sherlock Holmes it was. And the purchase at $0.00 was completed. Amazon’s hope is you’ll build up a PC library, and then buy a $259 Kindle so you can take it with you.

They did a nice job with the software, and the reader is nice. You can change font size and page width easily, to make it universally acceptable for all eyesight, and it keeps track of what page you are on in the read. You move pages by the arrow keys or scrolling and can jump via bookmarks and a table of contents.

Since I had loaded it at work and it seemed to work fine, I decided to try it at home and in the end unloaded it. I do not believe they have their syncs worked out yet, and maybe had problems with my loading it down twice.

One thing I did notice is that the books I “purchased” were kept at Amazon.  I had to share in my purchase in my account on Amazon, and then sync to ereader to read it.   A pain at best that does not work properly. If I understand this correctly, when I put down my money, I get a copy of the book for as long as I remain sync’d with Amazon, although this is done automatically by the software, a piece of me wants it both ways. Keep a record on Amazon, but also give me a copy on my computer or computers for instant set up and access.

The Kindle reader looks nice, but its not quite ready for prime time yet, and is still a beta. I think e-readers and by default PC readers will come into their own, but their still working out the kinks and are not quite ready for prime time.

Google Won’t Search

Posted by dale | Browsers,Companies,Software | Wednesday 17 March 2010 11:22 am

Instead of a portal, I use Google as my home page in my browser. I like the clean look, and when I’m looking for something, I don’t want to be distracted by a lot of information that you would find in most portals.

Recently I had a problem with Google. I typed in my query into the Google window, hit the search button, and nothing happen, no search results. Hmmm…what’s going on?

Let me digress for a moment to a pet peeve of mind. People who make software programs are human and want to make money just like the next guy. A way that software distributors make extra money is to load someone elses companion program along with their application. This companion program is usually made to be unobtrusive, not easily noticed, and usually is implemented in the form of a toolbar in your browser’s upper window. The “Ask toolbar” is probably the biggest culprit of this technique at the moment.

Normally when you load a utility program, during the installation, they give you a check box to uncheck, if you don’t want the toolbar loaded, but sometimes they just load it. Well behaved toolbars that suddenly pop up in your browser, usually can be turned off by right clicking on the toolbar, and turning off the toolbar you don’t want.

Back to the problem. Microsoft with their new Bing Search engine is desperate, and I do mean desperate, to gain market share in the search engine wars. They’ve decided to go the way of the “Ask toolbar” and load their toolbar into your browser when you load utility programs. This toolbar loads without an option not to load, and it puts their Bing search box prevalently in the upper left window, where you go for the “back” button.

Most folks, will not initially notice it, but if they do, and want to turn it off, a right click in the toolbar will not find the Microsoft Bing toolbar in the list of toolbars, not nice.

When a Google search does not return search results, I could see an unsuspecting user going right up to the Bing search bar, and doing their search from there, just what Microsoft wants.

Knowing and loving Microsoft the way I do, and seeing the Bing search box on my toolbar, I immediately suspected that there was some foul play going on here. I decided to get rid of the Bing toolbar. It turns out the only way to do that was to go to “Add Remove Programs” in the Control Panel. Once I removed the Microsoft Bing toolbar, and restarted my browser, the Google search results miraculously returned.

Now I’m not saying that Microsoft would make a toolbar that would interfere with the operation of a competitor’s program. I know Microsoft would never do that intentionally. They might change the format of Word documents with every release to make competitors trying to open their documents not be able to do that without a lot of work. Nor would I ever accuse Microsoft of intentionally slowing your computer down with every security update, so you would think your computer was getting old and slow, and go buy a new computer and operating system. I’m sure Microsoft only has benevolent intentions.

No, I’m just writing this post to let you know how to fix the problem if you find that Google will not give you search results, and you happen to have a Bing search box in your browser toolbar.

Using PHP to Switch CSS styles on a Web Page

Posted by dale | CSS,PHP | Tuesday 16 March 2010 9:37 pm

CSS is a very powerful language to make the content of a web page look way you want it to look.  The new CSS3, when it’s ready, promises to add another dimension to styling web pages and make it easier to do styling that now takes a little more effort with JavaScript.

One of the things that cause people problems, and is not built into CSS, is filtering CSS styles and applying one style if a certain condition exists.  In short, an “if” statement.

There are several workaround for this, one of which is to use PHP to switch styles depending on a condition.  Let’s take a look at the way you normally include a stylesheet in a web page.


The CSS file, cssfile.css, might look something like this:


h1 {color: blue;}
p {color: purple;}

.reguser {color: purple;}

Now suppose we only want the text in class “reguser”  to show up, with its special purple color styling, if the user is registered on your site, and if the visitor is not registered, then the text will not be displayed.

Let’s use PHP to set up the condition.


In the PHP portion we unset a variable that we will use to track a registered user to initialize the variable.  We then set the $user variable to true if a user registered on another web page, using a $_SESSION variable.

We then get to the “IF” statement.  “IF” the $user variable is NOT set, as in the visitor did not register on the site, we add another CSS include file.  As an aside, you could have just as easily added another CSS file with an “elseif” statement.

Most web pages have many CSS files included, so adding another CSS file is nothing unusual.  It is encouraged.  In fact, CSS defines rules called specificity and the Cascade, as in Cascading Style Sheets, that determine which styling rules apply if there is more than one style assigned to the same element.

Let’s see what the second CSS file looks like to take out the registered user text on the web page.


.reguser {display: none;}

Well, that was easy.  The CSS says not to display the element, so it will disappear off the page, and the PHP lets you have conditionals in your web page styling.

Text Editors and DarkRoom – a Review

Posted by dale | Software | Friday 12 March 2010 4:26 pm

I probably have spent more time looking at text editors than any other software application. I’m always on the lookout for a text editor that does just what I want, no more, no less.

There are so many text editors out there its very difficult to select an editor, simply because of the time it takes to evaluate them all.  Over the years, I have gone from editor to editor, as a new editor comes out and catches my attention. There is not one text editor that meets all my needs.  I find myself using several text editors depending on what I am doing.

DarkRoom

For the writing of formal documents that I use to communicate with others, I use a word processor, either Microsoft Word, or OpenOffice.org Writer. Lately, I have been using OpenOffice Writer.

For doing web development with PHP and MySQL, I use a full fledged IDE that has many coding utilities integrated into a development environment. I have tried, NetBeans, Aptana, Eclispe, and the one I currently use is Zend Studio.

For writing web pages, quickly, I do not need all the functionality offered by IDE’s. I need an “IDE Lite”, so to speak.   I look for a text editor that has some HTML and CSS code snippets built-in that I can quickly drop into my web page.  They usually have a file viewer.   I have tried PSPad, Notepad++, HTML Kit, Intype, and BlueFish.  I keep going back to HTML Kit, even though it takes some time to configure, just because it is so flexible and configurable.  I can make it into exactly what I want with an IDE Lite editor.

I have one editor I use to write either a blog article, a small program, or a note.  A “simple” text editor, an example would be Microsoft Notepad.  A “simple” text editor has the following characteristics: it loads extremely fast.  I click the icon, blink, and I’m ready to type.  It doesn’t have a lot of other functionality like code snippets, or file viewer, but it has a little more functionality than Notepad.  Alright, I’ll get right to it.

One of my major pet peeves with editors is color configuration.  I like a dark background, specifically #000066, which is a dark blue, with lime foreground text.  When you look at a monitor 8 to 12 hours a day, you get tired of staring at white background screens. It starts glaring at the eyes. I want to be able to have the colors the way I want them in my editor, and there lies the problems I have with editors.

With all the editors I use, you can configure the screen colors, except there’s always a catch.  It’s not easily done in the word processors, you have a huge amount of screen real estate taken up by menus, which you can not easily adjust or make disappear.  With IDE’s and the IDE Lites, you run into trouble changing colors, because of language color syntax highlighting, and you end up playing with the color configuration until you get all the different syntax highlighting visible on the screen without any disappearance due to the dark background.  You also seem to be constantly fiddling with the colors with the IDE’s.  I have found I have had to keep my color preferences in a separate notebook to quickly set up a new editor.  Long story short, I want a dark background that will not mess up the rest of the editor functionality. Note: to all you editor makers out there, I would like a dark default configuration, especially for you IDE folks, that I can change with one click.

Back to “simple” editors, Notepad does not allow you to change colors. The simple text editor I currently am using to write this article fits the bill nicely, and I must admit prompted this entire review.

It’s called “DarkRoom.”  I like DarkRoom a lot. It is a full screen editor, i.e. nothing shows on the screen except the editor window, no menus. It’s meant to have a dark background for those of us that write a lot.

DarkRoom is Windows based freeware that credits Mac based WriteRoom, as its inspiration.  DarkRoom has spawned a bunch of look-a-likes: Q10, JDarkRoom, PyRoom, WestEdit, and online editors, like Dark Copy, and Online Appwriter, but none of these do it as good as DarkRoom.

DarkRoom allows some basic configuration, like font, font-size, colors, and the look of the editor on start up, and that’s about it. And quite frankly that’s all I want. It opens in a flash from an icon on my desktop, and allows me to write full screen, if I press Esc or F11, a menu comes up that allows me to save, cut, and paste, some statistics, and search and replace. I don’t need to use the menu, because DarkRoom follows all the standard Windows keyboard conventions, like Ctrl-S saves the document.

I highly recommend you take a test drive of DarkRoom. It is an extremely good text writing editor, the kind of editor you would use to write a blog, sort of like this one.

Next Page »