Location: PHPKode > projects > top serious project > bco/functions/functions.watch.php
<?php

function watch($id)
{

    global $user_array, $logged_in;

    if (!$logged_in) {
        header("Location: http://board.crewcial.org/login.php");
        exit;
    }

    if ((!isset($id)) && (!is_numeric($id))) {
        bco_error("That id passed wasn't there, or was not numeric. Bad.");
    }

    $exists_query  = "select id from subject_index where id=$id order by id desc limit 1";
    if (!$result = pg_query($exists_query)) {
        bco_error("Exists query failed.");
    }

	if (pg_num_rows($result) == 0) {
        bco_error("Thread does not exist.");
    }
	unset($result);

	$last_post_id_query = "select id from messages where id_subject=$id order by id desc limit 1";
	if (!$result = pg_query($last_post_id_query)) {
		bco_error("Getting last post id failed.");
	}

    $last_post_id = pg_fetch_result(pg_query($last_post_id_query), 0);

    // If the user is already watching this thread, don't add it.
    $watched_query  = "select user_id from threads_watched where user_id=$user_array[myuserid]";
    $watched_query .= " and thread_id=$id";
    if (pg_num_rows(pg_query($watched_query)) > 0) {
        bco_error("You are already watching this thread.");
    }

    $add_id_query = "insert into threads_watched (user_id, thread_id, last_post_id) values ($user_array[myuserid], $id, $last_post_id)";

    if (!$result = pg_query($add_id_query)) {
        bco_error("Adding id failed.");
    }
    
    return true;
}

function unwatch($id)
{

    global $user_array, $logged_in;

    if (!$logged_in) {
        header("Location: http://board.crewcial.org/login.php");
        exit;
    }

    if ((!isset($id)) && (!is_numeric($id))) {
        bco_error("That id passed wasn't there, or was not numeric. Bad.");
    }

    $exists_query  = "select id from subject_index where id=$id";
    if (!$result = pg_query($exists_query)) {
        bco_error("Exists query failed.");
    }

    // If the user is already watching this thread, don't add it.
    $watched_query  = "select user_id from threads_watched where user_id=$user_array[myuserid]";
    $watched_query .= " and thread_id=$id";
    if (pg_num_rows(pg_query($watched_query)) == 0) {
        bco_error("You are not watching this thread.");
    }

    if (pg_num_rows($result) == 0) {
        bco_error("Thread does not exist.");
    }

    $add_id_query = "delete from threads_watched where user_id=$user_array[myuserid] and thread_id=$id";
    if (!$result = pg_query($add_id_query)) {
        bco_error("Deleteing id failed.");
    }

    return true;
}

function update_watch($id)
{

    global $user_array, $logged_in;

    if (!$logged_in) {
        header("Location: http://board.crewcial.org/login.php");
        exit;
    }

    if ((!isset($id)) && (!is_numeric($id))) {
        bco_error("That id passed wasn't there, or was not numeric. Bad.");
    }

    $exists_query  = "select id from subject_index where id=$id";
    if (!$result = pg_query($exists_query)) {
        bco_error("Exists query failed.");
    }

    // If the user is already watching this thread, don't add it.
    $watched_query  = "select user_id from threads_watched where user_id=$user_array[myuserid]";
    $watched_query .= " and thread_id=$id";
    if (pg_num_rows(pg_query($watched_query)) == 0) {
        bco_error("You are not watching this thread.");
    }

    if (pg_num_rows($result) == 0) {
        bco_error("Thread does not exist.");
    }

	$last_post_id_query = "select id from messages where id_subject=$id order by id desc limit 1";
    if (!$result = pg_query($last_post_id_query)) {
        bco_error("Getting last post id failed.");
    }

    $last_post_id = pg_fetch_result(pg_query($last_post_id_query), 0);

    $add_id_query = "update threads_watched set last_post_id=$last_post_id where user_id=$user_array[myuserid] and thread_id=$id";
    if (!$result = pg_query($add_id_query)) {
        bco_error("Updating watch list failed.");
    }

    return true;
}
Return current item: top serious project