www.webdeveloper.com
Results 1 to 6 of 6

Thread: Been a while...

  1. #1
    Join Date
    Dec 2005
    Posts
    203

    Been a while...

    ...but I'm back with a headache maker.

    PHP Code:
    $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

  2. #2
    Join Date
    Dec 2005
    Posts
    203
    OK corrected, ready for help

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    Here's a thought:
    PHP Code:
    $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

    eBookworm.us

  4. #4
    Join Date
    Dec 2005
    Posts
    203
    Quote Originally Posted by NogDog
    Here's a thought:
    PHP Code:
    $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:

    if($arr[0] LIKE $totalrecords)
    {

    }
    else
    {
    echo "$arr[0]";
    $totalrecords = "$totalrecords $arr[0]";
    }

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    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

    eBookworm.us

  6. #6
    Join Date
    Dec 2005
    Posts
    203
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles