Jump to content

Help I'm all over the place!


AdamCCFC

Recommended Posts

Right, I've had two scripts that I wanted to merger together. The problem I'm having is that when I click on comments link, all the other dreams in the database are being shown. Can anybody help me?? Here's my code, apologies for how messy it is!!

 

	   <?php
/**
Display the results from the database
**/
$q = "SELECT * FROM dream";
$r = mysql_query($q);

if(mysql_num_rows($r)>0): //table is non-empty
while($row = mysql_fetch_assoc($r)):
	$net_vote = $row['votes_up'] - $row['votes_down']; //this is the net result of voting up and voting down
?>
<div class='entry'>

<span class='link'>
	<?php echo $row['name']; ?><?php echo " dreamt "; ?><?php echo $row['dream']; ?> </a>
</span>


<span class='vote_buttons' id='vote_buttons<?php echo $row['dream_id']; ?>'>
	<a href='javascript:;' class='vote_up' id='<?php echo $row['dream_id']; ?>'>Vote Up!</a>
	<a href='javascript:;' class='vote_down' id='<?php echo $row['dream_id']; ?>'>Vote Down!</a>
</span>

	<span class='votes_count' id='votes_count<?php echo $row['dream_id']; ?>'><?php echo $net_vote." votes"; ?></span>



</div>

<?php echo $row['tags']; ?><?php echo " "; ?><?php echo $row['date_entered']; ?><?php echo "</br>"; ?>
<?php 

				/* get number of comments */
				$comment_query = "SELECT count(*) FROM dreams_comments WHERE dream_id={$row['dream_id']}";
				$comment_result = mysql_query ($comment_query);
				$comment_row = mysql_fetch_row($comment_result);
echo "<a id='commentslink' href=\"{$_SERVER['PHP_SELF']}?action=show&id={$row['dream_id']}\">Comments ($comment_row[0])</a>";



?>

<?php
endwhile;
endif;
?>
   
   
	<?php



		function displayDreams($all = 0) {
			/* bring in two variables
			 * $db is our database connection
			 * $max_items is the maximum number
			 * of news items we want to display */
			global $db, $max_items;

			//query to display the results
			$query = "SELECT dream_id,name,dream,tags FROM dream ORDER BY date_entered DESC";

			$result = mysql_query ($query);
			while ($row = mysql_fetch_assoc ($result)) {

				/* place table row data in 
				 * easier to use variables.
				 * Here we also make sure no
				 * HTML tags, other than the
				 * ones we want are displayed */
				$date = $row['date_entered'];        
				$name = htmlentities ($row['name']);
				$dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>'));
				$tags = $row['tags'];

				/* get number of comments */
				$comment_query = "SELECT count(*) FROM dreams_comments WHERE dream_id={$row['dream_id']}";
				$comment_result = mysql_query ($comment_query);
				$comment_row = mysql_fetch_row($comment_result);

				/* display number of comments with link */
			}
		}

		function displayOneItem($id) {
			global $db;

			/* query for item */
			$query = "SELECT * FROM `dream` WHERE `dream_id` = '$id'";
			$result = mysql_query ($query);

			/* if we get no results back, error out */
			if (mysql_num_rows ($result) == 0) {
				echo "Bad Dream ID\n";
				return;
			}
			$row = mysql_fetch_assoc($result);

			/* easier to read variables and 
			 * striping out tags */
			$name = htmlentities ($row['name']);
			$dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>'));
			$tags = ($row['tags']);

			/* display the items */
			echo "<h1>" . $row['name'] . "</h1>";
			echo 'dreamt';
			echo "<p class='dream'>" . $row['dream'] . "</p>";				  
			echo "<p class='tags'>" . $row['tags'] . "</p>";
			echo "<p class='date'>" . $row['date_entered'] . "</p>";
			echo "</br>";

			/* now show the comments */
			displayComments($id);
		}

		function displayComments($id) {
			/* bring db connection variable into scope */
			global $db;

			/* query for comments */
			$query = "SELECT * FROM dreams_comments WHERE dream_id=$id";
			$result = mysql_query ($query);
			echo "Comments:</br>";

			/* display the all the comments */
			while ($row = mysql_fetch_assoc ($result)) {

				$name = htmlentities ($row['name']);
				echo "<p class='name'>$name</p>";

				$comment = strip_tags ($row['comment'], '<a><b><i><u>');
				$comment = nl2br ($comment);
				echo "<p class='user_comment'>$comment</p>";

			echo "</br>";
			}

			/* add a form where users can enter new comments */
			echo "Leave a comment!";
			echo "</br>";
			echo "<form id='addcomment' action=\"" . $_SERVER['PHP_SELF'] . "?action=addcomment&id=$id\" method='POST'>";

			echo "<table border='0' width='300'";
			echo "<tr><td>Name:</td><td><input type'text' width'30' name='name'></td></tr>";
			echo "<tr><td>Comment:</td><td><textarea cols='30' rows='10' name='comment'></textarea></td></tr>";
			echo "<tr><td><input type='submit' name='submit' value='Add Comment'</td></tr>";
			echo "</form>";
			echo "</table>";
		}

		function addComment($id) {
			global $db;
			/* insert the comment */
			$query = "INSERT INTO dreams_comments VALUES('','$id','$_POST[name]','$_POST[comment]')";
			mysql_query($query);

			echo "Comment entered. Thanks!</br>";
			echo "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\"> Back</a>";
		}

		/* this is where the script decides what do do */

		switch($_GET['action']) {

			case 'show':
				displayOneItem($_GET['id']);
				break;
			case 'all':
				displayDreams(1);
				break;
			case 'addcomment':
				addComment($_GET['id']);
				break;
			default:
				displayDreams();
		}
	?>

Link to comment
https://forums.phpfreaks.com/topic/195018-help-im-all-over-the-place/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.