Wordpress $wpdb->get_results and num_rows - wordpress

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;

Related

Show max variable price in shop page - Woocommerce

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)

Incorrect values returned from $this->$xxxx fields

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;
}
?>

Php Excel DateStamd Formatted date

I am reading an xlsx file and the fields are in DateStamd I want to convert them to a date data here I leave my progress
Excel A1 = 43160
Converte date = 01/03/2018
enter code hererequire '../PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
require '../cnx/conection.php';
$objPHPExcel = PHPExcel_IOFactory::load('../../date.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$numRows = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
for ($i = 2; $i <= $numRows; $i++) {
$pm = $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
echo $pm;
$sql = "INSERT INTO regfecha (reg_fecha) VALUES('$pm')";
$result = $mysqli->query($sql);
}
`
here I have another example and I do not know how to apply it
`
DATEVALUE
Converts a date in the form of text to a serial number.`
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
include 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
$worksheet = $objPHPExcel->getActiveSheet();
// Add some data
$testDates = array( '26 March 2012', '29 Feb 2012', 'April 1, 2012', '25/12/2012',
'2012-Oct-31', '5th November', 'January 1st', 'April 2012',
'17-03', '03-2012', '29 Feb 2011', '03-05-07',
'03-MAY-07', '03-13-07',
);
$testDateCount = count($testDates);
for($row = 1; $row <= $testDateCount; ++$row) {
$worksheet->setCellValue('A'.$row, $testDates[$row-1]);
$worksheet->setCellValue('B'.$row, '=DATEVALUE(A'.$row.')');
$worksheet->setCellValue('C'.$row, '=B'.$row);
}
$worksheet->getStyle('C1:C'.$testDateCount)
->getNumberFormat()
->setFormatCode('yyyy-mmm-dd');
echo '<hr />';
// Test the formulae
?>
<p><strong>Warning: </strong>The PHPExcel DATEVALUE() function accepts a wider range of date formats than MS Excel's DATEFORMAT() function.</p>
<table border="1" cellspacing="0">
<tr>
<th>Date String</th>
<th>Formula</th>
<th>Excel DateStamp</th>
<th>Formatted DateStamp</th>
</tr>
<?php
for ($row = 1; $row <= $testDateCount; ++$row) {
echo '<tr>';
echo '<td>' , $worksheet->getCell('A'.$row)->getFormattedValue() , '</td>';
echo '<td>' , $worksheet->getCell('B'.$row)->getValue() , '</td>';
echo '<td>' , $worksheet->getCell('B'.$row)->getFormattedValue() , '</td>';
echo '<td>' , $worksheet->getCell('C'.$row)->getFormattedValue() , '</td>';
echo '</tr>';
}
?>

Order results in left join with db_query()

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.

Selecting posts in Wordpress

I'm new to wordress and php.
I need to display all posts from june 2009 to june 2010.
How can I do that by creating a custom loop?
query_posts() just allow to show posts from a specific week or month. However, you can show posts between two dates, adding a few lines of code. You need to paste this code wherever in your theme you'd like to display.
<?php
function filter_where($where = '') {
$where .= " AND post_date >= '2009-06-01' AND post_date <= '2010-06-30'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
while (have_posts()) :
the_post();
the_content();
endwhile;
?>
Source: http://bit.ly/i5zXP0
Using WP Query
Time Parameters
year (int) - 4 digit year (e.g. 2011).
monthnum (int) - Month number (from 1 to 12).
w (int) - Week of the year (from 0 to 53). Uses the MySQL WEEK command. The mode is dependent on the "start_of_week" option.
day (int) - Day of the month (from 1 to 31).
hour (int) - Hour (from 0 to 23).
minute (int) - Minute (from 0 to 60).
second (int) - Second (0 to 60).
m (int) - YearMonth (For e.g.: 201307).
Returns posts for just the current date:
$today = getdate();
$query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"] );
Returns posts for just the current week:
$week = date('W');
$year = date('Y');
$query = new WP_Query( 'year=' . $year . '&w=' . $week );
Return posts for March 1 to March 15, 2010:
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts for March 1 to March 15, 2010
$where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
Return posts 30 to 60 days old
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts 30 to 60 days old
$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
For more reference click here http://codex.wordpress.org/Class_Reference/WP_Query

Resources