This topic is locked

SEARCH field AND field

8/3/2025 7:03:38 AM
PHPRunner General questions
K
KeithS author

I have a DB column called ATTR. It contains a CSV string of multiple atributes - such as LP:, RT:, BZ: and VZ: I want to in my search panel be able to open two or three ATTR search boxes and enter one or more of these kind of ATTR strings and becasue of the what the ATTR values are strung together, the search option must always be CONTAINS. I want the search to be value AND value AND value. It appears that the normal behavior is value OR value OR value. How can I set the search to be AND???

I thought I saw something about this same question but cannot find it. So I decided I had to post the question. I did see how to setup things like multiple OR values but that did not help what I need.

Thanks
Keith

ChatGPT 8/3/2025

Hi Keith,

In PHPRunner, customizing the search behavior to use "AND" instead of "OR" for multiple search boxes can be a bit tricky, especially when dealing with CSV strings. However, you can achieve this by using a custom search condition in your query.

Here’s a step-by-step guide to set this up:

  1. Create the Search Fields: In the PHPRunner search panel, add your custom search fields for the ATTR column. You can create multiple search boxes (e.g., ATTR1, ATTR2, ATTR3) for users to enter their search values.


  2. Modify the SQL Query: You will need to customize the SQL query to combine the search conditions with AND. To do this, navigate to the "Before SQL Query" event in PHPRunner. Here, you can add custom logic to append the necessary AND conditions to your SQL statement.


  3. Custom Search Logic: In the "Before SQL Query" event, you can write a custom SQL condition. Here’s a sample code snippet that you might use:

     // Assuming ATTR1, ATTR2, ATTR3 are your search fields
    $searchConditions = array();

    if (!empty($_GET['ATTR1'])) {
    $searchConditions[] = "ATTR LIKE '%" . db_escape($_GET['ATTR1']) . "%'";
    }

    if (!empty($_GET['ATTR2'])) {
    $searchConditions[] = "ATTR LIKE '%" . db_escape($_GET['ATTR2']) . "%'";
    }

    if (!empty($_GET['ATTR3'])) {
    $searchConditions[] = "ATTR LIKE '%" . db_escape($_GET['ATTR3']) . "%'";
    }

    if (count($searchConditions) > 0) {
    $strWhereClause = implode(' AND ', $searchConditions);
    $where .= ($where ? ' AND ' : '') . $strWhereClause;
    }

  4. Test Your Search: After implementing the above code, generate your project and test the search. Enter values in the search boxes and ensure that the query returns results that contain all specified attributes.


  5. Adjusting for Edge Cases: Depending on your CSV formatting and potential edge cases (like leading/trailing spaces), you might want to enhance the logic further to clean up the inputs or handle specific scenarios.



By following these steps, you should be able to set up your PHPRunner application to use an "AND" logic for multiple search fields on the ATTR column.

If you run into any issues or need more specific guidance, feel free to ask!

Best,
[Your Name]

Sergey Kornilov admin 8/3/2025

My suggestion is to use Search API in situations like this:
https://xlinesoft.com/phprunner/docs/searchapi_about.htm

You will have a full control over WHERE clause there.