Content M anagement System 299 FUNCTIONS FROM / CONTENT/ FUNCTIONS.PHP

Chapter 11: Content M anagement System 299 FUNCTIONS FROM / CONTENT/ FUNCTIONS.PHP

These will be used throughout the application. There will be many references to Chapter 9 in this section.

LIST_STORIES( ) As mentioned in Chapter 10, in MySQL version 3.23 you can work MySQL’s lack of support for unions by creating temporary tables. However, we developed all of the applications on this book using MySQL version 3.22, which does not support the create temporary table feature. This gets to be a problem when you need to print out the contents of two queries within a single table. If you look at the source code on the CD-ROM, you will see that the pages start by printing all of the stories currently within the workflow that the current user has access to. Following that, if the user has access to live stories, the live stories should be printed within the same table.

To create this table, we need to call this function twice, passing the result iden- tifier of the query as the first argument. If the query has at least one row, the table header will be printed, and $in_table will be set to 1. The next time the function is called, the value of $in_table will be passed as the second argument. If the value of $in_table is 1 the header will not print.

Again, if you are using MySQL version 3.23, use the create temporary table feature. function list_stories($result, $in_table=””)

{ if (empty($in_table)) { $in_table = 0; }

while ($row = mysql_fetch_array($result)) {

if ($in_table == 0) {

print subtitle(“Edit Stories”); print start_table(); print table_row(“<b>Stage</b>”

, “<b>Story</b>” , “<b>Publish Date</b>”

); $in_table = 1;

} print table_row($row[“stage”],

anchor_tag(“edit_story.php?story_id=”.$row[“story_id”] , $row[“headline”] ) , $row[“publish_dt”]

); } return $in_table;

300 Part IV: Not So Simple Applications

FETCH_STORY( ) This function, like most of those in this section, makes use of the fetch_record() function, which is discussed in Chapter 9. In the call to the fetch_ record() , a story_id, which is the primary key of the table, is specified. Therefore, you can be sure that the query created by fetch_record will have only one row. The query in fetch_record will get all columns for the story_id.

function fetch_story ($story_id=””) {

$result = fetch_record(“stories”,”story_id”,$story_id); return $result;

} Because there is only one row, the fetch_record will call the set_result_

variable() function (also discussed in Chapter 9). That function will make all of the columns for that story_id available as globals. From the schema in Figure

11- 7, you can see that those columns are story_id int, stage_id, publish_dt, head- line, subtitle, byline_prefix, summary, and body. So after running fetch_story you can access any of the columns as global variables.

fetch_story(1); echo $headline;

The fetch_record() function is discussing in Chapter 9.

XREF

FETCH_STORY_VERSION( ) This function works almost identically to the fetch_ story() function just described, the only difference being that multiple attributes are being passed to the fetch_record function. These are added to the SQL statement. Note that the modify_dt column is 14 digits (YYYYMMDDHHMMSS). The combina- tion of a story_id and the second at which it was modified makes for a pretty good two-column primary key, so any query here should only return one row.

function fetch_story_version ($story_id=””,$modify_dt=””) {

$result = fetch_record(“story_versions” ,array(“story_id”,”modify_dt”) ,array($story_id,”’$modify_dt’”)

); return $result;