I'm working on this query for a Drupal 7 project:
$group = taxonomy_get_tree(2);
$group_list = "";
$total_countries_list = "";
foreach ($group as $member) {
$countries_list = "";
$id = str_replace(' ', '-', $member->name);
$id = str_replace('/', '-', $id);
$group_list .= '<li><a id="link-' . str_replace(' ', '-', $id) . '" href="#">. ' . $member->name . '</a></li>';
$results = db_query('SELECT n.title AS name, fdfea.field_email_address_value AS email
FROM node AS n
LEFT JOIN field_data_field_email_address AS fdfea ON fdfea.entity_id = n.nid
LEFT JOIN field_data_field_group_of_countries AS fdfgoc ON fdfgoc.field_group_of_countries_tid = :country_taxonomy
WHERE n.nid = fdfgoc.entity_id', array(':country_taxonomy' => $member->tid));
$i = 0;
foreach ($results as $country) {
if ($i % 13 == 0){
$countries_list .= '</ul><ul>';
}
$countries_list .= '<li>' . $country->name . '</li>';
$i++;
}
$countries_list = '<ul>' . $countries_list . '</ul>';
$total_countries_list .= '<div class="countries-list" id="' . $id . '">' . $countries_list . '</div>';
}
$total_countries_list = '<div class="countries-wrapper">' . $total_countries_list . '</div>';
$group_list = '<ul class="big-list">' . $group_list . '</ul>';
return $group_list . $total_countries_list;
I'm using the results of this query to form lists of countries. The query works fine but I also need to sort countries in each group alphabetically. ORDER BY is apparently not the way to do so so how can I do the sorting?
I'm not sure why you say you can't use ORDER BY.
Assuming the country name is in node.title and there is a group id of some sort in the field_data_field_group_of_countries table (I'm just going to guess something like gid), then surely you could use an ORDER BY clause something like this:
ORDER BY fdfgoc.gid, n.title
So it would order first by the country group, and then by country name in each group.
Related
I have this code to display the price.
function edit_selected_variation_price( $data, $product, $variation ) {
$price = $variation->price;
$price_incl_tax = $price + round($price * ( 23 / 100 ), 2);
$price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
$price = number_format($price, 2, ",", ".");
$display_price = '<div><span class="price">';
$display_price .= '<span>Cena netto</span><span class="amount">' . $price .'<small class="woocommerce-price-suffix"> zł</small></span>';
$display_price .= '</div><div>';
$display_price .= '<span>Cena brutto</span><span class="amount">' . $price_incl_tax .'<small class="woocommerce-price-suffix"> zł</small></span>';
$display_price .= '</span></div>';
$data['price_html'] = $display_price;
return $data;
}
add_filter( 'woocommerce_available_variation', 'edit_selected_variation_price', 10, 3);
How to get the maximum price displayed without choosing any variants?
I do not fully understand your question, you want the max price from what prices (incl tax and ...)?
However, when you have different prices in an array (or as loose variables), consider using the PHP max() function:
https://www.php.net/manual/en/function.max.php
That will return the highest value (Hope that's what you're after)
I have a problem with getting shortcode values into variables, you advise please?
This is the code:
class virtual_cheshire_ring
{
public $start_date = '';
public $end_date = '';
public $db_table_participants = '';
public $db_table_log = '';
public function __construct()
{
add_shortcode('virtual_crr', array($this, 'virtual_crr') );
}
public function virtual_crr($atts)
{
//******************************
//** Get shortcode parameters **
//******************************
global $post;
$shortcode_defaults = [ 'start_date' => '2020-01-01',
'end_date' => '2050-00-01',
'db_table_participants' => 'db_table_participants',
'db_table_log' => 'db_table_log',
];
$attributes = array_merge($shortcode_defaults,$atts);
$this->$start_date = $attributes['start_date'];
$this->$end_date = $attributes['end_date'];
$this->$db_table_participants = $attributes['db_table_participants'];
$this->$db_table_log = $attributes['db_table_log'];
var_dump($attributes);
$html = 'start_date = ' . $this->$start_date . ' / ' . $attributes['start_date'] . '<br>';
$html .= 'end_date = ' . $this->$end_date . ' / ' . $attributes['end_date'] . '<br>';
$html .= 'db_table_participants = ' . $this->$db_table_participants . ' / ' . $attributes['db_table_participants'] . "<br>";
$html .= 'db_table_log = ' . $this->$db_table_log . ' / ' . $attributes['db_table_log'] . '<br>';
return $html;
}
}
The shortcode on the webpage is:
[virtual_crr start_date="2020-06-02" end_date="2020-06-30"]
The var_dump($attributes) returns:
array (size=4)
'start_date' => string '2020-06-02' (length=10)
'end_date' => string '2020-06-30' (length=10)
'db_table_participants' => string 'db_table_participants' (length=21)
'db_table_log' => string 'db_table_log' (length=12)
The output on the webpage is:
start_date = db_table_log / 2020-06-02
end_date = db_table_log / 2020-06-30
db_table_participants = db_table_log / db_table_participants
db_table_log = db_table_log / db_table_log
So clearly I'm not understanding something fundamental as the '$this->$xxxx values differ from the $attributes array values, can you advise please?
Many thanks in advance
Alan
I think you need to remove the $ from your param when you access them with $this.
So it would look more like:
<?php
public function virtual_crr($atts)
{
//******************************
//** Get shortcode parameters **
//******************************
global $post;
$shortcode_defaults = [
'start_date' => '2020-01-01',
'end_date' => '2050-00-01',
'db_table_participants' => 'db_table_participants',
'db_table_log' => 'db_table_log',
];
$attributes = array_merge($shortcode_defaults,$atts);
$this->start_date = $attributes['start_date'];
$this->end_date = $attributes['end_date'];
$this->db_table_participants = $attributes['db_table_participants'];
$this->db_table_log = $attributes['db_table_log'];
var_dump($attributes);
$html = 'start_date = ' . $this->start_date . ' / ' . $attributes['start_date'] . '<br>';
$html .= 'end_date = ' . $this->end_date . ' / ' . $attributes['end_date'] . '<br>';
$html .= 'db_table_participants = ' . $this->db_table_participants . ' / ' . $attributes['db_table_participants'] . "<br>";
$html .= 'db_table_log = ' . $this->db_table_log . ' / ' . $attributes['db_table_log'] . '<br>';
return $html;
}
?>
I need some help.
On a Wordpress real estate website, I would like to display the price in XPF next to the price in €.
The price function is:
public static function format_price($price,$html = true){
$return = '';
$currency_code = self::get_general_option('currency');
$currency_symbol = self::get_currency_symbol($currency_code);
$currency_position = self::get_general_option('currency_position');
switch ( $currency_position ) {
case 'left' :
$format = '%1$s%2$s';
break;
case 'right' :
$format = '%2$s%1$s';
break;
case 'left_space' :
$format = '%1$s %2$s';
break;
case 'right_space' :
$format = '%2$s %1$s';
break;
default:
$format = '%1$s%2$s';
}
$thousands_sep = wp_specialchars_decode( stripslashes(self::get_general_option('price_thousand_sep')),ENT_QUOTES);
$decimal_sep = wp_specialchars_decode( stripslashes(self::get_general_option('price_decimal_sep')),ENT_QUOTES);
$num_decimals = self::get_general_option('price_num_decimals');
$price = floatval( $price );
if(!$html) {
return self::number_format( $price, $num_decimals, '.', '', $currency_code );
}
$price = self::number_format( $price, $num_decimals, $decimal_sep, $thousands_sep, $currency_code );
if('text' === $html) {
return sprintf( $format, $currency_symbol, $price );
}
//$price = preg_replace( '/' . preg_quote( self::get_general_option('price_decimal_sep'), '/' ) . '0++$/', '', $price );
$return = '<span class="amount">' . sprintf( $format, $currency_symbol, $price ) . '</span>';
return $return;
}
The conversion rate is : 1€ = 119.33XPF
How can I edit the code to display the price like this for instance :
11933000XPF (100000€)
Thanks.
You have the floatval of the price in euros. If your conversion rate is a fixed value just convert like this:
$price_xpf = $price * 119.33;
The formatting of the price is working in exact analogy as the formatting for the price in euros.
Just for display do the following:
$return = '<span class="amount">'. sprintf( $format,"XPF",$price_xpf ) ."(". sprintf( $format, $currency_symbol, $price ) . ')</span>';
For any other adaption most basic programming skills will be helpful.
I'm using the following code:
$wpdb->get_results("
SELECT * FROM " . $wpdb->prefix . "product_order
WHERE
rel = '" . $post["id"] . "' AND
`range` = '" . $range . "' AND
category = '" . $range . "'
");
echo $wpdb->num_rows;
num_rows returns 1 even though there is no rows in the database?
Any ideas?
The variables I am putting in look fine. so it should be querying correctly.
global $wpdb;
$wpdb->get_results("
SELECT * FROM " . $wpdb->prefix . "product_order
WHERE
rel = '" . $post["id"] . "' AND
`range` = '" . $range . "' AND
category = '" . $range . "'
");
echo $wpdb->num_rows;
Now it returns numbers of rows select from above query and 0 if no row selected.....
if you JUST want the count (maybe for pagination total), it is faster to do:
global $wpdb;
$rows = $wpdb->get_results("
SELECT COUNT(*) as num_rows FROM " . $wpdb->prefix . "product_order
WHERE
rel = '" . $post["id"] . "' AND
`range` = '" . $range . "' AND
category = '" . $range . "'
");
echo $rows[0]->num_rows;
I want to break up this archive list into multiple columns (three columns)...I don't want to use CSS3 for this. I can easily do this with wp_list_categories, but that method doesn't work with wp_get_archives.
Ideally, I'd like to implement a counter to add in tags at an approximate 1/3 count...then I can use CSS for the rest.
CUSTOM ARCHIVE FUNCTION:
function wp_custom_archive($args = '') {
global $wpdb, $wp_locale;
$defaults = array(
'limit' => '',
'format' => 'html', 'before' => '',
'after' => '', 'show_post_count' => false,
'echo' => 1
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
if ( '' != $limit ) {
$limit = absint($limit);
$limit = ' LIMIT '.$limit;
}
// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
$archive_date_format_over_ride = 0;
// options for daily archive (only if you over-ride the general date format)
$archive_day_date_format = 'Y/m/d';
// options for weekly archive (only if you over-ride the general date format)
$archive_week_start_date_format = 'Y/m/d';
$archive_week_end_date_format = 'Y/m/d';
if ( !$archive_date_format_over_ride ) {
$archive_day_date_format = get_option('date_format');
$archive_week_start_date_format = get_option('date_format');
$archive_week_end_date_format = get_option('date_format');
}
//filters
$where = apply_filters('customarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
$join = apply_filters('customarchives_join', "", $r);
$output = '<ul>';
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
$key = md5($query);
$cache = wp_cache_get( 'wp_custom_archive' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( 'wp_custom_archive', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
$year_text = sprintf('<li>%d</li>', $arcresult->year);
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
$output .= get_archives_link($url, $text, $format, $before, $after);
$temp_year = $arcresult->year;
}
}
$output .= '</ul>';
if ( $echo )
echo $output;
else
return $output;
}
CURRENT HTML OUTPUT (shortened):
<ul>
<li>2012</li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/09/' title='September'>September</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/08/' title='August'>August</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/07/' title='July'>July</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/06/' title='June'>June</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/05/' title='May'>May</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/04/' title='April'>April</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/03/' title='March'>March</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/02/' title='February'>February</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/01/' title='January'>January</a></li>
</ul>
If I understand correctly, you would have to do something like this
$output = '<ul class="col1">';
if ( $arcresults ) {
$numarticles = count($arcresults);
$column1 = round($numarticles/3);
$column2 = round($column1*2);
$counter = 0;
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
if($counter == $column1) {
$output .= "</ul><ul class='col2'>";
} elseif($counter == $column2) {
$output .= "</ul><ul class='col3'>";
}
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
$year_text = sprintf('<li>%d</li>', $arcresult->year);
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
$output .= get_archives_link($url, $text, $format, $before, $after);
$temp_year = $arcresult->year;
$counter ++;
}
$output .= '</ul>';
}
This way, when the counter gets to a third of the way, it will add another list, so at the end you will have 3 lists.
Hope this helps.