/    Sign up×
Community /Pin to ProfileBookmark

Been a while…

…but I’m back with a headache maker.

[code=php]$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]”;
}
}[/code]

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.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@Doc_ThirstauthorOct 17.2006 — OK corrected, ready for help ?
Copy linkTweet thisAlerts:
@NogDogOct 17.2006 — Here's a thought:
[code=php]
$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
}
[/code]
Copy linkTweet thisAlerts:
@Doc_ThirstauthorOct 18.2006 — Here's a thought:
[code=php]
$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
}
[/code]
[/QUOTE]


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:

if($arr[0] LIKE $totalrecords)

{

}

else

{

echo "$arr[0]";

$totalrecords = "$totalrecords $arr[0]";

}
Copy linkTweet thisAlerts:
@NogDogOct 18.2006 — 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:
<i>
</i>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():
[code=php]
if(strpos($totalrecords, $arr[0]) !== FALSE) // use !== not !=
{
[/code]
Copy linkTweet thisAlerts:
@Doc_ThirstauthorOct 18.2006 — Thanks NogDog!

Once again, you've both fixed my problem, and introduced me to a new tech that will make my life so much easier.
×

Success!

Help @Doc_Thirst spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...