Jump to content

Limiting comments part two


RalphLeMouf

Recommended Posts

Hey there -

 

I have been trying to limit the number of comments a user can make per day on my social network. Thanks to great help on here I am getting really close, however, there are some bugs that have me banging my head against the wall. I am able to limit, but now, its

 

1. not limiting PER day and limiting all around ( meaning: I can't make any comments at all today )

 

2. it is limiting for EVERY user as opposed to limiting a specific user

 

Here is the code I have:

 

if(isset($_POST['commentProfileSubmit'])) {


if($_POST['ProfileComment'] == "" || $_POST['ProfileComment'] == "Tell the community what's on your mind...") {
$valid = false;
$error_msgs_comments[] = "Whoops! You forgot to write your airwave.";
}else{
if($_POST['ProfileComment'] == "" || $_POST['ProfileComment'] == "Leave ".$prof->first_name." a comment here...") {
$valid = false;
$error_msgs_comments[] = "Whoops! You forgot to write your comment.";
}else{

/* if the person signed in is NOT the profile */

	$query = "SELECT * FROM `cysticUsers` WHERE `id` = '" . $prof->id . "'";
	$request = mysql_query($query,$connection) or die(mysql_error());
	$result = mysql_fetch_array($request); 
	$max_post_per_day = 5;

	$Email = $result['Email'];
	$check_profi = $result['check_profi'];
	$check_reply = $result['check_reply'];

	if($prof->id != $auth->id && $check_profi == 'checked' && $max_post_per_day < 5) {

	$to = $Email;
	$subject = "$auth->first_name $auth->last_name commented on your profile on CysticLife";
	$message = "$auth->first_name $auth->last_name commented on your profile on CysticLife: <br /><br />\"$body\"<br /><br /> <a href='http://www.cysticlife.org/Profile_build.php?id=" . $prof->id . "'>Click here to view</a><br /><br />Do LIFE,<br /> The CysticLife Team";
	$from = "CysticLife <noreply@cysticlife.org>";
	$headers  = 'MIME-Version: 1.0' . "\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
	$headers .= "From: $from";
	mail($to, $subject, $message, $headers);
}

$query = "SELECT COUNT(*) FROM `CysticAirwaves`
          WHERE `FromUserID` = $auth->id
            AND `date` = CURDATE()";
$result = mysql_query($query, $connection);
$post_count = mysql_result($result, 0);


if($post_count >= $max_posts_per_day)
{
    echo "You have reached the maximum number of posts for the day. Try again tomorrow";
}
else
{


  $comment = mysql_real_escape_string($_POST['ProfileComment']);
        $query = "INSERT INTO `CysticAirwaves`
                      (`FromUserID`, `ToUserID`, `comment`, `status`, `statusCommentAirwave`, `date`, `time`)
                  VALUES
                      ('{$auth->id}', '{$prof->id}', '{$comment}', 'active', 'active', CURDATE(), CURTIME())";
        mysql_query($query, $connection) or die(mysql_error()); 
    }

if($auth->id == $prof->id) {
	$just_inserted = mysql_insert_id();
	$query = "UPDATE `CysticAirwaves` SET `status` = 'dead' WHERE `FromUserID` = '" . $auth->id . "' AND `ToUserID` = '" . $prof->id . "' AND `id` != '" . $just_inserted . "'";
	$request = mysql_query($query,$connection);
}
}
}

}							

 

 

thanks so much in advanced

Link to comment
Share on other sites

Rough concept

 

Concept  (comments allowed based upon a new day is considered midnight GMT):

total_comments table

id, user_id

 

when a user makes a comment, check the total_comments table by COUNTING records where user_id = the user's id.

 

if count is >2, deny post, else add a record to the table and process the comment as normal.

 

cron job to run at midnite GMT - truncate the total_comments table

 

 

Link to comment
Share on other sites

I don't really like litebearer's solution, as it adds far too much complexity, and isn't accurate enough.

 

Is your `date` column of datetime type? Or just date? If you store the time as well, you can use something like this

 

SELECT COUNT(*) FROM `CysticAirwaves`
WHERE `FromUserID` = $auth->id
AND `date` > DATE_SUB(NOW(), INTERVAL 1 DAY)

 

That will return a count of the posts in the last 24 hours, not just today.

Link to comment
Share on other sites

Those are great suggestions that I will surely try, however the major problem I'm having is what I currently have is limiting EVERYONE. I just made a NEW profile and was not able to make a comment because it said I exceeded my daily allowance.

 

Here is the current code:

 

if(isset($_POST['commentProfileSubmit'])) {

 

if($_POST['ProfileComment'] == "" || $_POST['ProfileComment'] == "Tell the community what's on your mind...") {

$valid = false;

$error_msgs_comments[] = "Whoops! You forgot to write your airwave.";

}else{

if($_POST['ProfileComment'] == "" || $_POST['ProfileComment'] == "Leave ".$prof->first_name." a comment here...") {

$valid = false;

$error_msgs_comments[] = "Whoops! You forgot to write your comment.";

}else{

 

/* if the person signed in is NOT the profile */

 

$query = "SELECT * FROM `cysticUsers` WHERE `id` = '" . $prof->id . "'";

$request = mysql_query($query,$connection) or die(mysql_error());

$result = mysql_fetch_array($request);

 

$Email = $result['Email'];

$check_profi = $result['check_profi'];

$check_reply = $result['check_reply'];

 

if($prof->id != $auth->id && $check_profi == 'checked') {

 

$to = $Email;

$subject = "$auth->first_name $auth->last_name commented on your profile on CysticLife";

$message = "$auth->first_name $auth->last_name commented on your profile on CysticLife: <br /><br />\"$body\"<br /><br /> <a href='http://www.cysticlife.org/Profile_build.php?id=" . $prof->id . "'>Click here to view</a><br /><br />Do LIFE,<br /> The CysticLife Team";

$from = "CysticLife <noreply@cysticlife.org>";

$headers  = 'MIME-Version: 1.0' . "\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";

$headers .= "From: $from";

mail($to, $subject, $message, $headers);

}

$query_count = "SELECT COUNT(*) FROM `CysticAirwaves` WHERE `FromUserID` = $auth->id AND date = `CURDATE()`";

$request = mysql_query($query_count,$connection);

$result = mysql_fetch_array($request);

$query = "SELECT COUNT(*) FROM `CysticAirwaves`

          WHERE `FromUserID` = $auth->id

            AND date = `CURDATE()`";

$result = mysql_query($query, $connection);

$post_count = mysql_result($result, 0);

$max_post_per_day = 5;

 

if($post_count >= $max_posts_per_day)

{

    echo "You have reached the maximum number of posts for the day. Try again tomorrow";

}

else

{

 

$comment = mysql_real_escape_string($_POST['ProfileComment']);

$query = "INSERT INTO `CysticAirwaves`

(`FromUserID`, `ToUserID`, `comment`, `status`, `statusCommentAirwave`, `date`, `time`)

VALUES

('{$auth->id}', '{$prof->id}', '{$comment}', 'active', 'active', CURDATE(), CURTIME())";

mysql_query($query, $connection) or die(mysql_error());

 

}

 

if($auth->id == $prof->id) {

$just_inserted = mysql_insert_id();

$query = "UPDATE `CysticAirwaves` SET `status` = 'dead' WHERE `FromUserID` = '" . $auth->id . "' AND `ToUserID` = '" . $prof->id . "' AND `id` != '" . $just_inserted . "'";

$request = mysql_query($query,$connection);

}

}

}

 

}

Link to comment
Share on other sites

If this is your code then you should be able to post JUST the relevant code. Simply dumping a bunch of code here puts the onus on us (those providing free help) to try and decipher the logic and pinpoint the problem.

 

But, looking at your code I am seeing issues that make no sense. Take this for example

   $query_count = "SELECT COUNT(*) FROM `CysticAirwaves` WHERE `FromUserID` = $auth->id AND date = `CURDATE()`";
   $request = mysql_query($query_count,$connection);
   $result = mysql_fetch_array($request);
   $query = "SELECT COUNT(*) FROM `CysticAirwaves`
             WHERE `FromUserID` = $auth->id
               AND date = `CURDATE()`";
   $result = mysql_query($query, $connection);
   $post_count = mysql_result($result, 0);

 

It looks like you are running the same query twice. Why? While that would not specifically cause the problem you are describing, if you can't clean up the code to not be running the same query twice what other problems are there that would? You have a ton of nested if/else statements which is usually unnecessary to that level and only adds complexity. You should draw a flowchart on paper and then code to that.

 

Anyway, you should only get that message if ($post_count >= $max_posts_per_day)

 

So, for debugging purposes you should do a var_dump() of those two variable to verify what they are. Second you should echo the query to determine the number records to the page to verify it "looks" right. If so, run it through your database admin utility (e.g. PHPMyAdmin) and verify it is returning the results you expect.

Link to comment
Share on other sites

Psycho's right (and that's not the first time I've typed that sentence :) )

That section of the code is far too convoluted and you don't do any error checking, so you dont' know what's what.

 

Make LIBERAL use of print_r or echo throughout your code.  It's real easy to comment out when you're done and delete them when everything's tested as working.  Without that feedback, you can't see why it thinks your count is higher than 5 (which is another issue--in your description you said 2 messages a day, but your code says 5.)

 

And that whole section which Psycho pointed out is too much code--and slower code at that.  The PHP manual points out that mysql_result is slower that the corresponding mysql_fetch_array and related functions.

That whole section is as simple as

$query = "SELECT * FROM `CysticAirwaves` WHERE `FromUserID` = $auth->id AND date = `CURDATE()`"; 
$result = mysql_query($query);
if(!$result){echo "<br/><span style="color:red">Whoops!  We couldn't run the query ".$query."!  The SQL error is: ".mysql_errorno()." : ".mysql_error()."</span>;}
else{
     $post_count = mysql_num_rows($result);
    echo "<br/>TEST RESULT:  post_count is: $post_count";
}

 

But your code would be fine if you just ran it once instead of 2x.  Add in some error checking and troubleshooting feedback.  That will help you narrow down what's going on. 

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.