I have been working on a way of adding user comments to vairous parts of my website, using a table with colomns:
id, com_id (int), page_id(int), name(text), comment(text)
what i want to do is use this 1 table to store comments for the whole site, using the page_id colomn eg my news page = 1 files page = 2.
the comments are then linked to the story/link/file by there id, eg when a news story is added it gets it id, when the a comment for that story its id becomes the com_id.
my problem comes when i want to display the comments with it news story.
atm the news is displayed using a repeat table propulated from my database, what i need is to then select the comments that go with each story, the only way i could come up with is placeing the comments query inside the <? do { } ?> and have it seach for comments. however this seems very a very slow way of working as i have to connect/query the database for every news item.
neway... affter that my question is... can i have 1 query that selects all the comments for a page, but then l8r show only those comments that go with each story??
i'll post the code in case nothing i said makes sence.... sorry if i'm posting to much code :/
<?php require_once('Connections/eod.php'); ?>
<?php
$maxRows_news = 10;
$pageNum_news = 0;
if (isset($HTTP_GET_VARS['pageNum_news'])) {
$pageNum_news = $HTTP_GET_VARS['pageNum_news'];
}
$startRow_news = $pageNum_news * $maxRows_news;
mysql_select_db($database_eod, $eod);
$query_news = "SELECT name, `date`, news, title, ID FROM news ORDER BY ID DESC";
$query_limit_news = sprintf("%s LIMIT %d, %d", $query_news, $startRow_news, $maxRows_news);
$news = mysql_query($query_limit_news, $eod) or die(mysql_error());
$row_news = mysql_fetch_assoc($news);
if (isset($HTTP_GET_VARS['totalRows_news'])) {
$totalRows_news = $HTTP_GET_VARS['totalRows_news'];
} else {
$all_news = mysql_query($query_news);
$totalRows_news = mysql_num_rows($all_news);
}
$totalPages_news = ceil($totalRows_news/$maxRows_news)-1;
?>
<? $page = 1 ?>
<html>
<head>
<title>Front Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="eod.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
<!--
/******************************************
* Contractible Headers Script- © Dynamic Drive ([url]www.dynamicdrive.com[/url])
* Visit [url]http://www.dynamicdrive.com/[/url] for full source code
* This notice must stay intact for use
******************************************/
var ns6=document.getElementById&&!document.all?1:0
var head="display:''"
var folder=''
function expandit(curobj){
folder=ns6?curobj.nextSibling.nextSibling.style:document.all[curobj.sourceIndex+1].style
if (folder.display=="none")
folder.display=""
else
folder.display="none"
}
//-->
</script>
</head>
<body background="images/eod_r2_c19.gif" bgproperties="fixed" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="330" border="0">
<tr>
<?php do { // Show News storys ?>
<td width="327" height="99"><span class="whiteheader"><?php echo $row_news['title']; ?></span> <table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr bgcolor="#FFFFFF">
<td colspan="2" nowrap><a id="newdesign"><img src="images/spacer.gif" width="1" height="1"></a></td>
</tr>
<tr>
<td width="100%" height="20" nowrap class="whitebolded">Author: <?php echo $row_news['name']; ?>
| Date: <?php echo $row_news['date']; ?> | <a href="#top" class="whitelinks">Top</a> | <a href="#bot" class="whitelinks">Bottom</a></td>
<tr bgcolor="#FFFFFF">
<td colspan="2" nowrap><a id="newdesign"><img src="images/spacer.gif" width="1" height="1"></a></td>
<tr> </tr>
</table>
<p class="smalltext">
<?php //pass the news storys through the UBB class and echo out
require_once "UBBCode.php";
$class = new UBBCode;
$text = $row_news['news'];
$encode = $class->encode($text);
echo $encode;
?>
<br>
<br>
<img src="images/plus.gif" style="cursor:pointer; cursor:hand" onClick="expandit(this)"> <span style="display:none" style=&{head};>
<br/>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<? $com = $row_news['ID']; // news item id ?>
<? // find the comments and print them....
mysql_select_db($database_eod, $eod);
$query_comments = "SELECT comments.name, comments.comment FROM comments WHERE comments.com_id = $com AND comments.page_id = $page";
$comments = mysql_query($query_comments, $eod) or die(mysql_error());
$row_comments = mysql_fetch_assoc($comments);
$totalRows_comments = mysql_num_rows($comments);
?>
<tr>
<td onClick="MM_openBrWindow('comments.php?com_id=<? echo $com; ?>&page_id=<? echo $page; ?>','comment','width=500,height=600')">
<div class="whitebolded" style="cursor:pointer; cursor:hand">Add
a comment</div><br/></td>
</tr>
<? if ($row_comments != NULL) { //if there are news items carry om ?>
<?php do { ?> <tr>
<td>Name: <font class="whitebolded"><? echo $row_comments['name']; ?></font></td>
</tr>
<tr>
<td><? //pass the comments through the UBB class and echo out
$class = new UBBCode;
$text = $row_comments['comment'];
$encode = $class->encode($text);
echo $encode; ?>
</td>
</tr>
<tr>
<td> </td>
</tr>
<?php } while ($row_comments = mysql_fetch_assoc($comments)); ?>
<? } ?>
</table>
</span> <br></p>
<br>
</p></td>
</tr>
<?php } while ($row_news = mysql_fetch_assoc($news)); ?>
</table>
<p id="bot"> </p>
</body>
</html>
<?php
mysql_free_result($news);
?>