...

View Full Version : Resolved Array help



harkly
09-15-2011, 05:43 PM
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::


$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

$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??

Inigoesdr
09-15-2011, 05:48 PM
You need to be checking each row inside of the loop:

while($row = mysql_fetch_assoc($result))
{
if($row['id'] == $checkID)
print "<br>here it is";
else
print "<br>not found";
}

harkly
09-15-2011, 06:03 PM
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


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";
}

Fou-Lu
09-15-2011, 06:06 PM
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.

harkly
09-15-2011, 06:43 PM
Thanks Fou-LU! I knew I was making it to hard!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum