Counting Row By Specific User And Limit!

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

Post Reply
canabatz
Forum Newbie
Posts: 8
Joined: Thu Nov 20, 2008 3:08 pm

Counting Row By Specific User And Limit!

Post by canabatz »

i need to count the results sent by users ,i need a code to start counting results and find me results from the same user ,i want to be able to limit results by user ,i want to limit the user to maximum 4 rows in a run if it reaches 5 rows the result number 5 will be deleted or something !

this is my example code fo geting the results:

select * from my table where username=$username and bidprice=$bid_price order by bid_price desc

like this situation is good: 4 result from the same user then user 2 is between ,the result 5 from user 1 ,user 1 can continue to post more and have 4 rows in a run again!!


user1
user1
user1
user1
user2 <===== there is one result between user1 so its ok
user1

and this way is not good:


user1
user1
user1
user1
user1 <===== there is 5 result from user1 so result No. 5 will be deleted
user2
thanx
canabatz
Forum Newbie
Posts: 8
Joined: Thu Nov 20, 2008 3:08 pm

Re: Counting Row By Specific User And Limit!

Post by canabatz »

any one got an idea?

help ,please.
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: Counting Row By Specific User And Limit!

Post by sparrrow »

I'm really confused by how you've explained what you need but I'll take a stab at it. First, if you need the invalid post deleted, then I say you should not add it in the first place. Check what the user already has in the DB when they post, but before you save the post to the DB. Check to make sure they don't have 4 in a row, and if they do then give them an alert about it.

To run the check, you should pull out all posts for all users matching your required criteria, and parse through them 1 at a time using PHP. If you catch 4 in a row, then throw the alert and don't post their 5th post to the DB. Code would be like this:

Code: Select all

$Query="select * from mytable where column=$value order by column desc";//This is your query to pull all posts for all users
$result = mysql_query($Query);//execute query
while ($Row = mysql_fetch_array($result)) {//step through results one row at a time
   $thisvalue = $Row(user);//user for current row
      if ($thisvalue == $previousvalue) {//if user in current row is same as user from last row, add 1 to counter
         $x++;
      } else {//reset counter
         $x = 0;
      }
   $previousvalue = $Row(user);//put current user into variable for next pass
   if ($x >= 4) {//if x is 4 or more that means user had 4 posts in a row
      $uhoh = 1;//set boolean to check and perform action
   }
}
 
if ($uhoh) {
   echo "Too many posts";//Tell user they aren't allowed to post
} else {
   //create and execute your INSERT query here, because they don't have more than 4 posts
}
Post Reply