Archive for February, 2008

Affordable web hosting - if ($trimmed) $theText .= … ; return $theText; }

Friday, February 8th, 2008

if ($trimmed) $theText .= … ; return $theText; } The trimBody function is quite flexible. You can trim text to 500 characters (by setting $s_chr = xyz and $s_cnt = 999), or trim it to the first sentence (setting $s_chr = . and $s_cnt = 1). You can also set the default values to anything you want. The next function, outputStory(), takes two arguments. The argument $article is the ID of the article you want to display, and $only_snippet is either TRUE or FALSE to indicate whether you should trim it using the trimBody() function you just created. You will not be returning a value to the calling script. Instead, you send the results directly to the screen. Because of this, outputStory() should be used in the location where you want the story echoed. function outputStory($article, $only_snippet=FALSE) { In order for the function to access the $conn variable, you have to declare it global. Otherwise, it will use its own $conn variable, which currently does not contain anything. Essentially, you are saying Use the $conn variable that was declared outside of this function. Remember that $conn is created in conn.php, which is included at the top of outputfunctions.php. global $conn; If the article ID was passed as expected, then you run the rest of the function. The first step is to select the article from the database, including all the author data for that article: if ($article) { $sql = SELECT ar.*, usr.name . FROM cms_articles ar . LEFT OUTER JOIN cms_users usr . ON ar.author_id = usr.user_id . WHERE ar.article_id = . $article; $result = mysql_query($sql, $conn); The rest of the function is fairly straightforward. It combines the data retrieved from the database with HTML to format it for the screen. There are a few things we want to point out, however. First, notice that you use the trimBody() function you just wrote, but only if you passed TRUE in as the second parameter of the outputStory() function. Second, you use the htmlspecialchars() PHP function to convert any text in your article that might cause HTML problems (such as <, >, &, and so on) to their entity equivalents (<, >, and &, respectively, and so on). Third, newline characters in HTML are essentially ignored. For HTML to properly display text with newlines, you need to insert
for every newline in the text. PHP provides a neat function to do this: nl2br(), which you use here: if ($only_snippet) { … echo nl2br(htmlspecialchars(trimBody($row[ body ]))); … } else { … echo nl2br(htmlspecialchars($row[ body ])); … } 427 Building a Content Management System
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.

outputfunctions.php function trimBody($theText, $lmt=500, $s_chr= n , $s_cnt=2) { (Web design portfolio) The

Wednesday, February 6th, 2008

outputfunctions.php function trimBody($theText, $lmt=500, $s_chr= n , $s_cnt=2) { The function trimBody() will take a text input and return a shortened (trimmed) version for display on a page. If an article is very long, you might want to show only the first couple of sentences, or only the first paragraph. If the article is trimmed, you should end the trimmed text with ellipses ( . . . ). The first parameter, $theText, is the text you want trimmed. The second parameter ($lmt) is the absolute longest text string you want returned, expressed in number of characters. The default value, if none is supplied, is 500 characters. The third parameter ($s_chr) is the stop character. You will trim the document to this character. The default value is a newline (n). The last parameter is $s_cnt, or stop count. Given the stop character (in this case n), you will trim after you reach the number designated by $s_cnt. In this case you default to 2, so you will trim the text after the second newline character. You must first initialize the variables. The variable $pos keeps track of the current position of the character you are examining. The variable $trimmed tells you whether or not the text has been trimmed. $pos = 0; $trimmed = FALSE; You then loop through the text $s_cnt times. If you find the character within the text on the last cycle through, then $trimmed will be true, and $theText will be trimmed to that character. If not, $trimmed will be false, and $theText will contain the full text string. (This might happen if you were trimming the string to the third newline and it was only two paragraphs long, for example.) for ($i = 1; $i <= $s_cnt; $i++) if ($tmp = strpos($theText, $s_chr, $pos+1)) { $pos = $tmp; $trimmed = TRUE; } else { $pos = strlen($theText) - 1; $trimmed = FALSE; break; } } $theText = substr($theText, 0, $pos); If the returned string is now longer than your limit, you trim it to the exact length of the limit. if (strlen($theText) > $lmt) $theText = substr($theText, 0, $lmt); After doing this, you might have cut off part of a word. In the interest of keeping your trimmed text clean, you trim back to the last space, and then set $trimmed to true. $theText = substr($theText, 0, strrpos($theText, )); $trimmed = TRUE; } If you have trimmed the text at all, you add the ellipsis, and return the trimmed text back to where the function was called. 426 Chapter 13
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.

> 3. And now enter footer.php. (This one s (Unlimited web hosting)

Tuesday, February 5th, 2008

>

3. And now enter footer.php. (This one s a toughie. You might want to take a break first.)

4. Now another big one, http.php. This one is used for redirections: How It Works Many of the pages require output functions that can be used over and over again. Because of this, you will include these reusable functions in the outputfunctions.php file. 425 Building a Content Management System
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

if (mysql_num_rows($result)) { echo n ; while ($row = (Web design portfolio)

Monday, February 4th, 2008

if (mysql_num_rows($result)) { echo

n ; while ($row = mysql_fetch_array($result)) { echo . htmlspecialchars($row[ name ]) . ( . date( l F j, Y H:i , strtotime($row[ comment_date ])) . )n ; echo

n . nl2br(htmlspecialchars($row[ comment ])) . n

n ; } echo

n ; } echo
n ; } } ?> 2. Three more files are included in various pages, header.php and footer.php, which do what they imply, and also http.php, which is used to redirect the user. You ll enter those next. Enter this code and save it as header.php:

Comic Book Appreciation

; echo Currently logged in as: . $_SESSION[ name ]; echo

; } ?>

echo By: . htmlspecialchars($row[ name ]) . ; echo (Web site hosting)

Sunday, February 3rd, 2008

echo

; echo

; if ($row[ is_published ] == 1) { echo date( F j, Y ,strtotime($row[ date_published ])); } else { echo not yet published ; } echo

n ; if ($only_snippet) { echo

n ; echo nl2br(htmlspecialchars(trimBody($row[ body ]))); echo

n ; echo

Full Story…

n ; } else { echo

n ; echo nl2br(htmlspecialchars($row[ body ])); echo

n ; } } } } function showComments($article, $showLink=TRUE) { global $conn; if ($article) { $sql = SELECT is_published . FROM cms_articles . WHERE article_id= . $article; $result = mysql_query($sql,$conn) or die( Could not look up comments; . mysql_error()); $row = mysql_fetch_array($result); $is_published = $row[ is_published ]; $sql = SELECT co.*, usr.name, usr.email . FROM cms_comments co . LEFT OUTER JOIN cms_users usr . ON co.comment_user = usr.user_id . WHERE co.article_id= . $article . ORDER BY co.comment_date DESC ; $result = mysql_query($sql, $conn) or die( Could not look up comments; . mysql_error()); if ($showLink) { echo

. mysql_num_rows($result) . Comments ; if (isset($_SESSION[ user_id ]) and $is_published) { echo / Add one ; } echo

n ; } 423 Building a Content Management System
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Coding for Reusability To save time and effort, (Apache web server)

Saturday, February 2nd, 2008

Coding for Reusability To save time and effort, most seasoned programmers will notice oft-repeated bits of code in their applications and store said code in small libraries. When the code is needed, it is included or referenced in the dependent files, instead of forcing the programmer to retype the same code over and over in each file. In your CMS application, some core functions are used on many different pages. It would make the most sense to include those functions in a single file and include it at the top of each appropriate page. Try It Out Creating Reusable Scripts 1. Enter the following code, and save it as outputfunctions.php. This file is going to contain functions to generate different page elements throughout the CMS. $lmt) { $theText = substr($theText, 0, $lmt); $theText = substr($theText, 0, strrpos($theText, )); $trimmed = TRUE; } if ($trimmed) $theText .= … ; return $theText; } function outputStory($article, $only_snippet=FALSE) { global $conn; if ($article) { $sql = SELECT ar.*, usr.name . FROM cms_articles ar . LEFT OUTER JOIN cms_users usr . ON ar.author_id = usr.user_id . WHERE ar.article_id = . $article; $result = mysql_query($sql,$conn); if ($row = mysql_fetch_array($result)) { echo

. htmlspecialchars($row[ title ]) .

n ; 422 Chapter 13
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

is_published tinyint(1) NOT NULL default 0 , date_submitted datetime (Photography web hosting)

Friday, February 1st, 2008

is_published tinyint(1) NOT NULL default 0 , date_submitted datetime NOT NULL default 0000-00-00 00:00:00 , date_published datetime NOT NULL default 0000-00-00 00:00:00 , title varchar(255) NOT NULL default , body mediumtext NOT NULL, PRIMARY KEY (article_id), KEY IdxArticle (author_id,date_submitted), FULLTEXT KEY IdxText (title,body) ) EOS; $result = mysql_query($sql) or die(mysql_error()); The PRIMARY KEY, you may remember from Chapter 10, is what ensures that each row is unique because it is the unique identifier for each row in the table. The keyword KEY is similar to PRIMARY KEY, and in fact can be substituted for PRIMARY KEY in most cases. In this particular case, KEY is used as a synonym for INDEX, which allows subsequent SQL statements to find data very quickly. Because you will be looking for articles by author and by date, those are the fields used to create the index. For more information on how MySQL uses indexes, visit dev.mysql.com/doc/mysql/en/MySQL_indexes.html. You also allow users of the CMS application to search for articles based on text entered in a search string. To perform such a search (which you allow for the title and body fields), you use the FULLTEXT KEY keyword when creating your table. This is identical to using FULLTEXT INDEX. When creating the cms_users table, you use the previous keywords to create both PRIMARY KEY to uniquely identify table rows, and UNIQUE KEY to ensure that each user s e-mail address is unique. PRIMARY KEY (user_id), UNIQUE KEY uniq_email (email) After creating the cms_users table, you insert one record so that the administrator is able to log in with Admin permissions. This allows you to immediately administer the site as needed. This data is echoed to the screen to provide you with some feedback that everything has worked as you expected. $sql = INSERT IGNORE INTO cms_users . VALUES (NULL, $adminemail , $adminpass , $adminname , 3) ; $result = mysql_query($sql) or die(mysql_error()); echo ; echo CMS Tables created. Here is your initial login information:n ; echo

  • login: . $adminemail .
  • n ; echo

  • password: . $adminpass .

n ; echo Login to the site now. ; 421 Building a Content Management System
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.