Jump to content

Recommended Posts

I am looping a $.getJSON url with a SEARCHTERM.

 

I want to pro-grammatically also add each letter of the alphabet to the SEARCHTERM like such:

 

    


SEARCHTERM
    SEARCHTERM
    SEARCHTERM
    SEARCHTERM a
    SEARCHTERM a
    SEARCHTERM a
    a SEARCHTERM
    a SEARCHTERM
    a SEARCHTERM
    SEARCHTERM b
    SEARCHTERM b
    SEARCHTERM b
    b SEARCHTERM
    b SEARCHTERM
    b SEARCHTERM

 

Here is the code I am using to loop only the original "SEARCHTERM" without adding the alphabet on each end.

 

Here is the relevant javascript code:

 



    var suggestCallBack; // global var for autocomplete jsonp
    var keywordCount = 0;
    $('body').on("click", '#submit', function() {
      $('#keywords').html('');
      var search_input = $("#keyword").val();
      var language = $("#edit-domain").val();
      callAPI(search_input, language);
      return false;
    });
    
    function callAPI(s, language) {
      $.getJSON("http://suggestqueries.google.com/complete/search?callback=?", {
        "hl": language, // Language
        //"ds":"yt", // Restrict lookup to youtube
        "jsonp": "suggestCallBack", // jsonp callback function name
        "q": s, // query term
        "client": "youtube" // force youtube style response, i.e. jsonp
      });
      suggestCallBack = function(data) {
        var suggestions = [];
        var languageText = $("#edit-domain option:selected").text();
        $('#keywordTable').show();
        $.each(data[1], function(key, val) {
          suggestions.push({
            "value": val[0],
          });
          $('#keywordTable tr:last').after('<tr><td>' + s + '</td><td>' + val[0] + '</td><td>0</td><td>0</td><td>0</td></tr>');
          $('#keywordCount').text(++keywordCount);
          $('#keywordtext').text(s);
          $('#languageholder').text(languageText);
        });
      }
    }


 

code for alphabet (if you need?)

 



    var alphabet = "abcdefghijklmnopqrstuvwxyz0123456789".split("");
    _.each(alphabet, function(letter) {
      callAPI(search_input + ' ' + letter);
    });


 

Summary: I want the relevant code to also add each letter of the alphabet to the beginning and end, while calling callAPI();

Link to comment
https://forums.phpfreaks.com/topic/300692-looping-a-getjson-with-the-alphabet/
Share on other sites

Do both code blocks work? If so, are you just wondering how to combine them? If so, have you tried something like:

var suggestCallBack; // global var for autocomplete jsonp
var keywordCount = 0;
var alphabet = "abcdefghijklmnopqrstuvwxyz0123456789".split("");
$('body').on("click", '#submit', function() {
    $('#keywords').html('');
    var search_input = $("#keyword").val();
    var language = $("#edit-domain").val();
    callAPI(search_input, language);
    _.each(alphabet, function(letter) {
        callAPI(search_input + ' ' + letter);
        callAPI(letter + ' ' + search_input);
    });
    return false;
});

 

Do both code blocks work? If so, are you just wondering how to combine them? If so, have you tried something like:

var suggestCallBack; // global var for autocomplete jsonp
var keywordCount = 0;
var alphabet = "abcdefghijklmnopqrstuvwxyz0123456789".split("");
$('body').on("click", '#submit', function() {
    $('#keywords').html('');
    var search_input = $("#keyword").val();
    var language = $("#edit-domain").val();
    callAPI(search_input, language);
    _.each(alphabet, function(letter) {
        callAPI(search_input + ' ' + letter);
        callAPI(letter + ' ' + search_input);
    });
    return false;
});

Hey, this mostly works, but for some reason the "letter" stays the same.

 

Here is a live preview: http://keyworda.com

 

The problem:  (circled in red): http://i.imgur.com/gR46SuE.png

try this,

 

i have added the regexp to test before adding letter to make sure letter not exists.

pattern search only first letter and last only to make not search in actual search string i have used space after letter and before letter.

 

I am sure you can do this without regexp also, but i just add validation before passing it to function.

 

var alphabet = "abcdefghijklmnopqrstuvwxyz0123456789".split("");
$.each(alphabet, function(key, val) {
var regex = new RegExp("^" + val + ' ', "g");
       if (regex.exec(search_input) == null) {
        //console.log(val + ' '+ search_input);
          callAPI(val + ' '+ search_input);
       }
       var regex = new RegExp(val + ' ' + "$", "g");
       if (regex.exec(search_input) == null) {
        //console.log(search_input + ' ' + val);
          callAPI(search_input + ' ' + val);
       }
 });
Edited by venkatpvc
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.