starhawk85 Posted May 9, 2012 Share Posted May 9, 2012 okay so i have my search results and i wanted to know how i can get them to display in alpha numeric order. here is the reults code. <?php if (isset($qry_results)) { ?> <div id="results"> <!-- results listing --> <?php foreach ($qry_results as $_key => $_row){ $last_domain = $domain_name; extract($_row); if ($show_query_scores == 0) { $weight = ''; } else { $weight = "[$weight%]"; } ?> <?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?> <div class="idented"> <?php }?> <b><?php print $num?>.</b> <?php print $weight?> <a href="<?php print $url?>" class="title"> <?php print ($title?$title:$sph_messages['Untitled'])?></a><br/> <div class="description"><?php print $fulltxt?></div> <?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?> [ <a href="<?php print 'search.php?query='.quote_replace(addmarks($query)).'&search=1&results='.$results_per_page.'&domain='.$domain_name?>">More results from <?php print $domain_name?></a> ] </div class="idented"> <?php }?> <br/> <?php }?> </div> <?php }?> Thanks in advance! Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 9, 2012 Share Posted May 9, 2012 Not sure exactly what you mean but you might look at natsort Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 9, 2012 Author Share Posted May 9, 2012 mo that would be pre defined. I clearly stated that i wanted to have search results displayed in alpha numeric order. If you don't understand please ask for clarification, otherwise don't respond. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted May 9, 2012 Share Posted May 9, 2012 mo that would be pre defined. I clearly stated that i wanted to have search results displayed in alpha numeric order. If you don't understand please ask for clarification, otherwise don't respond. Well it's your lucky day: There is no such thing as "alphanumeric order." You'll have to clarify. Natural order? ASCII order? Give examples. You could also order your query Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 9, 2012 Share Posted May 9, 2012 mo that would be pre defined. I clearly stated that i wanted to have search results displayed in alpha numeric order. If you don't understand please ask for clarification, otherwise don't respond. You know, ManiacDan is probably the best person to have replied next. I suggest you read all the links in his signature. They are all good. And as he stated, "alpha numeric" is not clear at all, neither is your argument that what I suggested is "pre defined". Seriously. Read those links. Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 9, 2012 Author Share Posted May 9, 2012 mo that would be pre defined. I clearly stated that i wanted to have search results displayed in alpha numeric order. If you don't understand please ask for clarification, otherwise don't respond. Well it's your lucky day: There is no such thing as "alphanumeric order." You'll have to clarify. Natural order? ASCII order? Give examples. You could also order your query Very clear. When i search i am able to view my results. When viewing my results they read g z e a m c I would like it if my results would show in the following manner... a c e g m z PHP code provided above. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 9, 2012 Share Posted May 9, 2012 There's no numbers in your example. You either want to sort it in your query using ORDER BY column_name or put the results in an array and sort it, probably using natsort. I think the query sort is better but it does depend on what type the column is. Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 9, 2012 Author Share Posted May 9, 2012 mo that would be pre defined. I clearly stated that i wanted to have search results displayed in alpha numeric order. If you don't understand please ask for clarification, otherwise don't respond. You know, ManiacDan is probably the best person to have replied next. I suggest you read all the links in his signature. They are all good. And as he stated, "alpha numeric" is not clear at all, neither is your argument that what I suggested is "pre defined". Seriously. Read those links. Im not here to argue with you nore make any issues. as each time i have signed on for help i have received a "i don't understand but here is a link" from you. I wish not to continue this quarrel any longer good day to you ma'am Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 9, 2012 Share Posted May 9, 2012 Wow, is this 15th century England all of the sudden? Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 9, 2012 Author Share Posted May 9, 2012 There's no numbers in your example. You either want to sort it in your query using ORDER BY column_name or put the results in an array and sort it, probably using natsort. I think the query sort is better but it does depend on what type the column is. it is just that an example. to clerify further. Very clear. When i search i am able to view my results. When viewing my results they read g 2 z e a 9 m c I would like it if my results would show in the following manner... a c e g m z 2 9 or 2 9 a c e g m z PHP code provided above. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted May 10, 2012 Share Posted May 10, 2012 Your question, from what I can figure out, is that you want your query sorted using standard natural sort. Your answer, as you've already been given, is an "ORDER BY" clause in your query. You could also use usort or array_multisort in PHP, but the answer is "order your query." Since you have not clarified anything at all, including what database you're using, that's all the help I can give. Also, reading the links on how to post a good question will help you post questions that don't turn into 2-page arguments about what you mean. Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 10, 2012 Author Share Posted May 10, 2012 so then something like this ? $title = mysql_query("SELECT * FROM title ORDER BY title ASC") Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 10, 2012 Share Posted May 10, 2012 Did you try it? Is title the name of the column you want to sort on? Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 10, 2012 Author Share Posted May 10, 2012 Did you try it? Is title the name of the column you want to sort on? yes i have tried it in a few manners. <?php foreach ($qry_results as $_key => $_row){ $last_domain = $domain_name; extract($_row); if ($show_query_scores == 0) { $weight = ''; } else { $weight = "[$weight%]"; } ?> <?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?> <div class="idented"> <?php }?> <b><?php print $num?>.</b> <?php print $weight?> <a href="<?php print $url?>" class="title"> [code]<?php print ($title?$title:$sph_messages['Untitled'])?></a><? $title = mysql_query("SELECT * FROM title ORDER BY title ASC") ?> <br/> <div class="description"><?php print $fulltxt?></div>[/code] but looking at all this and very little of what i know im pretty sure this is not the right way to go about it.. i am confused, I was able to sort the second url by asc by changing $title to $ulr Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 10, 2012 Share Posted May 10, 2012 You need to do it on the query that gets you your $qry_results array. Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 10, 2012 Author Share Posted May 10, 2012 thanks so i tied that out and im getting the following Fatal error: Can't use function return value in write context in ../../../../results.html on line 55 <?php if ($total_results != 0 && $from <= $to){?> <div id ="result_report"> <?php $result = $sph_messages['Results']; $result = str_replace ('%from', $from, $result); $result = str_replace ('%to', $to, $result); $result = str_replace ('%all', $total_results, $result); $matchword = $sph_messages["matches"]; if ($total_results== 1) { $matchword= $sph_messages["match"]; } else { $matchword= $sph_messages["matches"]; } $result = str_replace ('%matchword', $matchword, $result); $result = str_replace ('%secs', $time, $result); echo $result; ?> </div> <?php }?> [b][b]<?php if (isset($qry_results ("SELECT * FROM title ORDER BY title ASC"))) { ?>[/b][/b] <div id="results"> <!-- results listing --> Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted May 10, 2012 Share Posted May 10, 2012 You're making this code worse instead of better. Un-do everything you've done and start from the beginning. somewhere in your code that you have not yet shown us, there is a query that generates $total_results. That query, and only that query, needs to have and ORDER BY clause. Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 10, 2012 Author Share Posted May 10, 2012 here is the whole code. <br/> <?php extract($search_results); ?> <?php if ($search_results['did_you_mean']){?> <div id="did_you_mean"> <?php echo $sph_messages['DidYouMean'];?>: <a href="<?php print 'search.php?query='.quote_replace(addmarks($search_results['did_you_mean'])).'&search=1'?>"><?php print $search_results['did_you_mean_b']; ?></a>? </div> <?php }?> <?php if ($search_results['ignore_words']){?> <div id="common_report"> <?php while ($thisword=each($ignore_words)) { $ignored .= " ".$thisword[1]; } $msg = str_replace ('%ignored_words', $ignored, $sph_messages["ignoredWords"]); echo $msg; ?> </div> <?php }?> <?php if ($search_results['total_results']==0){?> <div id ="result_report"> <?php $msg = str_replace ('%query', $ent_query, $sph_messages["noMatch"]); echo $msg; ?> </div> <?php }?> <?php if ($total_results != 0 && $from <= $to){?> <div id ="result_report"> <?php $result = $sph_messages['Results']; $result = str_replace ('%from', $from, $result); $result = str_replace ('%to', $to, $result); $result = str_replace ('%all', $total_results, $result); $matchword = $sph_messages["matches"]; if ($total_results== 1) { $matchword= $sph_messages["match"]; } else { $matchword= $sph_messages["matches"]; } $result = str_replace ('%matchword', $matchword, $result); $result = str_replace ('%secs', $time, $result); echo $result; ?> </div> <?php }?> <?php if (isset($qry_results)) { ?> <div id="results"> <!-- results listing --> <?php foreach ($qry_results as $_key => $_row){ $last_domain = $domain_name; extract($_row); if ($show_query_scores == 0) { $weight = ''; } else { $weight = "[$weight%]"; } ?> <?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?> <div class="idented"> <?php }?> <b><?php print $num?>.</b> <?php print $weight?> <a href="<?php print $url?>" class="title"> <?php print ($title?$title:$sph_messages['Untitled'])?></a><br/> <div class="description"><?php print $fulltxt?></div> <div class="url"><?php print $url2?> - <?php print $page_size?></div> <?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?> [ <a href="<?php print 'search.php?query='.quote_replace(addmarks($query)).'&search=1&results='.$results_per_page.'&domain='.$domain_name?>">More results from <?php print $domain_name?></a> ] </div class="idented"> <?php }?> <br/> <?php }?> </div> <?php }?> <!-- links to other result pages--> <?php if (isset($other_pages)) { if ($adv==1) { $adv_qry = "&adv=1"; } if ($type != "") { $type_qry = "&type=$type"; } ?> <div id="other_pages"> <?php print $sph_messages["Result page"]?>: <?php if ($start >1){?> <a href="<?php print 'search.php?query='.quote_replace(addmarks($query)).'&start='.$prev.'&search=1&results='.$results_per_page.$type_qry.$adv_qry.'&domain='.$domain?>"><?php print $sph_messages['Previous']?></a> <?php }?> <?php foreach ($other_pages as $page_num) { if ($page_num !=$start){?> <a href="<?php print 'search.php?query='.quote_replace(addmarks($query)).'&start='.$page_num.'&search=1&results='.$results_per_page.$type_qry.$adv_qry.'&domain='.$domain?>"><?php print $page_num?></a> <?php } else {?> <b><?php print $page_num?></b> <?php }?> <?php }?> <?php if ($next <= $pages){?> <a href="<?php print 'search.php?query='.quote_replace(addmarks($query)).'&start='.$next.'&search=1&results='.$results_per_page.$type_qry.$adv_qry.'&domain='.$domain?>"><?php print $sph_messages['Next']?></a> <?php }?> </div> <?php }?> <div class="divline"> </div> where would i place it? I will attempt a few tries Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 10, 2012 Share Posted May 10, 2012 There is no query in there. Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 10, 2012 Author Share Posted May 10, 2012 might you be talking about this? <?php require_once ('../login/includes/config.inc.php'); $page_title = 'YOUR PAGE TITLE GOES HERE'; // Start output buffering: ob_start(); // Initialize a session: session_start(); // Check for a $page_title value: if (!isset($page_title)) { $page_title = 'User Registration'; } // If no first_name session variable exists, redirect the user: if (!isset($_SESSION['first_name'])) { $url = BASE_URL . '../login/index.php'; // Define the URL. ob_end_clean(); // Delete the buffer. header("Location: $url"); exit(); // Quit the script. } ?> <?php //error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING); error_reporting(E_ALL); $include_dir = "./include"; include ("$include_dir/commonfuncs.php"); //extract(getHttpVars()); if (isset($_GET['query'])) $query = $_GET['query']; if (isset($_GET['search'])) $search = $_GET['search']; if (isset($_GET['domain'])) $domain = $_GET['domain']; if (isset($_GET['type'])) $type = $_GET['type']; if (isset($_GET['catid'])) $catid = $_GET['catid']; if (isset($_GET['category'])) $category = $_GET['category']; if (isset($_GET['results'])) $results = $_GET['results']; if (isset($_GET['start'])) $start = $_GET['start']; if (isset($_GET['adv'])) $adv = $_GET['adv']; $include_dir = "./include"; $template_dir = "./templates"; $settings_dir = "./settings"; $language_dir = "./languages"; require_once("$settings_dir/database.php"); require_once("$language_dir/en-language.php"); require_once("$include_dir/searchfuncs.php"); require_once("$include_dir/categoryfuncs.php"); include "$settings_dir/conf.php"; include "$template_dir/$template/header.html"; include "$language_dir/$language-language.php"; if ($type != "or" && $type != "and" && $type != "phrase") { $type = "and"; } if (preg_match("/[^a-z0-9-.]+/", $domain)) { $domain=""; } if ($results != "") { $results_per_page = $results; } if (get_magic_quotes_gpc()==1) { $query = stripslashes($query); } if (!is_numeric($catid)) { $catid = ""; } if (!is_numeric($category)) { $category = ""; } if ($catid && is_numeric($catid)) { $tpl_['category'] = sql_fetch_all('SELECT category FROM '.$mysql_table_prefix.'categories WHERE category_id='.(int)$_REQUEST['catid']); } $count_level0 = sql_fetch_all('SELECT count(*) FROM '.$mysql_table_prefix.'categories WHERE parent_num=0'); $has_categories = 0; if ($count_level0) { $has_categories = $count_level0[0][0]; } require_once("$template_dir/$template/search_form.html"); function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } <?php } function saveToLog ($query, $elapsed, $results) { global $mysql_table_prefix; if ($results =="") { $results = 0; } $query = "insert into ".$mysql_table_prefix."query_log (query, time, elapsed, results) values ('$query', now(), '$elapsed', '$results')"; mysql_query($query); echo mysql_error(); } switch ($search) { case 1: if (!isset($results)) { $results = ""; } $search_results = get_search_results($query, $start, $category, $type, $results, $domain); require("$template_dir/$template/search_results.html"); break; default: if ($show_categories) { if ($_REQUEST['catid'] && is_numeric($catid)) { $cat_info = get_category_info($catid); } else { $cat_info = get_categories_view(); } require("$template_dir/$template/categories.html"); } break; } include "$template_dir/$template/footer.html"; ?> <?php // Flush the buffered output. ob_end_flush(); ?> Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted May 10, 2012 Share Posted May 10, 2012 This line: extract($search_results); That line tells me that somewhere above this code (or in a file that includes this code) there is a variable called $search_results. That variable is the result of (you guessed it), the search. The search is the query. That query is what you need to change. You've been in the wrong file this whole time. Also, extract() is bad practice and lazy. Why? Because you end up not knowing what variables and files you're supposed to be looking at, because you can't tell what the variable names are supposed to be. Exactly as what happened here. Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 10, 2012 Author Share Posted May 10, 2012 okay so here is what i have. <?php require_once ('../login/includes/config.inc.php'); $page_title = 'YOUR PAGE TITLE GOES HERE'; // Start output buffering: ob_start(); // Initialize a session: session_start(); // Check for a $page_title value: if (!isset($page_title)) { $page_title = 'User Registration'; } // If no first_name session variable exists, redirect the user: if (!isset($_SESSION['first_name'])) { $url = BASE_URL . '../login/index.php'; // Define the URL. ob_end_clean(); // Delete the buffer. header("Location: $url"); exit(); // Quit the script. } ?> <?php //error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING); error_reporting(E_ALL); $include_dir = "./include"; include ("$include_dir/commonfuncs.php"); //extract(getHttpVars()); if (isset($_GET['query'])) $query = $_GET['query']; if (isset($_GET['search'])) $search = $_GET['search']; if (isset($_GET['domain'])) $domain = $_GET['domain']; if (isset($_GET['type'])) $type = $_GET['type']; if (isset($_GET['catid'])) $catid = $_GET['catid']; if (isset($_GET['category'])) $category = $_GET['category']; if (isset($_GET['results'])) $results = $_GET['results']; if (isset($_GET['start'])) $start = $_GET['start']; if (isset($_GET['adv'])) $adv = $_GET['adv']; $include_dir = "./include"; $template_dir = "./templates"; $settings_dir = "./settings"; $language_dir = "./languages"; require_once("$settings_dir/database.php"); require_once("$language_dir/en-language.php"); require_once("$include_dir/searchfuncs.php"); require_once("$include_dir/categoryfuncs.php"); include "$settings_dir/conf.php"; include "$template_dir/$template/header.html"; include "$language_dir/$language-language.php"; if ($type != "or" && $type != "and" && $type != "phrase") { $type = "and"; } if (preg_match("/[^a-z0-9-.]+/", $domain)) { $domain=""; } if ($results != "") { $results_per_page = $results; } if (get_magic_quotes_gpc()==1) { $query = stripslashes($query); } if (!is_numeric($catid)) { $catid = ""; } if (!is_numeric($category)) { $category = ""; } if ($catid && is_numeric($catid)) { $tpl_['category'] = sql_fetch_all('SELECT category FROM '.$mysql_table_prefix.'categories WHERE category_id='.(int)$_REQUEST['catid']); } $count_level0 = sql_fetch_all('SELECT count(*) FROM '.$mysql_table_prefix.'categories WHERE parent_num=0'); $has_categories = 0; if ($count_level0) { $has_categories = $count_level0[0][0]; } require_once("$template_dir/$template/search_form.html"); function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } <?php } function saveToLog ($query, $elapsed, $results) { global $mysql_table_prefix; if ($results =="") { $results = 0; } $query = "insert into ".$mysql_table_prefix."query_log (query, time, elapsed, results) values ('$query', now(), '$elapsed', '$results')"; mysql_query($query); echo mysql_error(); } switch ($search) { case 1: if (!isset($results)) { $results = ""; } $search_results = get_search_results($query, $start, $category, $type, $results, $domain); require("$template_dir/$template/search_results.html"); break; default: if ($show_categories) { if ($_REQUEST['catid'] && is_numeric($catid)) { $cat_info = get_category_info($catid); } else { $cat_info = get_categories_view(); } require("$template_dir/$template/categories.html"); } break; } include "$template_dir/$template/footer.html"; ?> <?php // Flush the buffered output. ob_end_flush(); ?> <br/> <?php extract($search_results); ?> <?php if ($search_results['did_you_mean']){?> <div id="did_you_mean"> <?php echo $sph_messages['DidYouMean'];?>: <a href="<?php print 'search.php?query='.quote_replace(addmarks($search_results['did_you_mean'])).'&search=1'?>"><?php print $search_results['did_you_mean_b']; ?></a>? </div> <?php }?> <?php if ($search_results['ignore_words']){?> <div id="common_report"> <?php while ($thisword=each($ignore_words)) { $ignored .= " ".$thisword[1]; } $msg = str_replace ('%ignored_words', $ignored, $sph_messages["ignoredWords"]); echo $msg; ?> </div> <?php }?> <?php if ($search_results['total_results']==0){?> <div id ="result_report"> <?php $msg = str_replace ('%query', $ent_query, $sph_messages["noMatch"]); echo $msg; ?> </div> <?php }?> <?php if ($total_results != 0 && $from <= $to){?> <div id ="result_report"> <?php $result = $sph_messages['Results']; $result = str_replace ('%from', $from, $result); $result = str_replace ('%to', $to, $result); $result = str_replace ('%all', $total_results, $result); $matchword = $sph_messages["matches"]; if ($total_results== 1) { $matchword= $sph_messages["match"]; } else { $matchword= $sph_messages["matches"]; } $result = str_replace ('%matchword', $matchword, $result); $result = str_replace ('%secs', $time, $result); echo $result; ?> </div> <?php }?> <?php if (isset($qry_results)) { ?> <div id="results"> <!-- results listing --> <?php foreach ($qry_results as $_key => $_row){ $last_domain = $domain_name; extract($_row); if ($show_query_scores == 0) { $weight = ''; } else { $weight = "[$weight%]"; } ?> <?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?> <div class="idented"> <?php }?> <b><?php print $num?>.</b> <?php print $weight?> <a href="<?php print $url?>" class="title"> <?php print ($title?$title:$sph_messages['Untitled'])?></a><br/> <div class="description"><?php print $fulltxt?></div> <div class="url"><?php print $url2?> - <?php print $page_size?></div> <?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?> [ <a href="<?php print 'search.php?query='.quote_replace(addmarks($query)).'&search=1&results='.$results_per_page.'&domain='.$domain_name?>">More results from <?php print $domain_name?></a> ] </div class="idented"> <?php }?> <br/> <?php }?> </div> <?php }?> <!-- links to other result pages--> <?php if (isset($other_pages)) { if ($adv==1) { $adv_qry = "&adv=1"; } if ($type != "") { $type_qry = "&type=$type"; } ?> <div id="other_pages"> <?php print $sph_messages["Result page"]?>: <?php if ($start >1){?> <a href="<?php print 'search.php?query='.quote_replace(addmarks($query)).'&start='.$prev.'&search=1&results='.$results_per_page.$type_qry.$adv_qry.'&domain='.$domain?>"><?php print $sph_messages['Previous']?></a> <?php }?> <?php foreach ($other_pages as $page_num) { if ($page_num !=$start){?> <a href="<?php print 'search.php?query='.quote_replace(addmarks($query)).'&start='.$page_num.'&search=1&results='.$results_per_page.$type_qry.$adv_qry.'&domain='.$domain?>"><?php print $page_num?></a> <?php } else {?> <b><?php print $page_num?></b> <?php }?> <?php }?> <?php if ($next <= $pages){?> <a href="<?php print 'search.php?query='.quote_replace(addmarks($query)).'&start='.$next.'&search=1&results='.$results_per_page.$type_qry.$adv_qry.'&domain='.$domain?>"><?php print $sph_messages['Next']?></a> <?php }?> </div> <?php }?> <div class="divline"> </div> placing $title = mysql_query("SELECT * FROM title ORDER BY title ASC") These are the only files that have to infamous $search_results Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted May 10, 2012 Share Posted May 10, 2012 You are NOT supposed to make a whole separate query and just stick it somewhere. You have to find the relevant query and edit it to have an ORDER BY clause. What is the filename of the second file? Where does it fit into the first file? Edit: get_search_results() is the function that generated that variable. Which, again, is not in the code you provided. This is how debugging works. You wanted to sort the results. Go back until you find where the results come from. Quote Link to comment Share on other sites More sharing options...
starhawk85 Posted May 10, 2012 Author Share Posted May 10, 2012 this seems to be the only other file that might have what i am looking for.... error_reporting(E_ALL ^ E_NOTICE); function swap_max (&$arr, $start, $domain) { $pos = $start; $maxweight = $arr[$pos]['weight']; for ($i = $start; $i< count($arr); $i++) { if ($arr[$i]['domain'] == $domain) { $pos = $i; $maxweight = $arr[$i]['weight']; break; } if ($arr[$i]['weight'] > $maxweight) { $pos = $i; $maxweight = $arr[$i]['weight']; } } $temp = $arr[$start]; $arr[$start] = $arr[$pos]; $arr[$pos] = $temp; } function sort_with_domains (&$arr) { $domain = -1; for ($i = 0; $i< count($arr)-1; $i++) { swap_max($arr, $i, $domain); $domain = $arr[$i]['domain']; } } function cmp($a, $b) { if ($a['weight'] == $b['weight']) return 0; return ($a['weight'] > $b['weight']) ? -1 : 1; } function addmarks($a) { $a = preg_replace("/[ ]+/", " ", $a); $a = str_replace(" +", "+", $a); $a = str_replace(" ", "+", $a); return $a; } function makeboollist($a) { global $entities, $stem_words; while ($char = each($entities)) { $a = preg_replace("/".$char[0]."/i", $char[1], $a); } $a = trim($a); $a = preg_replace("/"/i", "\"", $a); $returnWords = array(); //get all phrases $regs = Array(); while (preg_match("/([-]?)\"([^\"]+)\"/", $a, $regs)) { if ($regs[1] == '') { $returnWords['+s'][] = $regs[2]; $returnWords['hilight'][] = $regs[2]; } else { $returnWords['-s'][] = $regs[2]; } $a = str_replace($regs[0], "", $a); } $a = strtolower(preg_replace("/[ ]+/", " ", $a)); // $a = remove_accents($a); $a = trim($a); $words = explode(' ', $a); if ($a=="") { $limit = 0; } else { $limit = count($words); } $k = 0; //get all words (both include and exlude) $includeWords = array(); while ($k < $limit) { if (substr($words[$k], 0, 1) == '+') { $includeWords[] = substr($words[$k], 1); if (!ignoreWord(substr($words[$k], 1))) { $returnWords['hilight'][] = substr($words[$k], 1); if ($stem_words == 1) { $returnWords['hilight'][] = stem(substr($words[$k], 1)); } } } else if (substr($words[$k], 0, 1) == '-') { $returnWords['-'][] = substr($words[$k], 1); } else { $includeWords[] = $words[$k]; if (!ignoreWord($words[$k])) { $returnWords['hilight'][] = $words[$k]; if ($stem_words == 1) { $returnWords['hilight'][] = stem($words[$k]); } } } $k++; } //add words from phrases to includes if (isset($returnWords['+s'])) { foreach ($returnWords['+s'] as $phrase) { $phrase = strtolower(preg_replace("/[ ]+/", " ", $phrase)); $phrase = trim($phrase); $temparr = explode(' ', $phrase); foreach ($temparr as $w) $includeWords[] = $w; } } foreach ($includeWords as $word) { if (!($word =='')) { if (ignoreWord($word)) { $returnWords['ignore'][] = $word; } else { $returnWords['+'][] = $word; } } } return $returnWords; } function ignoreword($word) { global $common; global $min_word_length; global $index_numbers; if ($index_numbers == 1) { $pattern = "[a-z0-9]+"; } else { $pattern = "[a-z]+"; } if (strlen($word) < $min_word_length || (!preg_match("/".$pattern."/i", remove_accents($word))) || ($common[$word] == 1)) { return 1; } else { return 0; } } function search($searchstr, $category, $start, $per_page, $type, $domain) { global $length_of_link_desc,$mysql_table_prefix, $show_meta_description, $merge_site_results, $stem_words, $did_you_mean_enabled ; $possible_to_find = 1; $result = mysql_query("select domain_id from ".$mysql_table_prefix."domains where domain = '$domain'"); if (mysql_num_rows($result)> 0) { $thisrow = mysql_fetch_array($result); $domain_qry = "and domain = ".$thisrow[0]; } else { $domain_qry = ""; } //find all sites that should not be included in the result if (count($searchstr['+']) == 0) { return null; } $wordarray = $searchstr['-']; $notlist = array(); $not_words = 0; while ($not_words < count($wordarray)) { if ($stem_words == 1) { $searchword = addslashes(stem($wordarray[$not_words])); } else { $searchword = addslashes($wordarray[$not_words]); } $wordmd5 = substr(md5($searchword), 0, 1); $query1 = "SELECT link_id from ".$mysql_table_prefix."link_keyword$wordmd5, ".$mysql_table_prefix."keywords where ".$mysql_table_prefix."link_keyword$wordmd5.keyword_id= ".$mysql_table_prefix."keywords.keyword_id and keyword='$searchword'"; $result = mysql_query($query1); while ($row = mysql_fetch_row($result)) { $notlist[$not_words]['id'][$row[0]] = 1; } $not_words++; } //find all sites containing the search phrase $wordarray = $searchstr['+s']; $phrase_words = 0; while ($phrase_words < count($wordarray)) { $searchword = addslashes($wordarray[$phrase_words]); $query1 = "SELECT link_id from ".$mysql_table_prefix."links where fulltxt like '% $searchword%'"; echo mysql_error(); $result = mysql_query($query1); $num_rows = mysql_num_rows($result); if ($num_rows == 0) { $possible_to_find = 0; break; } while ($row = mysql_fetch_row($result)) { $phraselist[$phrase_words]['id'][$row[0]] = 1; } $phrase_words++; } if (($category> 0) && $possible_to_find==1) { $allcats = get_cats($category); $catlist = implode(",", $allcats); $query1 = "select link_id from ".$mysql_table_prefix."links, ".$mysql_table_prefix."sites, ".$mysql_table_prefix."categories, ".$mysql_table_prefix."site_category where ".$mysql_table_prefix."links.site_id = ".$mysql_table_prefix."sites.site_id and ".$mysql_table_prefix."sites.site_id = ".$mysql_table_prefix."site_category.site_id and ".$mysql_table_prefix."site_category.category_id in ($catlist)"; $result = mysql_query($query1); echo mysql_error(); $num_rows = mysql_num_rows($result); if ($num_rows == 0) { $possible_to_find = 0; } while ($row = mysql_fetch_row($result)) { $category_list[$row[0]] = 1; } } //find all sites that include the search word $wordarray = $searchstr['+']; $words = 0; $starttime = getmicrotime(); while (($words < count($wordarray)) && $possible_to_find == 1) { if ($stem_words == 1) { $searchword = addslashes(stem($wordarray[$words])); } else { $searchword = addslashes($wordarray[$words]); } $wordmd5 = substr(md5($searchword), 0, 1); $query1 = "SELECT distinct link_id, weight, domain from ".$mysql_table_prefix."link_keyword$wordmd5, ".$mysql_table_prefix."keywords where ".$mysql_table_prefix."link_keyword$wordmd5.keyword_id= ".$mysql_table_prefix."keywords.keyword_id and keyword='$searchword' $domain_qry order by weight desc"; echo mysql_error(); $result = mysql_query($query1); $num_rows = mysql_num_rows($result); if ($num_rows == 0) { if ($type != "or") { $possible_to_find = 0; break; } } if ($type == "or") { $indx = 0; } else { $indx = $words; } while ($row = mysql_fetch_row($result)) { $linklist[$indx]['id'][] = $row[0]; $domains[$row[0]] = $row[2]; $linklist[$indx]['weight'][$row[0]] = $row[1]; } $words++; } if ($type == "or") { $words = 1; } $result_array_full = Array(); if ($possible_to_find !=0) { if ($words == 1 && $not_words == 0 && $category < 1) { //if there is only one search word, we already have the result $result_array_full = $linklist[0]['weight']; } else { //otherwise build an intersection of all the results $j= 1; $min = 0; while ($j < $words) { if (count($linklist[$min]['id']) > count($linklist[$j]['id'])) { $min = $j; } $j++; } $j = 0; $temp_array = $linklist[$min]['id']; $count = 0; while ($j < count($temp_array)) { $k = 0; //and word counter $n = 0; //not word counter $o = 0; //phrase word counter $weight = 1; $break = 0; while ($k < $words && $break== 0) { if ($linklist[$k]['weight'][$temp_array[$j]] > 0) { $weight = $weight + $linklist[$k]['weight'][$temp_array[$j]]; } else { $break = 1; } $k++; } while ($n < $not_words && $break== 0) { if ($notlist[$n]['id'][$temp_array[$j]] > 0) { $break = 1; } $n++; } while ($o < $phrase_words && $break== 0) { if ($phraselist[$n]['id'][$temp_array[$j]] != 1) { $break = 1; } $o++; } if ($break== 0 && $category > 0 && $category_list[$temp_array[$j]] != 1) { $break = 1; } if ($break == 0) { $result_array_full[$temp_array[$j]] = $weight; $count ++; } $j++; } } } $end = getmicrotime()- $starttime; if ((count($result_array_full) == 0 || $possible_to_find == 0) && $did_you_mean_enabled == 1) { reset ($searchstr['+']); foreach ($searchstr['+'] as $word) { $word = addslashes($word); $result = mysql_query("select keyword from ".$mysql_table_prefix."keywords where soundex(keyword) = soundex('$word')"); $max_distance = 100; $near_word =""; while ($row=mysql_fetch_row($result)) { $distance = levenshtein($row[0], $word); if ($distance < $max_distance && $distance <4) { $max_distance = $distance; $near_word = $row[0]; } } if ($near_word != "" && $word != $near_word) { $near_words[$word] = $near_word; } } $res['did_you_mean'] = $near_words; return $res; } if (count($result_array_full) == 0) { return null; } arsort ($result_array_full); if ($merge_site_results == 1 && $domain_qry == "") { while (list($key, $value) = each($result_array_full)) { if (!isset($domains_to_show[$domains[$key]])) { $result_array_temp[$key] = $value; $domains_to_show[$domains[$key]] = 1; } else if ($domains_to_show[$domains[$key]] == 1) { $domains_to_show[$domains[$key]] = Array ($key => $value); } } } else { $result_array_temp = $result_array_full; } while (list($key, $value) = each ($result_array_temp)) { $result_array[$key] = $value; if (isset ($domains_to_show[$domains[$key]]) && $domains_to_show[$domains[$key]] != 1) { list ($k, $v) = each($domains_to_show[$domains[$key]]); $result_array[$k] = $v; } } $results = count($result_array); $keys = array_keys($result_array); $maxweight = $result_array[$keys[0]]; for ($i = ($start -1)*$per_page; $i <min($results, ($start -1)*$per_page + $per_page) ; $i++) { $in[] = $keys[$i]; } if (!is_array($in)) { $res['results'] = $results; return $res; } $inlist = implode(",", $in); if ($length_of_link_desc == 0) { $fulltxt = "fulltxt"; } else { $fulltxt = "substring(fulltxt, 1, $length_of_link_desc)"; } $query1 = "SELECT distinct link_id, url, title, description, $fulltxt, size FROM ".$mysql_table_prefix."links WHERE link_id in ($inlist)"; $result = mysql_query($query1); echo mysql_error(); $i = 0; while ($row = mysql_fetch_row($result)) { $res[$i]['title'] = $row[2]; $res[$i]['url'] = $row[1]; if ($row[3] != null && $show_meta_description == 1) $res[$i]['fulltxt'] = $row[3]; else $res[$i]['fulltxt'] = $row[4]; $res[$i]['size'] = $row[5]; $res[$i]['weight'] = $result_array[$row[0]]; $dom_result = mysql_query("select domain from ".$mysql_table_prefix."domains where domain_id='".$domains[$row[0]]."'"); $dom_row = mysql_fetch_row($dom_result); $res[$i]['domain'] = $dom_row[0]; $i++; } if ($merge_site_results && $domain_qry == "") { sort_with_domains($res); } else { usort($res, "cmp"); } echo mysql_error(); $res['maxweight'] = $maxweight; $res['results'] = $results; return $res; /**/ } function get_search_results($query, $start, $category, $searchtype, $results, $domain) { global $sph_messages, $results_per_page, $links_to_next, $show_query_scores, $mysql_table_prefix, $desc_length; if ($results != "") { $results_per_page = $results; } if ($searchtype == "phrase") { $query=str_replace('"','',$query); $query = "\"".$query."\""; } $starttime = getmicrotime(); // catch " if only one time entered if (substr_count($query,'"')==1){ $query=str_replace('"','',$query); } $words = makeboollist($query); $ignorewords = $words['ignore']; $full_result['ignore_words'] = $words['ignore']; if ($start==0) $start=1; $result = search($words, $category, $start, $results_per_page, $searchtype, $domain); $query= stripslashes($query); $entitiesQuery = htmlspecialchars($query); $full_result['ent_query'] = $entitiesQuery; $endtime = getmicrotime() - $starttime; $rows = $result['results']; $time = round($endtime*100)/100; $full_result['time'] = $time; $did_you_mean = ""; if (isset($result['did_you_mean'])) { $did_you_mean_b=$entitiesQuery; $did_you_mean=$entitiesQuery; while (list($key, $val) = each($result['did_you_mean'])) { if ($key != $val) { $did_you_mean_b = str_replace($key, "<b>$val</b>", $did_you_mean_b); $did_you_mean = str_replace($key, "$val", $did_you_mean); } } } $full_result['did_you_mean'] = $did_you_mean; $full_result['did_you_mean_b'] = $did_you_mean_b; $matchword = $sph_messages["matches"]; if ($rows == 1) { $matchword= $sph_messages["match"]; } $num_of_results = count($result) - 2; $full_result['num_of_results'] = $num_of_results; if ($start < 2) saveToLog(addslashes($query), $time, $rows); $from = ($start-1) * $results_per_page+1; $to = min(($start)*$results_per_page, $rows); $full_result['from'] = $from; $full_result['to'] = $to; $full_result['total_results'] = $rows; if ($rows>0) { $maxweight = $result['maxweight']; $i = 0; while ($i < $num_of_results && $i < $results_per_page) { $title = $result[$i]['title']; $url = $result[$i]['url']; $fulltxt = $result[$i]['fulltxt']; $page_size = $result[$i]['size']; $domain = $result[$i]['domain']; if ($page_size!="") $page_size = number_format($page_size, 1)."kb"; $txtlen = strlen($fulltxt); if ($txtlen > $desc_length) { $places = array(); foreach($words['hilight'] as $word) { $tmp = strtolower($fulltxt); $found_in = strpos($tmp, $word); $sum = -strlen($word); while (!($found_in =='')) { $pos = $found_in+strlen($word); $sum += $pos; //FIX!! $tmp = substr($tmp, $pos); $places[] = $sum; $found_in = strpos($tmp, $word); } } sort($places); $x = 0; $begin = 0; $end = 0; while(list($id, $place) = each($places)) { while ($places[$id + $x] - $place < $desc_length && $x+$id < count($places) && $place < strlen($fulltxt) -$desc_length) { $x++; $begin = $id; $end = $id + $x; } } $begin_pos = max(0, $places[$begin] - 30); $fulltxt = substr($fulltxt, $begin_pos, $desc_length); if ($places[$begin] > 0) { $begin_pos = strpos($fulltxt, " "); } $fulltxt = substr($fulltxt, $begin_pos, $desc_length); $fulltxt = substr($fulltxt, 0, strrpos($fulltxt, " ")); $fulltxt = $fulltxt; } $weight = number_format($result[$i]['weight']/$maxweight*100, 2); if ($title=='') $title = $sph_messages["Untitled"]; $regs = Array(); if (strlen($title) > 80) { $title = substr($title, 0,76)."..."; } foreach($words['hilight'] as $change) { while (preg_match("/[^\>](".$change.")[^\<]/i", " ".$title." ", $regs)) { $title = preg_replace("/".$regs[1]."/i", "<b>".$regs[1]."</b>", $title); } while (preg_match("/[^\>](".$change.")[^\<]/i", " ".$fulltxt." ", $regs)) { $fulltxt = preg_replace("/".$regs[1]."/i", "<b>".$regs[1]."</b>", $fulltxt); } $url2 = $url; while (preg_match("/[^\>](".$change.")[^\<]/i", $url2, $regs)) { $url2 = preg_replace("/".$regs[1]."/i", "<b>".$regs[1]."</b>", $url2); } } $num = $from + $i; $full_result['qry_results'][$i]['num'] = $num; $full_result['qry_results'][$i]['weight'] = $weight; $full_result['qry_results'][$i]['url'] = $url; $full_result['qry_results'][$i]['title'] = $title; $full_result['qry_results'][$i]['fulltxt'] = $fulltxt; $full_result['qry_results'][$i]['url2'] = $url2; $full_result['qry_results'][$i]['page_size'] = $page_size; $full_result['qry_results'][$i]['domain_name'] = $domain; $i++; } } $pages = ceil($rows / $results_per_page); $full_result['pages'] = $pages; $prev = $start - 1; $full_result['prev'] = $prev; $next = $start + 1; $full_result['next'] = $next; $full_result['start'] = $start; $full_result['query'] = $entitiesQuery; if ($from <= $to) { $firstpage = $start - $links_to_next; if ($firstpage < 1) $firstpage = 1; $lastpage = $start + $links_to_next; if ($lastpage > $pages) $lastpage = $pages; for ($x=$firstpage; $x<=$lastpage; $x++) $full_result['other_pages'][] = $x; } return $full_result; } ?> so really i guess my better chance of having this work would be more.. $result = mysql_query("SELECT * FROM url ORDER BY title ASC") Quote Link to comment Share on other sites More sharing options...
xyph Posted May 10, 2012 Share Posted May 10, 2012 I think this is getting to the point where this gets moved to 3rd Party Scripts. I'm worried the OP doesn't understand the code he/she has posted. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.