Old 10-11-2004, 12:43   #1 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
Displaying Comments for Articles

Ok, moving on, I am progressing well but have seemed to have come unstuck again.

If I want to display the comments for a particular article how do I do this, I know its something to do with matching comment_ID with say article_ID.

I have a table for comments and a table for articles.

I then want to view the article, and click on view comments and go to a page with comments just for that article.

Thanks in advance,
  Reply With Quote
Old 10-11-2004, 12:50   #2 (permalink)
Joewebber
Senior Member
 
Joewebber's Avatar
 
Join Date: Feb 2004
Location: Exeter
Posts: 773
Post the id number of the article in the url.
ie
$query = Select * from articles;
$result = ....

....
echo "<a href=http://www.mysite.com/view_comments.php?id=$row['id']>";

Then in the view_comments page, get the id from the url:

$id = $_GET['id']

$query = "select from comments where article_id = $id"

But you need a column in the comments table that identifies the comment with its related article
  Reply With Quote
Old 10-11-2004, 12:54   #3 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
ok. So do I need a hidden field that contains the id???

This is the code for my view comments button

viewComments.php?article_id=<?php echo $row_rsArticles['article_id']; ?>
  Reply With Quote
Old 10-11-2004, 12:56   #4 (permalink)
Joewebber
Senior Member
 
Joewebber's Avatar
 
Join Date: Feb 2004
Location: Exeter
Posts: 773
You need to store the id of the article that the comment relates to in the table
  Reply With Quote
Old 10-11-2004, 12:57   #5 (permalink)
Joewebber
Senior Member
 
Joewebber's Avatar
 
Join Date: Feb 2004
Location: Exeter
Posts: 773
This might help:

http://codegrrl.com/tutorials/cat_build_a_blog.php

Its quite hard to explain without seeing all your code
  Reply With Quote
Old 10-11-2004, 13:07   #6 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
ok,

I have added article_id to my comments table in MySQL.

viewComments.php

