Jump to content

php count() string except what is defined


shortysbest

Recommended Posts

I have the php to count the parts of the string that i want, but i want to define a variable for it to not count if it's found.

 

in my database the column would look like this:

 

(1), (2), (13), (111),

 

So what is returned is 4

 

However i want it to return 3, skipping the number i define.

 

So if the number turns out to be 13, It should only count, 1,2, and 111, giving 3.

 

(these would be user ids, so if session['id'] is 13 i do not want it to count the 13)

 

this is what i'm using to count now:

 

$num = explode(', ', $number);
$num_of_users = count($num);

 

Link to comment
Share on other sites

Have you tried writing any code?

 

This wreaks of poor database design in the first place but...

 

<?php
function stupidcount($str, $except) {
  $arr = explode('), (', trim($str, '()'));
  $cnt = 0;
  forreach ($arr as $v) {
    if ($v != $except) {
      $cnt++;
    }
  }
  return $cnt;
}
  
$str = '(1), (2), (13), (111)';
echo stupidcount($str, 13);

Link to comment
Share on other sites

I haven't tried writing any code for this, got everything else working. And this design is for a little notification system for each comment, where this was the only way i could think of to do this without taking a load of rows in the database.

Which basically what its purpose is is that when somebody comments on a post you have made it will show a notification on the post itself saying, for instance 5 new comments, and the ids of all of the users that have commented on the post are put into this string, so this way when one person clicks on the post to see the new comments it will remove the notification for them, but not everyone else that has posted on it. It will remove their id from the string.

 

Not sure how clear i have explained it, but if you knew of any better ways of accomplishing this, that would be nice, this is a pain :P

 

 

Link to comment
Share on other sites

if you knew of any better ways of accomplishing this, that would be nice, this is a pain

 

Of course its a pain, because its the wrong way to go about it.

 

Surely you already have the id of each user stored along with there comment and the id of the thing they are commenting on?

Link to comment
Share on other sites

All that is being stored again is the id of the users that have commented on the status.

 

my database for commenting on the status looks like this:

 

|-----id-----|----post_id----|----user_id----|-----notification----|-----comment------|

 

*id is unique id for the comment

*post_id is the original post unique id that the comment belongs to

*user_id is the users id that made the comment

*notification is where the (2), (3), (5), (22), comes in. Which is a string of all of the id of all of the users that have made a comment on the post

Link to comment
Share on other sites

You simply don't need notification. The data is already there.

 

Say I'm user 13, and Ive commented on article 44. I now go and view article 44 and see a list of all users who have also commented on article 44..... How do you get this list?

 

SELECT user_id from tbl WHERE post_id = 44 and user_id != 13

Link to comment
Share on other sites

well no that's not what i'm doing, I could show you my site and try to show u from there what it is i'm trying to achieve

 

http://www.squiblo.com/index.php?node=profile&user=1

 

There is a place for you to share, which is the post, and then there is a part to comment on it, That is what i'm focusing on. The red circle with the number is where it should display how many new comments have been made since you last clicked comments

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.