Wordpress Search Results in Order - php

One of my clients websites, www.kevinsplants.co.uk is not showing the search results in alphabetical order, how do I go about ordering the results in alphabetical order?
We are using the Shopp plugin and I believe its that plugin that is generating the results!
Cheers, Brad
case "orderby-list":
if (isset($Shopp->Category->controls)) return false;
if (isset($Shopp->Category->smart)) return false;
$menuoptions = Category::sortoptions();
$title = "";
$string = "";
$default = $Shopp->Settings->get('default_product_order');
if (empty($default)) $default = "title";
if (isset($options['default'])) $default = $options['default'];
if (isset($options['title'])) $title = $options['title'];
if (value_is_true($options['dropdown'])) {
if (isset($Shopp->Cart->data->Category['orderby']))
$default = $Shopp->Cart->data->Category['orderby'];
$string .= $title;
$string .= '<form action="'.esc_url($_SERVER['REQUEST_URI']).'" method="get" id="shopp-'.$Shopp->Category->slug.'-orderby-menu">';
if (!SHOPP_PERMALINKS) {
foreach ($_GET as $key => $value)
if ($key != 'shopp_orderby') $string .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
}
$string .= '<select name="shopp_orderby" class="shopp-orderby-menu">';
$string .= menuoptions($menuoptions,$default,true);
$string .= '</select>';
$string .= '</form>';
$string .= '<script type="text/javascript">';
$string .= "jQuery('#shopp-".$Shopp->Category->slug."-orderby-menu select.shopp-orderby-menu').change(function () { this.form.submit(); });";
$string .= '</script>';
} else {
if (strpos($_SERVER['REQUEST_URI'],"?") !== false)
list($link,$query) = explode("\?",$_SERVER['REQUEST_URI']);
$query = $_GET;
unset($query['shopp_orderby']);
$query = http_build_query($query);
if (!empty($query)) $query .= '&';
foreach($menuoptions as $value => $option) {
$label = $option;
$href = esc_url($link.'?'.$query.'shopp_orderby='.$value);
$string .= '<li>'.$label.'</li>';
}
}
return $string;
break;

http://wordpress.org/extend/plugins/sort-searchresult-by-title/
This is a plug-in that seems to do what you want.

You need to edit the template file of the search page and add to the search query parameters orderby title, as opposed to the default ordering which is by time of publication

It is not a good idea to edit the core code. And lucky for you it is not necessary. You can edit/create your own template file collection-search.php and style it any way you want.
There is more info on template files here.

Related

Getting only the first word of a option inside value attribute in html

I am trying to select a previously selected value inside dropdown selection using php. My code is as follows
<?php
$selected_val = "Assistant Professor";
function generateSelect($type = 'text', $name = 'desgntn', $id = 'fac_dsgn', $options = array(), $default) {
$html = '<select type="'.$type.'" name="'.$name.'" id="'.$id.'">';
foreach ($options as $option ) {
if ($option == $default) {
$html .= '<option value='.$option.' selected="selected">'.$option.'</option>';
} else {
$html .= '<option value='.$option.'>'.$option.'</option>';
}
}
$html .= '</select>';
return $html;
}
$list = array("Lecturer", "Assistant Professor", "Associate Professor", "Professor", "Assistant Professor (On Leave)", "Associate Professor (On Leave)", "Professor (On Leave)");
$res = generateSelect('text', 'desgntn', 'fac_dsgn', $list, $selected_val);
echo $res;
?>
But the problem is I am always getting the first word of a multi spaced option inside value attributein option tag of html. for example: I am getting Assistant inside value tag for Assistant Professor. Is there any way so that I can always get the actual value with spaces?
I believe you forgot to enclose the option value in quotes:
it should be something like
$html .= '<option value="'.$option.'" selected="selected">'.$option.'</option>';
(note the double quotes around $option)
Hope it helps

Get certain strings only (PHP)