Code:
<?php require_once('Connections/connNickToye.php'); ?> <?php require_once('Connections/connNickToye.php'); ?> <?php mysql_select_db($database_connNickToye, $connNickToye); $query_rsAddComment = "SELECT * FROM comments"; $rsAddComment = mysql_query($query_rsAddComment, $connNickToye) or die(mysql_error()); $row_rsAddComment = mysql_fetch_assoc($rsAddComment); $totalRows_rsAddComment = mysql_num_rows($rsAddComment); $colname_rsArticles = "1"; if (isset($_GET['article_id'])) { $colname_rsArticles = (get_magic_quotes_gpc()) ? $_GET['article_id'] : addslashes($_GET['article_id']); } mysql_select_db($database_connNickToye, $connNickToye); $query_rsArticles = sprintf("SELECT * FROM articles WHERE article_id = %s", $colname_rsArticles); $rsArticles = mysql_query($query_rsArticles, $connNickToye) or die(mysql_error()); $row_rsArticles = mysql_fetch_assoc($rsArticles); $totalRows_rsArticles = mysql_num_rows($rsArticles); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/main.dwt.php" codeOutsideHTMLIsLocked="false" --> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <!-- InstanceBeginEditable name="doctitle" --> <title>Untitled Document</title> <!-- InstanceEndEditable --><link href="css/myStyle.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> <!-- function getWindowHeight() { var windowHeight = 0; if (typeof(window.innerHeight) == 'number') { windowHeight = window.innerHeight; } else { if (document.documentElement && document.documentElement.clientHeight) { windowHeight = document.documentElement.clientHeight; } else { if (document.body && document.body.clientHeight) { windowHeight = document.body.clientHeight; } } } return windowHeight; } function setFooter() { if (document.getElementById) { var windowHeight = getWindowHeight(); if (windowHeight > 0) { var contentHeight = document.getElementById('content').offsetHeight; var footerElement = document.getElementById('footer'); var footerHeight = footerElement.offsetHeight; if (windowHeight - (contentHeight + footerHeight) >= 0) { footerElement.style.position = 'relative'; footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px'; } else { footerElement.style.position = 'static'; } } } } window.onload = function() { setFooter(); } window.onresize = function() { setFooter(); } //--> </script> </head> <body> <div id="container"> <div id="header"></div> <!-- InstanceBeginEditable name="nav" --> <div id="nav"> <li><a href="home.php">HOME </a></li> <li><a href="about.php">ABOUT</a></li> <li><a href="blog.php">BLOG</a></li> <li><a href="work.php">WORK</a></li> <li><a href="contact.php">CONTRACT ME</a></li> <li><a href="admin/adminIndex.php">ADMIN</a></li> </div> <!-- InstanceEndEditable --> <div id="content"><!-- InstanceBeginEditable name="main" --> <?php if ($totalRows_rsAddComment > 0) { // Show if recordset not empty ?> <p><?php echo $row_rsArticles['title']; ?> - <?php echo $row_rsArticles['date']; ?><br /> <br /> <?php do { ?> <?php echo $row_rsAddComment['comment']; ?><br /> <?php echo $row_rsAddComment['author']; ?> - <?php echo $row_rsAddComment['date']; ?></MM:DECORATION></MM_REPEATEDREGION></p> <form name="form1" id="form1" method="post" action=""> <input name="hiddenField" type="hidden" value="<?php echo $row_rsArticles['article_id']; ?>" /> </form> <p><MM_REPEATEDREGION SOURCE=" "><MM:DECORATION OUTLINE="Repeat" OUTLINEID=2><br /> </MM:DECORATION></MM_REPEATEDREGION></p> <hr size="1" /> <p><MM_REPEATEDREGION SOURCE=" "><MM:DECORATION OUTLINE="Repeat" OUTLINEID=1><br /> <?php } while ($row_rsAddComment = mysql_fetch_assoc($rsAddComment)); ?> </p> <?php } // Show if recordset not empty ?> <!-- InstanceEndEditable --></div> <div id="sidebar"> <!-- InstanceBeginEditable name="sidebar" -->Recent Entries <ul> <li>Link Item 1</li> <li>Link Item 2</li> <li>Link Item 3</li> <li>Link Item 4</li> </ul> <!-- InstanceEndEditable --></div> <div id="footer"> </div> </div> </body><!-- InstanceEnd --></html> <?php mysql_free_result($rsAddComment); mysql_free_result($rsArticles); ?>

blog.php

Code:
<?php require_once('Connections/connNickToye.php'); ?> <?php $maxRows_rsArticles = 10; $pageNum_rsArticles = 0; if (isset($_GET['pageNum_rsArticles'])) { $pageNum_rsArticles = $_GET['pageNum_rsArticles']; } $startRow_rsArticles = $pageNum_rsArticles * $maxRows_rsArticles; mysql_select_db($database_connNickToye, $connNickToye); $query_rsArticles = "SELECT date_format(date,' %D %M %Y') as date, article_id, title, text FROM articles ORDER BY `date` DESC"; $query_limit_rsArticles = sprintf("%s LIMIT %d, %d", $query_rsArticles, $startRow_rsArticles, $maxRows_rsArticles); $rsArticles = mysql_query($query_limit_rsArticles, $connNickToye) or die(mysql_error()); $row_rsArticles = mysql_fetch_assoc($rsArticles); if (isset($_GET['totalRows_rsArticles'])) { $totalRows_rsArticles = $_GET['totalRows_rsArticles']; } else { $all_rsArticles = mysql_query($query_rsArticles); $totalRows_rsArticles = mysql_num_rows($all_rsArticles); } $totalPages_rsArticles = ceil($totalRows_rsArticles/$maxRows_rsArticles)-1; mysql_select_db($database_connNickToye, $connNickToye); $query_rsComments = "SELECT * FROM comments"; $rsComments = mysql_query($query_rsComments, $connNickToye) or die(mysql_error()); $row_rsComments = mysql_fetch_assoc($rsComments); $totalRows_rsComments = mysql_num_rows($rsComments); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/main.dwt.php" codeOutsideHTMLIsLocked="false" --> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <!-- InstanceBeginEditable name="doctitle" --> <title>NickToye 2005: Blog</title> <!-- InstanceEndEditable --><link href="css/myStyle.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> <!-- function getWindowHeight() { var windowHeight = 0; if (typeof(window.innerHeight) == 'number') { windowHeight = window.innerHeight; } else { if (document.documentElement && document.documentElement.clientHeight) { windowHeight = document.documentElement.clientHeight; } else { if (document.body && document.body.clientHeight) { windowHeight = document.body.clientHeight; } } } return windowHeight; } function setFooter() { if (document.getElementById) { var windowHeight = getWindowHeight(); if (windowHeight > 0) { var contentHeight = document.getElementById('content').offsetHeight; var footerElement = document.getElementById('footer'); var footerHeight = footerElement.offsetHeight; if (windowHeight - (contentHeight + footerHeight) >= 0) { footerElement.style.position = 'relative'; footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px'; } else { footerElement.style.position = 'static'; } } } } window.onload = function() { setFooter(); } window.onresize = function() { setFooter(); } //--> </script> </head> <body> <div id="container"> <div id="header"></div> <!-- InstanceBeginEditable name="nav" --> <div id="nav"> <li><a href="home.php">HOME </a></li> <li><a href="about.php">ABOUT</a></li> <li><a href="blog.php" class="active">BLOG</a></li> <li><a href="work.php">WORK</a></li> <li><a href="contact.php">CONTRACT ME</a></li> <li><a href="admin/adminIndex.php">ADMIN</a></li> </div> <!-- InstanceEndEditable --> <div id="content"><!-- InstanceBeginEditable name="main" --> <?php do { ?> <p><span class="title"><?php echo $row_rsArticles['title']; ?> -</span> <span class="dateline"><?php echo $row_rsArticles['date']; ?></span></p> <p><?php echo $row_rsArticles['text']; ?></p> <p><br /> <a href="addComment.php">Add Comment </a><br /> <a href="viewComments.php?article_id=<?php echo $row_rsComments['article_id']; ?>">View Comments </a> </p> <hr size="1" /> <p>&nbsp;</p> <?php } while ($row_rsArticles = mysql_fetch_assoc($rsArticles)); ?> <!-- InstanceEndEditable --></div> <div id="sidebar"> <!-- InstanceBeginEditable name="sidebar" --><span class="sidebarHeader">Blog Topics </span> <ul> <li>Topic 1</li> <Li>Topic 2</Li> <li>Topic 3</li> <li>Topic 4</li> </ul> <p class="sidebarHeader">Archive</p> <p class="sidebarHeader"> Inspiration</p> <p>&nbsp;</p> <!-- InstanceEndEditable --></div> <div id="footer"> </div> </div> </body> <!-- InstanceEnd --></html> <?php mysql_free_result($rsArticles); mysql_free_result($rsComments); ?>

I hope this is more clear, I had a look at that tutorial and its hurting my brain, I think I need to get a coffee and chill for a minute.

It seems to be mre difficult then it should be.
  Reply With Quote
Old 10-11-2004, 14:20   #7 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
Do I have to pass any parameters when I insert record from the addComments.php
  Reply With Quote
Old 10-11-2004, 14:45   #8 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
I have just been checking through stuff, and when I look at the phpMyAdmin panel. I look at comments table and my column for article_id is always 0, the default is 0 but obviously I want it to pass the article_id from the articles page. Does that help solve where the problem is??
  Reply With Quote
Old 10-11-2004, 16:49   #9 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
Hey Joewebber, fixed the problem, I was just all confused with passing the right parameters and then in the record set I didn't set the article_id to = the url parameter of artice_id. So cool, did a little debugging and sorted it.

While i'm on though there are a couple of other things that are stumping me, this is also for anyone who knows. But when I put in linespaces when I am entering data, it doesn't seem to show up in the dynamic table, that is it is showing continuous text with no line spaces between paragraphs.

And also I want to show how many comments there are for an article, now when I use the built in function in DMX2004 it shows up all the comments and I only want the comments that are relative to that article.

Cheers

Nick
  Reply With Quote
Old 10-11-2004, 17:44   #10 (permalink)
Joewebber
Senior Member
 
Joewebber's Avatar
 
Join Date: Feb 2004
Location: Exeter
Posts: 773
Quote:
Originally Posted by NickToye
But when I put in linespaces when I am entering data, it doesn't seem to show up in the dynamic table, that is it is showing continuous text with no line spaces between paragraphs.

It won't put them in. What you're entering is basically html code

Quote:
And also I want to show how many comments there are for an article, now when I use the built in function in DMX2004 it shows up all the comments and I only want the comments that are relative to that article.

Just write your query statements so it selects the comments for a given article, then use mysql_count_rows. (Don't know if thats the right syntax coz I'm a bit pissed)
  Reply With Quote
Old 10-11-2004, 17:54   #11 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
Quote:
Originally Posted by Joewebber
It won't put them in. What you're entering is basically html code
So I will sort this through CSS then I presume.

Quote:
Originally Posted by Joewebber
Just write your query statements so it selects the comments for a given article, then use mysql_count_rows. (Don't know if thats the right syntax coz I'm a bit pissed)
You will have to verify that for me when your not so pissed. But hey have a good night.
  Reply With Quote
Old 11-11-2004, 06:15   #12 (permalink)
dan
Iris Folder
 
dan's Avatar
 
Join Date: Apr 2003
Location: smokey
Posts: 2,650
If your entering line breaks in a form the text is entered as a plain text format ie uses \n for line breaks. For these to show up in your html table you will have to replace them with html tags (<br/>) using a php switchermajig. Smallbeers posted a good one a couple of times...

mySql Advice
  Reply With Quote
Old 11-11-2004, 06:23   #13 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
Hey thats fantastic. But a quick question, how does it work, usually when I use a function I try and understand what is going on justso I can learn from it.
  Reply With Quote
Old 11-11-2004, 06:37   #14 (permalink)
dan
Iris Folder
 
dan's Avatar
 
Join Date: Apr 2003
Location: smokey
Posts: 2,650
Searches for a regular expression in $text in this case "\n" and replaces it with the html code it needs "<br/>"

$text = ereg_replace("\n",'<br />',$text);

Does this for various elements to create the correct html markup. Returns the answer.
  Reply With Quote
Old 11-11-2004, 06:55   #15 (permalink)
NickToye
Senior Member
 
Join Date: Oct 2004
Location: Chester
Posts: 364
ok so ereg_replace is a search and replace command,

It looks for "\n", - line breaks
Replaces them with '<br />' - html formatted line breaks
$text - returns the new variable.
  Reply With Quote
Old 11-11-2004, 10:29   #16 (permalink)
dan
Iris Folder
 
dan's Avatar
 
Join Date: Apr 2003
Location: smokey
Posts: 2,650
yup, pretty much. Far better way of putting it.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8