Jump to content

news script help


L

Recommended Posts

Hey everybody,

I am working on a news script where when I submit the news, subject, and dude who wrote it it will appear on the index. and Then people can post comments for each article when logged in. Everthing seems fine except two problems. One is that I want to keep count of the total comments for each article, but i can\t seem to get the query to work

 

the second problem is that when i submit news, an older article shows up...but then i have to submit another article for the article that i originally wanted to show up to show up. How can i fix this so the article i sbmit is the one that will appear immediatly on my index?

Here is the code for my submit page, my index, and my comments page.

 

submitnews.php

<?PHP
include("database.php");
?>
<form id='1' name='1' method='post'>
<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
      <td><div align="right">Alias:   </div></td>
       <td><input name="user" type="text" size="30" value=""></td>
   </tr>
   <tr>
      <td><div align="right">Subject:   </div></td>
       <td><input name="subject" type="text" size="30" value=""></td>
   </tr>
      <td><div align="right">News:   </div></td>
      <td><textarea name="news" cols="45" rows="5" wrap="VIRTUAL"></textarea></td>
    </tr>
    <tr>
      <td></td>
      <td colspan="2">     
        <input type="submit" name="submit" value="Submit News"></td>
    </tr>
  </table>
</form>

<?PHP
$user = addslashes(strip_tags($_POST['user']));
$subject = addslashes(strip_tags($_POST['subject']));
$news = addslashes(strip_tags($_POST['news'],'<a>'));
$date = date("F j, Y | h:i A");
if(isset($_POST['submit']))
{
    mysql_query("INSERT INTO `news` (`user`, `subject`, `news`, `date`) VALUES ('".$user."','".$subject."','".$news."',now())");
    echo "News Posted";
}
  
?>

 

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>Infinite Vortex</title>
<meta http-equiv="content-type" content="text/html; charset=us-ascii" />
<link rel="stylesheet" type="text/css" href="/newmain.css" />
<link rel="stylesheet" type="text/css" href="/newupdate.css" />
<script type="text/javascript" src="/extternal.js">
</script>
<script type="text/javascript" src="/updatepic.js">
</script>
<link rel="alternate" title="Infinite Vortex News Feed" href="/rss.xml" type="application/rss+xml" />
</head>


				<!-- Start Web -->
<body>
<div class="border">
					<!-- START Banner -->

				<!-- END Banner -->
<?php 
include("menu.php");
include("database.php");
$newsidm = mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 0,17") or die(mysql_error());
$news = mysql_fetch_array($newsidm);
?>
<div class="left">
<?PHP
while ($news = mysql_fetch_array($newsidm)) {
echo "<p class=\"breakoff\"><b>".stripslashes($news['subject'])."</b><span class=\"comments\">| "."<a href=\"show.php?id=".$news['id']."\">"."Comments</a> |</span><br />By ".stripslashes($news['user'])." on ".$news['date']."<br /></p>".stripslashes($news['news'])."<br /><br />";
}

?>



		</div>
	</div>
</div>
	<?php include("footer1.php"); ?>

 

show.php

(showing comments with article)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>Infinite Vortex</title>
<meta http-equiv="content-type" content="text/html; charset=us-ascii" />
<link rel="stylesheet" type="text/css" href="/newmain.css" />
<link rel="stylesheet" type="text/css" href="/newupdate.css" />
<script type="text/javascript" src="/extternal.js">
</script>
<script type="text/javascript" src="/updatepic.js">
</script>
<link rel="alternate" title="Infinite Vortex News Feed" href="/rss.xml" type="application/rss+xml" />
<meta name="keywords" content="naruto, bleach, pictures, Rukia, anime, pics, sasuke, cool, free, download, sakura, kakashi, manga,  episode, gallery, episodes, information, downloads, Dragon, DragonBall, Z, GT, Naruto, Sasuke, Sakura, Rasengan, Itachi, anime, cool, goku, vegeta, gohan, IGPX, Takashi, Amy, Luca, Maya, Natsume, Tenjo Tenge, Yu yu Hakisho, Yuske, Kurama, spirit energy, chakra, ki, power, japanese, animation, deathgod, chi, toonami, Ninja, cartoonnetwork db, rasengan, dattebayo, naruto manga 347, naruto chapter 347, naruto episode, bleach episode, manga chapter 267, Arrancar, donation, games, Sharingon, Gaara of the Desert, Sand, Villages, Konoha, fighting, fun, computers, flash, school, tenjo tenge, avatar, airbending, waterbending, firebending, earthbending, koda kumi, songs, ultimate site" />

</head>


				<!-- Start Web -->
<body>
<div class="border">
					<!-- START Banner -->

				<!-- END Banner -->
<?php include("menu.php"); ?>
<div class="left">
<!-- MAIN -->
<?PHP
include("database.php");
session_start();
$userID = $_SESSION['userid'];
$query = mysql_query("SELECT * FROM users WHERE userid='$userID'")or die(mysql_error());
$row = mysql_fetch_assoc($query);

