Lukeidiot Posted January 29, 2016 Share Posted January 29, 2016 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(); Quote Link to comment https://forums.phpfreaks.com/topic/300692-looping-a-getjson-with-the-alphabet/ Share on other sites More sharing options...
cyberRobot Posted January 29, 2016 Share Posted January 29, 2016 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; }); Quote Link to comment https://forums.phpfreaks.com/topic/300692-looping-a-getjson-with-the-alphabet/#findComment-1530584 Share on other sites More sharing options...
Lukeidiot Posted January 29, 2016 Author Share Posted January 29, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/300692-looping-a-getjson-with-the-alphabet/#findComment-1530593 Share on other sites More sharing options...
venkatpvc Posted January 30, 2016 Share Posted January 30, 2016 (edited) 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 January 30, 2016 by venkatpvc Quote Link to comment https://forums.phpfreaks.com/topic/300692-looping-a-getjson-with-the-alphabet/#findComment-1530604 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.