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 5 of 5

Thread: Array help

  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts

    Array help

    Ok so I am not to familiar with arrays, working hard on learning. I am pretty sure that I am working with an array but not sure how to handle it.

    Here is my problem.

    I am trying to pull a group of IDs from a DB and then search that group to see if a value exist, if it does this will happen if not something else will happen.


    My code::
    PHP Code:
    $checkID 6;

    $sql2 "SELECT id FROM threads WHERE receiver='$clientID'";
    $result mysql_query($sql2) or die(mysql_error());

        while(
    $row mysql_fetch_array($result)){
            
    print_r ($row);
            echo 
    "<br />";
        }

        if (
    in_array($checkID$row)){
          print 
    "<br>here it is"
             } 
            else {
                print 
    "<br>not found"
             } 
    This is the result I am getting from the print_r, it is not what I have been working with so not sure how to handle it or even if I am on the right track.
    Array ( [0] => 9 [id] => 9 )
    Array ( [0] => 5 [id] => 5 )
    Array ( [0] => 10 [id] => 10 )
    Array ( [0] => 41 [id] => 41 )
    Array ( [0] => 42 [id] => 42 )
    I am able to get the results I want from another array that is not being pulled from a DB so I know where my issue lays just cannot figure out what to do.

    This is one I have working
    PHP Code:
    $a = array ('a' => 'apple''b' => 'banana''c' => 'carrots');
    print_r ($a);

        if (
    in_array("apple"$a)){
          print 
    "<br>here it is"
             } 
            else {
                print 
    "<br>not found"
             } 
    Can someone help me out? Or point me in the right direction??
    Last edited by harkly; 09-15-2011 at 06:44 PM.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You need to be checking each row inside of the loop:
    PHP Code:
    while($row mysql_fetch_assoc($result))
    {
        if(
    $row['id'] == $checkID)
            print 
    "<br>here it is";
        else
            print 
    "<br>not found";  


  • #3
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Tried that but was having issues with it but I think, after all my searching, that I have found an answer for it..

    This seems to work

    PHP Code:
        while($row mysql_fetch_assoc($result)){
            
    print_r ($row);
            echo 
    "<br />";
            
            
    $found false;
            if (
    $row ['id'] == $checkID){
                
    $found true;
                break;
            } 
        }

        if (
    $found == true) {
            print 
    "<br> found"
        } 
        else {
            print 
    "<br> not found";
        } 

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You have a break in there, indicating you only care about the one record.
    Add it to your WHERE clause instead, then you simply need to count the rows to see if it has been found.

  • Users who have thanked Fou-Lu for this post:

    harkly (09-15-2011)

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Thanks Fou-LU! I knew I was making it to hard!


  •  

    Posting Permissions

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