// Get News ID
$id = $_GET['id']; 
// Get the Actual News that matches the News ID
$news = mysql_fetch_array(mysql_query("SELECT * FROM `news` WHERE `id`=$id ")) or die(mysql_error());
// Get Comments for that specicif News ID
$comid = mysql_query("SELECT * FROM `comments` WHERE `newsid`='".$news['id']."' ORDER BY `id` DESC LIMIT 0,50") or die(mysql_error());
// Display News Artical
echo "<p class=\"breakoff\"><b>".$news['subject']."</b><br />By ".$news['user']." on ".$news['date']."<br /></p>".stripslashes($news['news'])."<br />";
?>

<?

// Set up an IP address that has access to delete comments
$ip = $_SERVER['REMOTE_ADDR'];
$okip = "24.15.10.41";
while ($view = mysql_fetch_array($comid)) {
// Display all comments for artical, with Delete Comment Option if your IP matches the OK IP
if ($ip == $okip) {
echo "<br /><br /><p class=\"breakoff\">By <a href=\"cp.php?user=".stripslashes($view['user'])."\">".stripslashes($view['user'])."</a> on ".$view['date']."</p><a href=\"show.php?del=".$view['id']."\">[X]</a><br />
<p class=\"news\">".stripslashes($view['comment'])."</p>";
}


// If it doesn't match it, display comments without delete option
else {
echo "<br /><br /><p class=\"breakoff\">By <a href=\"mailto:".stripslashes($view['contact'])."\">".stripslashes($view['user'])."</a> | ".$view['date']."</p><p class=\"news\">".stripslashes($view['comment'])."</p>";
}
}
?>

<?PHP
// This stuff is needed to delete the comment from the database
$comment = $_GET['del'];
if ($comment != NULL && is_numeric($comment)) {
  if ($ip == $okip) {
mysql_query("DELETE FROM `comments` WHERE `id` = '".$comment."' LIMIT 1 ");
echo "Comment has been deleted.<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"2; URL=show.php\"> ";
}
else {
echo "<br /><br />You do not have permission to delete comments."; }
}

$user = addslashes(strip_tags($_POST['username']));
$contact = addslashes(strip_tags($_POST['contact']));
$date = date("F j, Y | h:i A");
$comment = addslashes(strip_tags($_POST['comment']));
$newsid = $_GET['id'];
$check1 = @mysql_num_rows(mysql_query("SELECT * FROM `comments` WHERE `comment`='".$comment."' "));

if ($_POST['submit']) {
  if((empty($_SESSION['username'])) || (empty($_SESSION['userid']))) {
  echo "<b><br /><br />Please login to post a comment.</b>";
  }
  else
  {
  if ($user != $row['username']) {
echo "<b><br /><br />That alias is being used by a member, please Login if you are that member</b>"; 
}
else {
  if ($check1 >= 1) { // Check to see if comment already exists
  echo "<b><br /><br />Comment has already been posted!</b>"; }
  elseif ($contact == NULL || $comment == NULL || $user == NULL) {
   echo "<b><br /><br />Please fill in all fields.</b>"; }
   else {
   mysql_query("INSERT INTO `comments` (`user`, `contact`,`comment`,`date`, `newsid`) VALUES ('".$user."','".$contact."','".$comment."','".$date."','".$newsid."')");
    mysql_query("UPDATE `news` SET `comments`=comments + 1 WHERE `newsid`='$newsid'");
   echo "<b><br /><br />Comment has been posted! </b><META HTTP-EQUIV=\"REFRESH\" CONTENT=\"2; URL=/show.php?id=$id\">"; }
   }
   }
   }
?>

<br />
<br />

<form id='1' name='1' method='post'>
<table width="90%" border="0" cellspacing="0" cellpadding="0">
   <tr>
      <td><div align="right">Alias:   </div></td>
       <td><input name="username" type="text" size="30" value=""></td>
   </tr>
    <tr>
      <td><div align="right">Contact:   </div></td>
      <td><input type="text" name="contact" size="30" value=""> <i>(Email Address)</i></td>
    </tr>
    <tr>
      <td><div align="right">Comment:   </div></td>
      <td><textarea name="comment" cols="45" rows="5" wrap="VIRTUAL"></textarea></td>
    </tr>
    <tr>
      <td></td>
      <td colspan="2">     
        <input style="font-size: 8pt; color: #000000; word-spacing: 0; margin-top: 0; margin-bottom: 0" type="submit" name="submit" value="Add Comment"></td>
    </tr>
  </table>
</form>
<?PHP

   ?>

	</div>
	</div>

	<?php include("footer1.php"); ?>

 

Thank you for your time

~L

Link to comment
Share on other sites

I wouldn't help you write it, try it out yourself.

1. Use COUNT() on the query and includes this at the end WHERE newsid='X' where X refers to the newsid.

 

2. Remove the code at line 25, since you already wrote it in line 29.

Change your query to this

SELECT * FROM `news` ORDER BY `id` DESC LIMIT 17

Take note: 0,17 means it take the records starts from 0 to 17.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.