<?php 

function showstories() {


if (!$db = @mysql_connect("www.rationalmind.net", "capworld", "capworld")) {

	$db_error = "Could not connect to MySQL Server";

	do_error($db_error);

}


mysql_select_db("capworld",$db);

$result = mysql_query("SELECT id,DatePosted,TimePosted,Author,Subject,Description,IntroText,Title,imgtype,ImgAltText,ImgURL, Text FROM articles ORDER BY id",$db);

if ($myrow = mysql_fetch_array($result)) {
  do {

printf("<h2>%s</h2>\n", $myrow["Title"]);
echo "<div id=\"article\">";

if ($myrow["imgtype"]) {
printf("<img src=\"fetch.php?id=%s\" alt=\"%s\" align=\"right\" class=\"article-img\" />\n", $myrow["id"],$myrow["ImgAltText"]); }

if (strlen($myrow["ImgURL"]) > 4) {
printf("<img src=\"%s\" alt=\"%s\" align=\"right\" class=\"article-img\" />\n", $myrow["ImgURL"], $myrow["ImgAltText"]); }
// $txt = preg_replace("/(\015\012)|(\015)|(\012)/","<br />",$myrow["Description"]); 
printf("<p class=\"introtext\">%s</p>\n", nl2br($myrow["IntroText"]));
printf("<p>%s</p>\n", nl2br($myrow["Description"]));
if (strlen($myrow["Text"]) > 5) {
printf("<p><a href=\"%s?id=%s\" class=\"clickforfull\">Click here to read the full story.</a>\n", $PHP_SELF, $myrow["id"]); }
echo "</p>\n";
echo "<hr /></div>\n";

  } while ($myrow = mysql_fetch_array($result));
} else {
  echo "Sorry, no records were found!\n";	
}

} // end function

function showstory($id="0") {

if ($id == 0) {

echo "No story specified!";
}
else
{
// Get full text and photo based on $id


if (!$db = @mysql_connect("www.rationalmind.net", "capworld", "capworld")) {

	$db_error = "Could not connect to MySQL Server";

	do_error($db_error);

}


mysql_select_db("capworld",$db);

$sql = "SELECT * FROM articles WHERE id ='".$id."'";
// echo $sql;
$result = mysql_query($sql ,$db);

if ($myrow = mysql_fetch_array($result)) {
  do {

printf("<h2>%s</h2>\n", $myrow["Title"]);
echo "<div id=\"article\">";

printf("<p class=\"introtext\">%s</p>\n", nl2br($myrow["IntroText"]));
printf("<p>%s</p>\n", nl2br($myrow["Description"]));

if ($myrow["imgtype"]) {
printf("<img src=\"fetch.php?id=%s\" alt=\"%s\" align=\"right\" class=\"article-img\" />\n", $myrow["id"],$myrow["ImgAltText"]); }

if (strlen($myrow["ImgURL"]) > 4) {
printf("<img src=\"%s\" alt=\"%s\" align=\"right\" class=\"article-img\" />\n", $myrow["ImgURL"], $myrow["ImgAltText"]); }
echo "<div id=\"article\">";
printf("<p>%s</p>\n", nl2br($myrow["Text"]));
//echo "<img src=\"Image\" alt=\"\" align=\"right\" class=\"article-img\" />\n";  // TODO
printf("<br /><a href=\"%s\" class=\"clickforfull\">Back to homepage</a>", $PHP_SELF);
echo "<hr /></div>";

  } while ($myrow = mysql_fetch_array($result));
} else {
  echo "Sorry, no story by that id was found!";	
	}
} // end story specified if
}

function  do_error($error) {

	echo  "<h2>Hmm, looks like there is a connection problem between the webpage and the database...<br \>";

	echo "The reported error was \"$error.\"\n<br \>";

	echo "Best you get hold of the <a href=\"mailto:errors@rationalmind.net\">site admin</a> and let him know.</h2><br \>";

	die;

}



?>
