CS 601 – PHP Multiline Strings

During the course of my project, I had many instances of needing to write very long strings, often when constructing moderately complex MySQL queries. There are several way to do this. One would be repeated each statements or assignment with .= . Another would be running the echo/assignment over multiple lines by using . at the end of each until the final one (ended with ; ). Example:

$mystr = 'Here is a really long line of text that runs to '.
         'more than one line';

I found a very clean and convenient way is to use PHP’s heredoc syntax. Here’s an example:

$query = <<<EOT
        SELECT * FROM tblOrderItems
        INNER JOIN tblFoodItems
                ON tblFoodItems.intFoodItemID = tblOrderItems.intFoodItemID
        INNER JOIN tblFoodCategories
                ON tblFoodCategories.intFoodCategoryID = tblFoodItems.intFoodCategoryID
        INNER JOIN tblFoodCategoryOrder
                ON tblFoodCategoryOrder.intFoodCategoryID = tblFoodCategories.intFoodCategoryID
        WHERE intOrderID = {$order['intOrderID']}
        ORDER BY tblFoodCategoryOrder.intFoodCategoryOrder, tblFoodItems.vcFoodItemName
EOT;

There’s a few things to note:

  • The EOT is arbitrary as long as both instances are the same. (I use EOT for “end of text”.)
  • Variables can be included in the string. A simple variable is simply included as $myvar. Here, I use the {} to separate out a “complex” variable. (I didn’t realize this could be done until later on, so for some of my earlier instances, I set it to a simpler variable immediately before the heredoc declaration.)
  • The terminator (here EOT;) MUST be on a line by itself with no leading or trailing whitespace. This cannot be emphasized enough. Leading whitespace is pretty easy to spot, but several times NetBeans added trailing whitespace which wasn’t obvious in and of itself. (NetBeans does color the heredoc string differently, so you will see the “carry over”.) I finally got in the habit of checking.

PHP ≥ 5.3.0 also has the nowdoc syntax. It’s very similar to heredoc except is uses $myvar = <<<‘EOT’ (note the single quotes). The difference is that variables are not expanded akin to standard single quoted strings.

CS 601 – Different Footers Depending on State

One of the things I’m doing with the site is to have different footers depending on if a user is logged in. The footer will contain a second, static menu for accessibility reason. This would be pretty straightforward to do in PHP in the footer page except I have different numbers of items depending on if a customer is logged in, if an employee is logged in, or if no one is logged in. This means the div widths have to change to keep it centered in the footer menu wrapper div. I could do this in the page, but I thought it would get messy and cumbersome. Instead I use includes.

In index.php:

$loggedinCustomer = True;  // Just here for styling testing.
$loggedinEmployee = False; // Will be extracted from login cookie.

In footer.php:

if ($loggedinCustomer) {
    include('footermenuCustomerLoggedIn.html');
} else if ($loggedinEmployee) {
    include('footermenuEmployeeLoggedIn.html');
} else {
    include('footermenuNotLoggedIn.html');
} 

Each of the HTML pages is the html for just the menu and not a complete page (with head and body elements, etc.) The main.css file then contains selectors for different divs, etc., classes of NotLoggedIn, CustomerLoggedIn, and EmployeeLoggedIn. In fact, considering it, I could even import different CSS files depending on the login state, but that might be overkill for this.

This looks like an interesting solution. and I may use it for more than this. It also follows the model-view-controller (MVC) desing concept presented by Murach.

PS, coming soon will be the post about MySQL developer which I promised a couple of weeks back. I’m also going to do one on the Netbeans IDE. For the latter, suffice to say that if you’re not using it (or an equivalent IDE), get it.

CS 601 – Tip for long divs

While working on this weeks homework, I had a couple of times when I lost track of which ending div tag went with the starting tag. I started adding it and commenting it as soon as I write it:

<div id="content">
</div> <!-- END content -->

I then add the content in between. This way, as the content gets very long, I still know which </div> goes with which <div>. In fact, I have also starting do this for other elements that might be long (tables, lists) in html and for code blocks in PHP.

CS 601 – Murach Product Manager “correction”

While working through the Product Manager application in Murach’s Chapter 4, I was getting errors on the following statement:

1: if(!isset($category_id)) {
2:   $category_id = $_GET['category_id'];
3:   if (!isset($category_id)) {
4:     $category_id = 1;
5:   }
6: }

The error was an invalid index for the expression on line 2.

Notice:  Undefined index: category_id in C:\xampp\htdocs\
cs601\week4\HW\index.php on line [2]

I had originally rewritten the code, but also tried with a direct copy and paste. I fixed the error using the isempty function:

1: if (!isset($category_id)) {
2:   if (!empty($_GET)) {
3:     $category_id = $_GET['category_id'];
4:   } else {
5:     $category_id = 1;
6:   }

(Note, I’m running in PHP 5.3.5, the book requires 5.3.)