基本上,我为我的网站构建了一个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感谢家伙:)
返回“无效的查询时出现错误:每个派生的表必须有其自己的别名“ – cyclobs
想象错误,最后一行”ORDER BY occurrences DESC“需要是”t ORDER BY occurrences DESC“。感谢您的帮助 – cyclobs
没问题。我修复了我的解决方案以添加别名。 –