PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Mon Aug 18, 2003 1:24 am
i'm making my home page, which has the news. and i want to add a comments system. i know how to write a comments system and a news system but having a hard time merging them togeather.
like the news are posted from my control panel i wrote. it inserts TITLE and NEWS into my sql db. then in the index.php, it reads from the db.
now i want to add the No. of comments on the news. how would i do it? here what's i'm guessing. i've not tried it yet though:
Code: Select all
<?
$connection = mysql_connect("$hostname" , "$user" , "$pass");
$db = mysql_select_db($dbase , $connection);
$q="SELECT * from news order by id desc limit 5";
$result= mysql_query($q, $connection) or die
("Could not execute query : $q." . mysql_error());
while ($row=mysql_fetch_array($result)) {
$title=$row["title"];
$news=$row["news"];
$q="SELECT * from comments where commentfrom=$title";
$result= mysql_query($q, $connection) or die
("Could not execute query : $q." . mysql_error());
$noOfComments = mysql_num_rows($result);
echo "$title<br>";
echo "$news<br>";
echo :Comments: $noOfComments";
}
?>
thanks...
-Nay
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Mon Aug 18, 2003 12:28 pm
okay, i've got a 50% working script:
Code: Select all
<?php
$hostname="host";
$user="me";
$pass="mypw";
$dbase="mydb";
$connection = mysql_connect("$hostname" , "$user" , "$pass");
$db = mysql_select_db($dbase , $connection);
$q="SELECT * from news order by id desc limit 7";
$result= mysql_query($q, $connection) or die
("Could not execute query : $q." . mysql_error());
while($row=mysql_fetch_array($result)) {
function comments($newsId) {
$hostname="host";
$user="me";
$pass="mypw";
$dbase="mydb";
$connection = mysql_connect("$hostname" , "$user" , "$pass");
$db = mysql_select_db($dbase , $connection);
$q="SELECT * from comments where news_id = $newsId";
$result= mysql_query($q, $connection) or die
("Could not execute query : $q." . mysql_error());
$nrc = mysql_num_rows($result);
echo "$nrc"; }
function addLinks($string) {
$string = preg_replace("/(?<!quot;|[="]|:\/\/)\b((\w+:\/\/|www\.).+?)".
"(?=\W*([<>\s]|$))/i", "<a href="$1" target="_blank">$1</a>", $string);
return preg_replace("/href="www/i", "href="http://www", $string); }
$news_id=$row['id'];
$title=$row['title'];
$time=$row['time'];
$author=$row['author'];
$news=$row['news'];
$news=str_replace("\n", "<br />", $news);
$news=str_replace("<a", "<a target="blank" ", $news);
$news=addLinks($news);
echo "<p>";
echo "$news<br><br>";
echo "Posted by $author, at $time";
echo "</p>";
echo "<a href="comments.php?id=$news_id">";
echo comments($news_id);
echo " Comments</a>";
}
?>
but when there's more than 1 news in the database, i get:
Code: Select all
Fatal error: Cannot redeclare comments() (previously declared in /home/virtual/site21/fst/var/www/html/index.php:18) in /home/virtual/site21/fst/var/www/html/index.php on line 18
please help!
-Nay
Dacicle
Forum Newbie
Posts: 1 Joined: Mon Aug 18, 2003 12:34 pm
Contact:
Post
by Dacicle » Mon Aug 18, 2003 12:34 pm
youre defining the function inside of a loop, so i believe its trying to define it over and over again
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Mon Aug 18, 2003 12:38 pm
omg!........thanks so much!......
stupid me
-Nay