$recordset1 = mssql_query ("select distinct id from site where (name like '%$freetext%') or (location like '%$freetext%') or (state like '%$freetext%')", $handle);
while($arr1 = mssql_fetch_row($recordset1))
{
$recordset2 = mssql_query ("select * from object where site like '%$arr[0]%'", $handle);
while($arr2 = mssql_fetch_row($recordset2))
{
echo "$arr2[0]";
}
}
ok lets assume you have two "sites" US and Canada. So your first loop returns the id's 1 and 2 (one for each site).
No you only have an object found both in US and Canada but because it's embedded in the first loop it will display twice, but it should only display once.
If you want to check this code out, shoot me a pm and I'll toss you the URL.
Let me know if you have questions, I'll be happy to expand on anything that will you help me.
Last edited by NogDog; 10-17-2006 at 05:18 PM.
Reason: changed code tags to php tags to make it easier to read
$recordset1 = mssql_query ("select distinct id from site where (name like '%$freetext%') or (location like '%$freetext%') or (state like '%$freetext%')", $handle);
$where = array();
while($arr1 = mssql_fetch_row($recordset1))
{
$where[] = "site like '%{$arr1[0]}%'";
}
if(count($where))
{
$whereClause = implode(' || ', $where);
$recordset2 = mssql_query ("select * from object where $whereClause", $handle);
while($arr2 = mssql_fetch_row($recordset2))
{
echo "$arr2[0]";
}
}
else
{
// handle error for no matches in first query
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
$recordset1 = mssql_query ("select distinct id from site where (name like '%$freetext%') or (location like '%$freetext%') or (state like '%$freetext%')", $handle);
$where = array();
while($arr1 = mssql_fetch_row($recordset1))
{
$where[] = "site like '%{$arr1[0]}%'";
}
if(count($where))
{
$whereClause = implode(' || ', $where);
$recordset2 = mssql_query ("select * from object where $whereClause", $handle);
while($arr2 = mssql_fetch_row($recordset2))
{
echo "$arr2[0]";
}
}
else
{
// handle error for no matches in first query
}
Nice, let me make sure I understand, you are creating an array called where containing all the returns from the first sql statement. If results are returned, continue on. Now this is where I get a bit hazy, let me make sure i got this. The implode combines the array into a string, then you run your second search off that string. So if the first returns 1 and 2 your whereclause is gonna be "12" or "1 2". OK I need to get my head around this, but I think you are on the right track. The problem lies in the formatting of the data I believe.
Is there any qualifier similiar to "LIKE" in if statements so it would look like this:
It sounds like you get the gist of things. If the first query returned two rows with site values of "1" and "2", then the result of the implode() would be:
Code:
site like '%1%' || site like '%2%'
So, we're just popping that string into the where clause.
As far as the if question, you could use strpos():
PHP Code:
if(strpos($totalrecords, $arr[0]) !== FALSE) // use !== not !=
{
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks