Affordable web hosting - if ($trimmed) $theText .= … ; return $theText; }
Friday, February 8th, 2008if ($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.