Jump to content

continuous array - push,merge???


cindreta

Recommended Posts

i am working on this search engine. and in the advanced section you can turn on/off some of the options. So it you turn on an option it will search another table and so on.

 

Instead of building a massive query and left joining lots of stuff. I tried a different approach.

 

There is always a basic query and it returns the results to an array like this

 

 

$sql = mysql_query("SELECT SQL_CACHE id FROM table WHERE field LIKE '$getquery' ORDER BY field");
  while ($row = mysql_fetch_array($sql)) {						
     $idArray[] = $row['id'];				
  } mysql_free_result($sql);

 

and if something is turned on then it adds something like this:

 

if($_POST['value'] == "1") {
$sql = mysql_query("SELECT SQL_CACHE table1_relation_id FROM table2 WHERE field2 LIKE '$getquery' ORDER BY field2");
  while ($row = mysql_fetch_array($sql)) {						
    $idArray[] .= $row['table1_relation_id'];				
  } mysql_free_result($sql);
} //end if

 

so basicy i just continue the array and it keeps pumping ids into a big aray. after that i run array_unique and remove all duplicate results and then i foreach and get the whole item info ased on every id. it runs pretty fast.

 

 

now i would like to make the second one a function. but when i do that it does not work.

 

function sample($query, $table, $field) {
$sql = mysql_query("SELECT SQL_CACHE naziv_id FROM ".$table." WHERE ".$field." LIKE '$query'");			
while ($row = mysql_fetch_array($sql)) {	
		$return[] .= $row['naziv_id'];
} mysql_free_result($sql);

return $return;				

}

 

before i put it into a function $idArray vas a single continuous array. And if i try to do it like this:

 

$arr[] = sample($query, "table1", "filed_id");
$arr[] .= sample($query, "table2", "filed_id");

 

it returns the result of the first array ok but if there are more then one results in other arrays it returns and arary inside of the array.

 

like this:

 

array(4) { [0]=> array(2) { [0]=> string(4) "9057" [1]=> string(5) "14186" } [1]=> string(5) "Array" [2]=> string(5) "Array" [3]=> string(0) "" } 

 

and all i wanna have is a single id variable that has all the id's in it. like $array = 1,2,3,4,5 but from different sources/functions

 

can you help? thank you so much.

 

 

Link to comment
Share on other sites

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.