0

Sry, for unclear title to the question, I hope someone will correct it to something more precise.

The situation:

I am using a google custom search engine which is set to search only two sites and display results only.In control panel of GSE when I go to advanced tab in search features, i can specify query parameter that it will look for. after I am done i get a javascript code to paste in my page's code.

I believe that this javascript code listens to $_GET['q'] variable and then produce results based on that.

Problem :

I have four field in a page and a php form set up to display result from the other sites, the end result is the URL with four get variables like $_GET['q1'] to $_GET['q4']. I don't have a $_GET['q'] in the page or anywhere else.

What I tried:

  1. I just can't make the earlier form to print another or a combined get parameter as $_GET['q']. i'll have to add a lot of JS and a hidden input field i guess.

  2. I tried setting $_GET['q'] in php as $_GET['q']=$_GET[q1]; and then adding the javascript for custom search engine.

  3. I tried adding a link to update or set GET <a href="myurl.com?q=searchthis">anything</a> but this doesn't seem to work.

I think the reason is maybe lack of knowledge about how GSE gets a keyword,search and returns result, and lack of knowledge about $_GET parameter.

Question:

  1. Anyway I can set $_GET['q'] in my php script(dynamically?) that google custom search engine javascript will understand and work fluently ??
  2. Is there any other better way to do this.

This doesn't need me to show code but here is some code that may be relevant to the question :

GOOGLE CUSTOM JAVASCRIPT

<script>
  (function() {
    var cx = '010474608308939830468:6s3kklfffuu';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//cse.google.com/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:searchresults-only></gcse:searchresults-only>

And here is my php approach :

$_GET['q'] = $_GET['q1'] ;

any help will be greatly appreciated and hope you can also clear out on the functioning of GSE for fellow readers. Thanks, in advance

Rishiraj Purohit
  • 447
  • 5
  • 17

1 Answers1

0

So I was unclear from your question if you need to search ALL of the queries, or if the user is only going to fill in one of the forms. If the user fills in only one form, this should work - using PHP to check which query variable is used, and then having the CSE look at that parameter.

<?php
    if (isset($_GET['q1'])  && $_GET['q1'])
       {$param = 'q1';
       } elseif (isset($_GET['q2'])) {
      $param = 'q2';
       }
    echo "<gcse:search queryParameterName='".$param."'></gcse:search>";
?>

If you need to concatenate all your queries into one, your best bet would probably be to rewrite the URL using htaccess before it hits the page, so that all the parameters are concatenated into one query parameter before the page loads. Then set the queryParameterName to whatever you have called that variable.

If you don't have access to creating a rewrite rule on your server, you could explore this answer: changing the query string without reloading the page. Just do that ahead of the tag, which evaluates the query string to figure out what you are searching for.

Community
  • 1
  • 1
Joel
  • 237
  • 3
  • 9