Search among more than one sections

9 posts by 3 authors in: Forums > CMS Builder
Last Post: September 8, 2022   (RSS)

By andreasml - July 5, 2022

Hi

My CMSB contains a bunch of sections which share some common fields (eg. surname, name, id number etc). 

I wonder whether I could create a search tool for all different sections together (optimally, I could predefine which sections I would like to search into), instead of searching within each section as  the default search option of CMSB does.

Kind regards, 

Andreas Lazaris

By daniel - July 5, 2022

Hi Andreas,

Multi-table search is not natively supported in CMSB, but I can definitely see the potential use cases. It would be possible to create a plugin that does this, though the complexity of doing so would depend a bit on your needs.

CMSB has an experimental function called searchMultipleTables() that can be used to search through multiple tables as you want, but it was designed like a front-end view for listing records, so the list HTML is 100% custom, and it doesn't include the functionality built into the CMSB admin (view/modify/delete/etc.). If all you need to do is search the tables and list specific details this could work well. It's also pretty simple to create links to the edit page of any given record ("/cmsb/admin.php?menu=[insert table name here]&action=edit&num=[insert record num here]").

I've attached a file (multiSearch.php) that shows an example of how it can be used. The Sample Plugin (samplePlugin.php) could then be used as an example of how to add a page in CMSB to output the multi-search form/output.

If you need more of the built-in CMSB functionality, this is still possible but would require a more significant amount of custom programming. It would require either creating a plugin that replicates the default list and edit pages - but modified to support multiple tables - or a fully custom plugin built to your specific needs. As always, we're happy to provide an estimate for creating something like this if you're interested!

Let me know if you have any other questions.

Thanks!

Daniel
Technical Lead
interactivetools.com
Attachments:

multiSearch.php 4K

By andreasml - July 7, 2022

Hi Daniel

Thank you for your reply. For the beginning, this option seems acceptable. 

I created a web-page containing the multisearch.php file but it does not give anything 

Could you please give me a way to start?

Regards, 

Andreas

By daniel - July 7, 2022

Hi Andreas,

If the page is blank, it's likely having an issue loading the viewer library. By default it specifies the path manually:

require_once "../lib/viewer_functions.php";

You can either update this to point to the location of your viewer_functions.php file, or copy the "//load viewer library" section from the code generator, which would look something like this:

  // load viewer library
  $libraryPath = 'cmsb/lib/viewer_functions.php';
  $dirsToCheck = ['','../','../../','../../../','../../../../'];
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
  

Let me know if that helps!

Thanks,

Daniel
Technical Lead
interactivetools.com

By daniel - July 11, 2022

Hi Andreas,

1. The sample multiSearch.php starts with the "debugSql" search option set to 1 - you can either change this to 0 or remove it entirely, which should remove that output.

2. This portion of the code is where you can change the returned values:

      $searchTables['news'] = array(
        'viewerUrl'       => 'newsPage.php',
        'titleField'      => 'title',
        'summaryField'    => 'summary',
        'searchFields'    =>  array('title','summary','content'),
      );

For example, you can change "title" and "summary" to the appropriate field names in your table. As well, you can use field1, field2, etc. to add up to 10 additional fields, like this:

      $searchTables['news'] = array(
        'viewerUrl'       => 'newsPage.php',
        'titleField'      => 'title',
        'summaryField'    => 'summary',
        'field1'          => 'content',
        'field2'          => 'category',
        'searchFields'    =>  array('title','summary','content'),
      );

3. The link-building portion of this function is currently incomplete - I would recommend constructing your own links in the page's output.

Regarding the warning: if you need this to open viewer/editor pages in the CMS it will need to be built into a plugin - I had forgotten that it's not possible to directly link to the admin from non-admin pages. Otherwise, if it'd work to view/edit on the front-end like you're doing with this test page, the simplest way would be to either use a regular details page from the Code Generator (view) or use the Form Generator with record editing enabled (edit).

4. You can change the number of records with the "perPage" search option.

Hopefully that all helps - let me know if you have any further questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By Tom - September 7, 2022 - edited: September 7, 2022

Hello Daniel,

As you said, "you can use field1, field2, etc. to add up to 10 additional fields"

Since 10 fields is not enough for my search page.

Is it possible to add more fields?

For example add up to 20 additional fields.

Thank You

By daniel - September 7, 2022

Hi Tom,

Do you need to search using more than 10 fields, or do you need to return more than 10 fields for display?

It's possible to use any number of fields for searching, but it can only return up to 10 custom fields (in addition to the title and summary field). If you need to return more than the available fields, I can think of two options:

  1. After running the search, run an additional query in the display loop to get the full details of each row with mysql_get(), like this:
    <?php foreach ($searchRows as $record): ?>
      <?php $fullRecord = mysql_get('table_name', $record['num']); ?>
    <?php endforeach; ?>
    ​
    Be aware that this adds a MySQL query for every record, so be cautious using this approach on pages where you need to list a large number of records.

  2. Modify this line in /cmsb/lib/viewer_functions.php (at or near line 1221):
        foreach (range(1,10) as $num) {​
    Changing the 10 to however many fields you need to use. Be warned that this is a core file, so any changes you make to it may be erased by future upgrades. If you build any functionality in this way, make sure to document them so that they can be re-done after upgrades.

Let me know if you have any other questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By Tom - September 8, 2022

Thanks a lot Daniel !!