Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: PHP if syntax

  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question PHP if syntax

    Hello, i could use some help.

    I have a field in the database that takes the users id as values,
    like this: 61, 65, 87, e.t.c.

    Is there an if statement that can check if users id (i can get the users id) is included in one of those numbers.


    Thanks John
    Last edited by gatsman; 03-16-2010 at 03:25 PM. Reason: Resolved

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Are you extracting the id's from the DB and creating an array of id's, a string of comma delimited id's straight from the DB field or...?

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the fast reply

    I am new with PHP.
    I don t know if i read the field in the right way but i hope that you may get your answer from the following

    Code:
    if ($userid == ($votesdb->useridfield)) {
    do something}
    if the fields data are 61, 65, 87
    $userid value will have to be exactly 61, 65, 87 in order for this to be true
    but the $userid will be 61 or 87 or e.t.c.

    I hope i said it right.
    Thanks again for reading this.

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You still didn't answer Matt's question. Is $votesdb->useridfield an array, a string, or something else?
    Are you a Help Vampire?

  • #5
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I think it's a string of comma separated id's.

  • #6
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    You'd need to use in_array.

    PHP Code:
    $checkid  $userrow['id'];
     
    $unique explode','$checkid );

    if (
    in_array("$id"$unique)) {
        echo 
    "This ID matches.";

    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by masterofollies View Post
    You'd need to use in_array.

    PHP Code:
    $checkid  $userrow['id'];
     
    $unique explode','$checkid );

    if (
    in_array("$id"$unique)) {
        echo 
    "This ID matches.";

    If you're going to give an example, at least either use the asker's original code or state that the example is an unrelated example.

    For example:
    PHP Code:
    $ids explode(','$votesdb->useridfield);
    if(
    in_array($userid$ids))
    {
        
    // do something


  • Users who have thanked Inigoesdr for this post:

    gatsman (03-16-2010)

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    $idfield = explode(',', str_replace(' ', '', $votesdb->useridfield));
    
    if (in_array($userid, $idfield))
    {
            [do something here]
    }

  • #9
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you all for your effort.

    Quote Originally Posted by Inigoesdr View Post
    If you're going to give an example, at least either use the asker's original code or state that the example is an unrelated example
    Thank you!!! i am new at this and your example made it clear.

  • #10
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Quote Originally Posted by Inigoesdr View Post
    If you're going to give an example, at least either use the asker's original code or state that the example is an unrelated example.

    For example:
    PHP Code:
    $ids explode(','$votesdb->useridfield);
    if(
    in_array($userid$ids))
    {
        
    // do something

    That coding isn't something I am familar with. I posted the correct code, just needed to be customized. No reason to make me look dumb in front of everyone when I supplied the correct code.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #11
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by masterofollies View Post
    No reason to make me look dumb in front of everyone when I supplied the correct code.
    That wasn't his? intention, I believe. It's simply a fact that if someone posts on here, their level of proficiency at coding could be anywhere from practically zero to master of. If someone is in the early stages of programming skills, presenting an indirect example may be taken as being literal code to use, hence could end up confusing the O.P tenfold, hence the suggestion to present usable code that can be dropped in or to mention the fact that it is unusable, (directly, as is), example code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •