Jump to content

Php Array And For Loop Problem


tinfanide
 Share

Recommended Posts

<?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

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>

Edited by Tin
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...