Customise the search results for MX Site Search

Customise the search results for MX Site Search

MX Site Search can be frustratingly limited at times. However, it is quite easy to overcome this limitation and with a little bit of custom coding, you can expand the search results to show just about anything you want. 
divider

MX Site Search can be frustratingly limited at times. However, it is quite easy to overcome this limitation and with a little bit of custom coding, you can expand the search results to show just about anything you want. That includes modifying the URL that points to the result.

  • First define your MX Search behaviour as you would usually do taking note of the URL parameter you define in the "Table Search Configuration"

As an example mine is read_articles.php?id= and the URL parameter is id (the database column that stored the unique id)

The generated code for the repeat region that displays the search results will look something like this:

<div align="justify"><p><a href="<?php echo $row_rsSearch['url_cah']; ?>" target="_self"><?php echo (min($startRow_rsSearch + 1, $totalRows_rsSearch)) + $offset?>. <?php echo $row_rsSearch['title_cah']; ?></a><br><?php echo $KTSE_rsSearch->formatDescription($row_rsSearch['shortdesc_cah']); ?><br><font color="#339900"><?php echo $row_rsSearch['url_cah']; ?></font> </p></div>

Now the problem is if you need to have more than 1 URL parameter to open the page where the result is pointing to you are stranded.  You are also limited to only display the information that is available within the rsSearch recordset.

To overcome this you need to extract the unique record id from $row_rsSearch['url_cah']

  • Within the repeat region add this code (the <div align="justify"> is the first line in the repeat region):
<div align="justify">
      <?php
              // Extract primary key from search results
              $q=$row_rsSearch['url_cah']; $paramName = 'id'; // This is your url_parameter defined in the search behaviour
              $partial_q = stristr($q, $paramName);
              $value = '';
              $pos = strpos($partial_q, '&');
              if ($pos !== false) {
                            $value = substr($partial_q, strlen($paramName)+1, $pos - strlen($paramName) - 1);
                            } else {
                                          $value = substr($partial_q, strlen($paramName)+1, strlen($paramName));
                                          }
              echo $value; // This will be the primary key value (id) In production mode you will not echo this value.

If you run your query now you will see the primary ($value) key displayed in the repeat region. Obviously it should be different for every entry and unique to the record.

Now you can use $value to filter your custom recordset to get the information you need to customise everything you want inside your search result.

  • Directly following the echo $value; line add your mysql query:
//echo $value
              mysql_select_db($database_siteConn, $siteConn);
              $query_rs_custom_results = "SELECT * FROM articles WHERE a_id = '$value'";
              $rs_custom_results = mysql_query($query_rs_custom_results, $siteConn) or die(mysql_error());
              $row_rs_custom_results = mysql_fetch_assoc($rs_custom_results);
              $totalRows_rs_custom_results = mysql_num_rows($rs_custom_results);
​​​​​​​// End Extract primary key from search results

The sql query can be anything, including joined statements, as long as it is valid and can be filtered by the $value as the primary key.

Now you are free to modify your search results as you wish.

For simplicity I just modified the url to the result page:

<p><a href="<?php echo $row_rsSearch['url_cah']; ?>&g_id=<?php echo $row_rs_custom_results['a_gal_id']; ?>&m_id=<?php echo $row_rs_custom_results['mem_id']; ?>" target="_self"><?php echo (min($startRow_rsSearch + 1, $totalRows_rsSearch)) + $offset?>. <?php echo $row_rsSearch['title_cah']; ?></a><br><?php echo $KTSE_rsSearch->formatDescription($row_rsSearch['shortdesc_cah']); ?><br></p>

The code in <bold> is my custom code.

The important part of this exercise is to ensure the extraction of the primary value and the sql query is INSIDE the repeat region. If it is not you will end up with the value of the first record in the search result as the value of $value

Written by:  - 1 Jul, 2010