tinfanide Posted November 25, 2011 Share Posted November 25, 2011 <?php$sites = array(array(array("Google","http://www.google.com"), array("search engine","platform","news","weather") ), array(array("Youtube","http://www.youtube.com"), array("search engine","platform","news","video") ) ); $query = $_GET["query"];if (strlen($query) > 0) {$suggestions = "";for ($i=0; $i<count($sites); $i++) { if (strtolower($query) == strtolower(substr($sites[$i][0][0],0,strlen($query)))) { for ($j=0; $j<count($sites[$i][1]); $j++) if ($suggestions == "") { $suggestions = "<a href=".$sites[$i][0][1]." target='_blank'>".$sites[$i][0][0]."</a>"."<br />".$sites[$i][1][$j]; } else { $suggestions += " , "."<a href=".$sites[$i][0][1]." target='_blank'>".$sites[$i][0][0]."</a>"; } } }}if ($suggestions == "") {$response = "no suggestion related to your query: ".$query;} else { $response = $suggestions; }echo $response;?> I want when users search for the first array, it also displays the third array array("search engine","platform","news","weather") But I seem to fail to use a for loop inside another for loop. Any fix for that? Link to comment Share on other sites More sharing options...
JamesB Posted November 25, 2011 Share Posted November 25, 2011 try changing += to .= and change $sites[$i][0][0] to $sites[$i][1][$j] in this line: $suggestions += " , "."<a href=".$sites[$i][0][1]." target='_blank'>".$sites[$i][0][0]."</a>"; Link to comment Share on other sites More sharing options...
tinfanide Posted November 26, 2011 Author Share Posted November 26, 2011 try changing += to .= and change $sites[$i][0][0] to $sites[$i][1][$j] in this line: $suggestions += " , "."<a href=".$sites[$i][0][1]." target='_blank'>".$sites[$i][0][0]."</a>"; Yes, I missed this important bit. And how can I let users click on the returned tags (e.g. "search engine", "platform") and do another tag search?My demo:http://lifelearning.x10.mx/LiveSearch/live-search.html <?php$sites = array(array(array("Google","http://www.google.com"), array("search engine","platform","news","weather") ), array(array("Youtube","http://www.youtube.com"), array("search engine","platform","news","video") ) ); $query = $_GET["query"]; if (strlen($query) > 0) { $suggestions = ""; for ($i=0; $i<count($sites); $i++) {for ($j=-1; $j<count($sites[$i][1]); $j++) { if (strtolower($query) == strtolower(substr($sites[$i][0][0],0,strlen($query)))) { if ($suggestions == "") { $suggestions = "<a href=".$sites[$i][0][1]." target='_blank'>".$sites[$i][0][0]."</a>"."<br />".$sites[$i][1][$j]; } // I think I need to work on this part but just no idea how to bring a new-defined $query (e.g. a tag) back to the HTML form else { $suggestions .= " | "."<a href=".$sites[$i][0][1]." target='_blank'>".$sites[$i][1][$j]."</a>"; } // The HTML form is below this block of codes } if (strtolower($query) == strtolower(substr($sites[$i][1][$j],0,strlen($query)))) { if ($suggestions == "") { $suggestions = "<a href=".$sites[$i][0][1]." target='_blank'>".$sites[$i][0][0]."</a>"; } else { $suggestions .= " | "."<a href=".$sites[$i][0][1]." target='_blank'>".$sites[$i][0][0]."</a>"; } } } }}if ($suggestions == "") {$response = "no suggestion related to your query: ".$query;} else { $response = $suggestions; }echo $response;?> HTML form: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Live Search</title><script>var liveSearch = function(query){ if(query.length == 0){ document.getElementById("suggestions").innerHTML = ""; return; } xmlhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");xmlhttp.onreadystatechange = function(){ document.getElementById("suggestions").innerHTML = (xmlhttp.readyState == 4 && xmlhttp.status == 200) ? xmlhttp.responseText : "loading your search" ; } xmlhttp.open("GET", "live-search.php?query="+query,true);xmlhttp.send(); }</script></head><body><form>Your Search: <input type="text" onkeyup="liveSearch(this.value)" value="" size="20" /></form><p id="suggestions"></p></body></html> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.