Text:
TestString
HT-Child1 CLASS-Class1
AnotherString
HT-Child2 CLASS-Class2
HT-Child3 CLASS-Class3
HT-Child4 CLASS-Class4
CLASSOFWEEK-Class
What I have so far: (Inside $display->getExtraHTML() is the text). Could someone guide me towards what I need to do to adapt my code to get the results I want.
<?php
$additionalHTML = explode("\n", $display->getExtraHTML());
$html = "";
$html .= "<ul>";
foreach($additionalHTML as $key => $item){
$html .= "<li>$item</li>";
}
$html .= "</ul>";
echo $html;
?>
I know I can use something like this to get string between, but how do I use it to get all the values i need?
$string = strstr($display->getExtraHTML(), "HT-"); // gets all text from HT
$string = strstr($string, "CLASS-", true); // gets all text before CLASS
Can I use both explode and strstr to get to where I want?
Expect HTML markup:
Expected Result: (Get the values from HT- and CLASS-)
<ul>
<li>Child1 Class1</li>
<li>Child2 Class2</li>
<li>Child3 Class3</li>
<li>Child4 Class4</li>
</ul>
Complete solution with preg_match_all function:
$txt = '
TestString
HT-Child1 CLASS-Class1
AnotherString
HT-Child2 CLASS-Class2
HT-Child3 CLASS-Class3
HT-Child4 CLASS-Class4
CLASSOFWEEK-Class';
preg_match_all('/^HT-(\S+)\s+CLASS-(\S+)/m', $txt, $m);
$html = "<ul>";
if (isset($m[1]) && isset($m[2])){
foreach(array_map(null, $m[1], $m[2]) as $pair){
$html .= "<li>". implode(' ', $pair) ."</li>";
}
}
$html .= "</ul>";
echo $html;
The output (push Run code snippet):
<ul><li>Child1 Class1</li><li>Child2 Class2</li><li>Child3 Class3</li><li>Child4 Class4</li></ul>
Here's the solution
<?php
$str = "TestString
HT-Child1 CLASS-Class1
AnotherString
HT-Child2 CLASS-Class2
HT-Child3 CLASS-Class3
HT-Child4 CLASS-Class4
CLASSOFWEEK-Class";
$additionalHTML = explode("\n", $str);
$html = "";
$html .= "<ul>";
foreach($additionalHTML as $key => $item){
if(substr($item,0,3) == "HT-") {
$i = explode(" ",$item);
$a = substr($i[0],3);
$b = substr($i[1],6);
$html .= "<li>$a"." ". "$b</li>";
}
}
$html .= "</ul>";
echo $html;
Result
Child1 Class1
Child2 Class2
Child3 Class3
Child4 Class4
You may use regex, to find the matching lines and extract required data:
if(preg_match("/HT-([A-Za-z0-9]+) CLASS-([A-Za-z0-9]+)/", $item, $output))
{
$html .= "<li>".implode(" ",array_slice($output,1))."</li>";
}

Alter all a href links in php

