Prev | Current Page 531 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

For the user, the SID satisfies this requirement. The
documents can be identified really in any way you wish, but this example uses the
article??™s title and URL, and assumes that this information is derived from data stored
in a database table named articles, displayed here:
CREATE TABLE articles (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(50),
content MEDIUMTEXT NOT NULL,
PRIMARY KEY(id)
);
The only required task is to store the article identifiers in session variables, which
is implemented next:
// Start session
session_start();
// Connect to server and select database
mysql_connect("localhost","webuser","secret");
mysql_select_db("chapter18");
// Retrieve requested article id
$articleid = mysqli_real_escape_string($_GET['id']);
462 CHAPTER 18 ?–  SESSION HANDLERS
// User wants to view an article, retrieve it from database
$query = "SELECT title, content FROM articles WHERE id='$id'";
$result = mysql_query($query);
// Retrieve query results
list($title, $content) = mysql_fetch_row($result);
// Add article title and link to list
$articlelink = "$title";
if (! in_array($articlelink, $_SESSION['articles']))
$_SESSION['articles'][] = $articlelink;
// Output list of requested articles
echo "

$title

$content

";
echo "

Recently Viewed Articles

";
echo "
    ";
    foreach($_SESSION['articles'] as $doc) echo "
  • $doc
  • ";
    echo "
";
?>
The sample output is shown in Figure 18-1.


Pages:
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543