2011-06-24 63 views
1

基本上,我为我的网站构建了一个serch引擎,但是我的sql数据库基本上包含2组表(一组用于网站页面,另一组用于文件(.doc等))php:使用mysql结果合并数组并将其排序为数组键

我得到了popluate工作,我得到它返回页面的结果,现在我想搜索页面和文件,我想出了运行2 querys(因为),然后将这两个结果数组合并为一个,然后通过querys中添加的“发生”对它们进行排序。但输出不匹配输入数组。无论如何一些代码来给你点工作过

// Search the DB for pages 
$page_result = mysql_query("SELECT p.page_url AS url, COUNT(*) AS occurrences FROM page p, word w, occurrence o WHERE p.page_id = o.page_id AND w.page_word_id = o.page_word_id AND w.word_word LIKE '' '" . $stemmed_string . "' '%' GROUP BY p.page_id ORDER BY occurrences DESC") // LIMIT " . $results . "") 
or die("Invalid query: " . mysql_error()); 

// Search the DB for files 
$file_result = mysql_query("SELECT f.file_url AS url, COUNT(*) AS occurrences FROM files f, filenames fn, fileoccurrence fo WHERE f.file_id = fo.file_id AND fn.file_word_id = fo.file_word_id AND fn.file_word LIKE '' '" . $stemmed_string . "' '%' GROUP BY f.file_id ORDER BY occurrences DESC") 
or die ("Invalid query: " . mysql_error()); 

$page_array = mysql_fetch_array($page_result); 
$file_array = mysql_fetch_array($file_result); 


$results = array_merge((array)$page_array, (array)$file_array); 

搜索项(a)中使用var的输出转储看起来像这样

阵列(4){[0] =>串( 33)“/index.php?page=footy_tipping.htm”[“url”] => string(33)“/index.php?page=footy_tipping.htm”[1] => string(4)“1272”[ (4){/ 0} => string(43)“/files/forms/misc/Adjustment%20TEMPLATE.xls”[“url”] => string(4)“1272”}

array ] => string(43)“/files/forms/misc/Adjustment%20TEMPLATE.xls”[1] => string(1)“2”[“occurrences”] => string(1) “2”}

array(6){[0] => string(33)“/index.php?page=footy_tipping.htm”[“url”] => string(43)“/ files/forms /misc/Adjustment%20TEMPLATE.xls“[1] => string(4)”1272“[”occurrences“] => string(1)”2“[2] => string(43)”/ files/forms /杂项/调整%20TEMPLATE.xls” [3] =>串(1) “2”}

它们进行排序一样的变量上述

具有手动和没有在查找围绕真有帮助想出了如何通过key => value(例如sort($ results ['occurrence'],DESC)对数组进行排序)

任何帮助将appreaciated感谢家伙:)

回答

1

如何在SQL中做它作为联合?

SELECT * FROM ( 
    SELECT p.page_url AS url, 
     COUNT(*) AS occurrences 
    FROM page p, word w, occurrence o 
    WHERE p.page_id = o.page_id 
     AND w.page_word_id = o.page_word_id 
     AND w.word_word LIKE '' '" . $stemmed_string . "' '%' 
    GROUP BY p.page_id 
    UNION 
    SELECT f.file_url AS url, 
     COUNT(*) AS occurrences 
    FROM files f, filenames fn, fileoccurrence fo 
    WHERE f.file_id = fo.file_id 
     AND fn.file_word_id = fo.file_word_id 
     AND fn.file_word LIKE '' '" . $stemmed_string . "' '%' 
    GROUP BY f.file_id 
) t 
ORDER BY occurrences DESC 
+0

返回“无效的查询时出现错误:每个派生的表必须有其自己的别名“ – cyclobs

+0

想象错误,最后一行”ORDER BY occurrences DESC“需要是”t ORDER BY occurrences DESC“。感谢您的帮助 – cyclobs

+0

没问题。我修复了我的解决方案以添加别名。 –

0

你看看array_multisort?你将不得不这样做:

<?php 
// Obtain a list of columns 
foreach ($results as $key => $row) { 
    $occurrences[$key] = $row['occurrences']; 
} 

// Sort the data with volume descending, edition ascending 
// Add $data as the last parameter, to sort by the common key 
array_multisort($occurrences, SORT_DESC, $results); 
?> 
+0

我试过,但,输出结果将是相同的页面(如果有3个结果这将会是第一个结果3次) – cyclobs