Log in

View Full Version : Comment Script Issues



Titan85
03-01-2007, 03:19 AM
Hello, I am trying to make a script that will allow a user to comment once on a job done for them. I got it working fine to only allow them to leave one comment, but then I realized that on user may have multiple jobs to comment and that I need them to be able to comment once on each job. I have a page that shows is supposed to show the jobs available for commenting, but when I run the page, I get nothing. Here is the code:
<?php

// Get username
$user = $_SESSION['user'];

// Query to get job data
$sql = "SELECT * FROM `jobs` WHERE username = '$user' ORDER BY id DESC";
$result = mysql_query($sql) or die ('Error Getting Job Data! <br />' .mysql_error());
$row = mysql_fetch_array($result);
// Get number of results
$results = mysql_num_rows($result);

// If there are no results
if($results < 1) {
echo '
There are no jobs availible for you to comment. Contact me <a href="http://echo-designes.com/contact.php">here</a> to have a job done.
<br />
';
}

// Find out whether to display "are" or "is" and "jobs" or "job"
if($results = 1) {
$are = "is";
$job = "job";
}
else {
$are = "are";
$job = "jobs";
}

// If there are results and form is not active
if($results >= 1 && !$_GET['page']) {

// Tell how many jobs can be commented
echo '
There '.$are.' <i>'.$results.'</i> '.$job.' that you may comment.
<br /><br />
';

echo 'Chose the job you would like to comment.
<br /><br />';

// get job data
$sql = "SELECT * FROM `jobs` WHERE username = '$user' ORDER BY id DESC";
$jobs = mysql_query($sql) or die ('Error Getting Job Data! <br />' .mysql_error());
$j = mysql_fetch_array($jobs);

$title = $j['title'];
$comments = mysql_query("SELECT * FROM `comments` WHERE job = '$title'") or die ('Error Checking Comments! <br />' .mysql_error());
$c = mysql_fetch_array($comments);


// Display jobs
while($job = mysql_fetch_array($jobs)) {

if($job['title'] !== $c['title']) {
echo '
<a href="comment.php?page=add&id='.$job['id'].'">'.$job['title'].'</a>
<br />';
}
}

}


// If add was hit
if($_GET['page'] == "add") {
require('add.php');
}

?>The pages reads that there is indeed a job to comment, but does not display the link to comment it (in the while statement). Thanks

codeexploiter
03-01-2007, 03:25 AM
if($results = 1) {
$are = "is";
$job = "job";
}
else {
$are = "are";
$job = "jobs";
}


One logical error in the above condition that must be == i think.

boxxertrumps
03-01-2007, 02:19 PM
you are correct, = assigns values, == is equal, and === is exact match.

alexjewell
03-01-2007, 03:46 PM
I saw the same thing. Those equal signs get me all the time. Haha.

Titan85
03-01-2007, 09:58 PM
Thanks for pointing out the incorrect "=" usage, I appreciate it. However, the main issue is still unresolved because the page still does not display links to comment on jobs that currently have not been commented. Any ideas?

thetestingsite
03-02-2007, 03:11 AM
Not 100% sure on this, but wouldn't this have something to do with it:



// get job data
$sql = "SELECT * FROM `jobs` WHERE username = '$user' ORDER BY id DESC";
$jobs = mysql_query($sql) or die ('Error Getting Job Data! <br />' .mysql_error());
$j = mysql_fetch_array($jobs);

$title = $j['title'];
$comments = mysql_query("SELECT * FROM `comments` WHERE job = '$title'") or die ('Error Checking Comments! <br />' .mysql_error());
$c = mysql_fetch_array($comments);


// Display jobs
while($job = mysql_fetch_array($jobs)) {

if($job['title'] !== $c['title']) {
echo '
<a href="comment.php?page=add&id='.$job['id'].'">'.$job['title'].'</a>
<br />';
}
}


It seems to me that you are selecting jobs that are already commented on. That is why you are not getting any links to show up. Try removing the part in red in the above snippet.

Hope this helps.

Added Later:
---------------------------------------

After looking at the original script a little more, if what I posted above doesn't work, try undoing that and removing the following part in red.




// get job data
$sql = "SELECT * FROM `jobs` WHERE username = '$user' ORDER BY id DESC";
$jobs = mysql_query($sql) or die ('Error Getting Job Data! <br />' .mysql_error());
$j = mysql_fetch_array($jobs);

$title = $j['title'];
$comments = mysql_query("SELECT * FROM `comments` WHERE job = '$title'") or die ('Error Checking Comments! <br />' .mysql_error());
$c = mysql_fetch_array($comments);


// Display jobs
while($job = mysql_fetch_array($jobs)) {

if($job['title'] !== $c['title']) {
echo '
<a href="comment.php?page=add&id='.$job['id'].'">'.$job['title'].'</a>
<br />';
}
}

Titan85
03-04-2007, 01:26 AM
Thanks for the help guys. I decided to redo it another way that I think will work better for what I want to do. So far I think its working fine :)