Currently working on something where i need to add the UTM tag to all links, got 1/2 minor issues i cant figure out
This is the code im am using, the issue is if a link got a parameter like ?test=test then this refuses to add the utm tags.
The other issue is a minor issue that im not sure would make sence to change, insted of me having to add a url, it could be neat if it added utm tags to ALL a href's by default with out knowing the domain name.
Hope someone can help me out and push me in the right direction.
$url_modifier_domain = preg_quote('add-link.com');
$html_text = preg_replace_callback(
'#((?:https?:)?//'.$url_modifier_domain.'(/[^\'"\#]*)?)(?=[\'"\#])#i',
function($matches){
$url_modifier = 'utm=some&medium=stuff';
if (!isset($matches[2])) return $matches[1]."/?$url_modifier";
$q = strpos($matches[2],'?');
if ($q===false) return $matches[1]."?$url_modifier";
if ($q==strlen($matches[2])-1) return $matches[1].$url_modifier;
return $matches[1]."&$url_modifier";
},
$html);
once detected the urls you can use parse_url() and parse_str() to elaborate the url, add utm and medium and rebuild it without caring too much about the content of the get parameters or the hash:
$url_modifier_domain = preg_quote('add-link.com');
$html_text = preg_replace_callback(
'#((?:https?:)?//'.$url_modifier_domain.'(/[^\'"\#]*)?)(?=[\'"\#])#i',
function ($matches) {
$link = $matches[0];
if (strpos($link, '#') !== false) {
list($link, $hash) = explode('#', $link);
}
$res = parse_url($link);
$result = '';
if (isset($res['scheme'])) {
$result .= $res['scheme'].'://';
}
if (isset($res['host'])) {
$result .= $res['host'];
}
if (isset($res['path'])) {
$result .= $res['path'];
}
if (isset($res['query'])) {
parse_str($res['query'], $res['query']);
} else {
$res['query'] = [];
}
$res['query']['utm'] = 'some';
$res['query']['medium'] = 'stuff';
if (count($res['query']) > 0) {
$result .= '?'.http_build_query($res['query']);
}
if (isset($hash)) {
$result .= '#'.$hash;
}
return $result;
},
$html
);
As you can see, the code is longer but simpler
Edit
I made some change, searching for every href="xxx" inside the text. If the link is not from add-link.com the script will skip it, otherwise he will try to print it in the best way possible
$html = 'blabla a
a
a
a
a
a
a
a
a
a
a
';
$url_modifier_domain = preg_quote('add-link.com');
$html_text = preg_replace_callback(
'/href="([^"]+)"/i',
function ($matches) {
$link = $matches[1];
// ignoring outer links
if(strpos($link,'add-link.com') === false) return 'href="'.$link.'"';
if (strpos($link, '#') !== false) {
list($link, $hash) = explode('#', $link);
}
$res = parse_url($link);
$result = '';
if (isset($res['scheme'])) {
$result .= $res['scheme'].'://';
} else if(isset($res['host'])) {
$result .= '//';
}
if (isset($res['host'])) {
$result .= $res['host'];
}
if (isset($res['path'])) {
$result .= $res['path'];
} else {
$result .= '/';
}
if (isset($res['query'])) {
parse_str($res['query'], $res['query']);
} else {
$res['query'] = [];
}
$res['query']['utm'] = 'some';
$res['query']['medium'] = 'stuff';
if (count($res['query']) > 0) {
$result .= '?'.http_build_query($res['query']);
}
if (isset($hash)) {
$result .= '#'.$hash;
}
return 'href="'.$result.'"';
},
$html
);
var_dump($html_text);

Strange with function to list countries from txt

i have a problem and i can't explain it ,,
first this is my function
function list_countries($id,$name=null,$result=null){
$countries = 'countries.txt';
$selected = '';
echo '<select name="'.$name.'" id="'.$id.'">';
echo '<option disabled>طالب الغد</option>';
if(file_exists($countries)){
if(is_readable($countries)){
$files = file_get_contents($countries);
$files = explode('|',$files);
foreach($files AS $file){
$value = sql_safe($file);
if(strlen($value) < 6){
echo '<option disabled>'.$value.'</option>';
}else{
if($value == $result){
$selected = ' selected="selected" ';
}
echo '<option value="'.$value.'".$selected.'>'.$value.'</option>';
}
}
}else{
echo 'The file is nor readable !';
}
}else{
echo "The file is not exist !";
}
echo '</select>';
}
Now the explain
i have a text file includes a countries names separated with "|"
In this file there is a heading before the countries ,, i mean Like this
U|United Kingdom|United State|UAE etc ..
L|Liberia|Libya etc ..
Now what the function Do is Disabled the Heading , and it's always one character ..
but the strlen function the minimum number that it's give to me is 5 not one .. " This is the first problem
The second one in the $result never equaled the $value and ether i don't know why ??
You need to split twice the file, one for the lines, one for the countries.
Also, since your "country header" is always the first item of each row, you do not need to check using strlen. Just shift out the first item of each row set: that one is the header, the following ones are the countries.
Something like this.
Note that in your code there is a syntax error in the echo that outputs the value, the > symbol is actually outside the quotes.
function list_countries($id,$name=null,$result=null){
$countries = 'countries.txt';
$selected = '';
$text = '<select name="'.$name.'" id="'.$id.'">';
$text .= '<option disabled>ﻁﺎﻠﺑ ﺎﻠﻏﺩ</option>';
if(file_exists($countries)){
if(is_readable($countries)){
$list = file($countries);
foreach($list as $item){
$item = trim($item);
$opts = explode('|', $item);
// The first item is the header.
$text .= "<option disabled>$opts[0]</option>";
array_shift($opts);
foreach($opts as $opt)
{
$value = sql_safe($opt);
$text .= '<option';
if($value == $result)
$text .= ' selected="selected"';
$text .= ' value="'.$value.'"';
$text .= '>'.$value."</option>\n";
}
}
}else{
$text .= "The file is not readable!";
}
}else{
$text .= "The file does not exist!";
}
$text .= '</select>';
return $text;
}
I have slightly modified your code so that the function actually returns the text to be output instead of echoing it; this makes for more reusability. To make the above function behave as yours did, just replace the return with
echo $text;
}
and you're good.

