Script displaying wrong info
Posted: Thu Oct 26, 2006 10:06 am
My full script is below. The problem with this is my displayOneItem function is also showing "There are no articles in this category"
This is the error message from another function called displayList. I can't work out why this is happening, could someone help please.
This is the error message from another function called displayList. I can't work out why this is happening, could someone help please.
Code: Select all
<?php
$type = 1;
$dbc = mysql_connect ('localhost','root','admin');
mysql_select_db ('UNC',$dbc);
function displayCategories() {
global $dbc, $username, $type;
$query = "SELECT * FROM site_categories WHERE cat_type=$type";
$result = mysql_query ($query)or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error());
while ($row = mysql_fetch_assoc ($result)) {
/* display categories in a simple table */
echo "<TABLE border=\"1\" width=\"580\">\n";
$description = htmlentities ($row['description']);
$title = htmlentities ($row['cat_name']);
echo "<TR><TD width=\"300\"><b>$title</b></TD>";
/* get number of articles */
$comment_query = "SELECT count(*) FROM downloads WHERE cat_id={$row['cat_id']}";
$comment_result = mysql_query ($comment_query);
$comment_row = mysql_fetch_row($comment_result);
/* display number of articls with link */
echo "<TD><a href=\"{$_SERVER['PHP_SELF']}" .
"?action=show&cat_id={$row['cat_id']}\">Articles</a>" .
"($comment_row[0])</TD></TR>\n";
echo "<tr><td colspan=\"2\">$description</td></tr>";
/* finish up table*/
echo "</TABLE>\n";
echo "<BR>\n";
}
}
function displayList($cat_id) {
global $dbc, $type, $redirectpage;
$dbc = mysql_connect ('localhost','root','admin');
mysql_select_db ('UNC',$dbc);
/* query for item */
$query = "SELECT * FROM downloads WHERE cat_id='$cat_id'";
$result = mysql_query ($query)or die("Problem with the query: $query on line:" . __LINE__ . "<br>" . mysql_error());
/* if we get no results back, error out */
if (mysql_num_rows ($result) == 0) {
echo "There are no articles in this category\n";
echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=all\">Back</a>\n";
return;
}
while ($row = mysql_fetch_assoc ($result)) {
echo "<TABLE border=\"1\" width=\"580\">\n";
/* easier to read variables and
* striping out tags */
$title = htmlentities ($row['down_title']);
$author = htmlentities($row['author']);
$postdate = htmlentities($row['postdate']);
/* display the items */
/* display number of articls with link */
echo "<TD><a href=\"{$_SERVER['PHP_SELF']}" .
"?action=display&id={$row['down_id']}\">Article</a>" .
"</TD></TR>\n";
echo "<TR><TD><b>$title</b></TD></TR>\n";
echo "<TR><TD>$postdate<br /><i>Posted by $author </TD></TR>";
echo "</TABLE>\n";
echo "<BR>\n";
}
}
function displayOneItem($id) {
$dbc = mysql_connect ('localhost','root','admin');
mysql_select_db ('UNC',$dbc);
global $dbc;
/* query for item */
$query = "SELECT * FROM downloads WHERE down_id=$id";
$result = mysql_query ($query) OR die ('Could not connect to MySQL: ' . mysql_error() );
/* if we get no results back, error out */
if (mysql_num_rows ($result) == 0) {
echo "Bad article id\n";
return;
}
$row = mysql_fetch_assoc($result);
echo "<TABLE border=\"1\" width=\"580\">\n";
/* easier to read variables and
* striping out tags */
$title = htmlentities ($row['down_title']);
$news = nl2br (strip_tags ($row['comments'], '<a><b><i><u>'));
$author = htmlentities($row['author']);
$postdate = ($row['postdate']);
/* display the items */
echo "<TR><TD><b>$title</b></TD></TR>\n";
echo "<TR><TD>$news<br /><i>Posted by $author </TD></TR>";
echo "</TABLE>\n";
echo "<BR>\n";
}
echo "<CENTER>\n";
switch($_GET['action']) {
case 'display':
displayOneItem($_GET['id']);
case 'show':
displayList($_GET['cat_id']);
break;
case 'all':
displayCategories(1);
break;
default:
displayCategories();
}
echo "</CENTER>\n";
?>