PHP odd string-related performance issue

I have a class that is building some HTML using data stored in an array. There are around 100 items in this array. Each item includes information like company name, a description, and flags for the different programming languages the company supports. I am doing string concatenation as I build the HTML for each item.
I have noticed that performance suddenly takes a huge hit when I append the programming language data. I see the page rendering timer jump from 0.15 secs to ~0.60 secs. This time includes grabbing the same data from the database each time. I can consistently get the performance to jump between these 2 times but commenting/uncommenting the following line of code:
$html .= '<div class="programmingLanguages"><strong>Programming Languages</strong> '.implode(', ', $progLanguagesArray).'</div>';
I've also been able to get the same performance drop by appending a long test string, something like this:
$html .= 'testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest';
What's especially bizarre is that I have another line of code that uses the same 'implode' function and does NOT make any significant difference in performance:
$html .= '<div class="integrationMethods"><strong>Integration Methods:</strong> '.implode(', ', $intMethodsArray).'</div>';
Does anybody have any insight into what might be going on here? I am doing tons of concatenation like this elsewhere in my code and haven't seen anything like this before. At this point, I'm stumped.
Here's the full class:
class DeveloperView {
public static function getHtml($developers) {
$html = '';
$html .= '<div>';
$html .= '<div>';
$count = 0;
foreach ($developers as $developer) {
$url = $developer['attributes']['url'];
$phone = $developer['attributes']['phone'];
$company = $developer['attributes']['desc'];
$active = $developer['attributes']['active'];
$desc = $developer['object_value'];
$intMethodsArray = array();
if ($developer['attributes']['m1']) { $intMethodsArray[] = 'method 1'; }
if ($developer['attributes']['m2']) { $intMethodsArray[] = 'method 2'; }
if ($developer['attributes']['m3']) { $intMethodsArray[] = 'method 3'; }
if ($developer['attributes']['m4']) { $intMethodsArray[] = 'method 4'; }
if ($developer['attributes']['m5']) { $intMethodsArray[] = 'method 5'; }
$progLanguagesArray = array();
if ($developer['attributes']['dotnet']) { $progLanguagesArray[] = '.Net (C# or VB.Net)'; }
if ($developer['attributes']['asp']) { $progLanguagesArray[] = 'Classic ASP'; }
if ($developer['attributes']['cf']) { $progLanguagesArray[] = 'Cold Fusion'; }
if ($developer['attributes']['java']) { $progLanguagesArray[] = 'Java'; }
if ($developer['attributes']['php']) { $progLanguagesArray[] = 'PHP'; }
if ($developer['attributes']['perl']) { $progLanguagesArray[] = 'Perl'; }
if ($developer['attributes']['other']) { $progLanguagesArray[] = 'Other'; }
$html .= '<div class="';
if ($count % 2 == 0) {
$html .= 'listingalt';
} else {
$html .= 'listing';
}
$html .= '">';
$html .= '<div class="developerPhone">'.$phone.'</div>';
$html .= '<a class="ext_link" target="_blank" href="'.$url.'">'.$company.'</a>';
$html .= '<div>';
if (!empty($intMethodsArray)) {
$html .= '<div class="integrationMethods"><strong>Integration Methods:</strong> '.implode(', ', $intMethodsArray).'</div>';
}
if (!empty($progLanguagesArray)) {
$html .= '<div class="programmingLanguages"><strong>Programming Languages</strong> '.implode(', ', $progLanguagesArray).'</div>';
}
$html .= '</div>';
$html .= '<p>'.$desc.'</p>';
$html .= '</div>'."\n";
$count++;
}
$html .= '</div></div>';
return $html;
}
}
Now that I can provide an answer, I'll just post my follow-up comment as the 'answer'...
I did indeed have a 'bug' in my timer, in that it was calculating the end processing time AFTER the echo of the HTML. So the amount of data being sent to the browser was effecting the processing time, where I was expecting to see the time spent processing BEFORE transmitting any data.

Categories