Wordpress prepare fails...? - wordpress

Ok... I got another WP problem now.
For my WP theme, i have some special things. For example, i have a table containing some stuff.
when i insert (in this case update) to this table i use $wpdb, like the code below:
$sql = $wpdb->prepare("UPDATE $table_name SET
`title` = '$title',
`text` = '$text',
`image` = '$image',
`thumbnail` = '$thumb',
`show` = $sql_show,
`order` = $order,
`language` = '$language',
`type` = '$type'
WHERE `id` = $id
;");
$wpdb->query($sql);
I have also tried this:
$sql = $wpdb->prepare("UPDATE $table_name SET
`title` = %s,
`text` = %s',
`image` = %s,
`thumbnail` = %s,
`show` = %d,
`order` = %d,
`language` = %s,
`type` = %s
WHERE `id` = $id
;", $title, $text, $image, $thumb, $show, $order, $language, $type);
Both of them works, EXCEPT when the $text contains a "%". If it contains this, the $sql is blank. Of coure i could change all the "%" to "percent", but that resolution is not acceptable! ;)

% has to be escaped with a percent, so replace a single percent with a double percent in $text: $text = str_replace('%', '%%', $text)

Related

Why is wpdb-prepare() with passed variable not working

The following using wpdb->prepare returns empty results while the code below not using wpdb->prepare returns the correct results. What is wrong?
global $wpdb;
$query = $wpdb->prepare("select subid, firstname, lastname from wpks_members where member_id = %d,".$mid);
$row= $wpdb->get_row($query);
$query = $wpdb->prepare("select t.subid, t.testid, t.test_date, t.puzzle_score,t.clock_score,t.match_score,t.oddone_score,(t.puzzle_score+t.clock_score+t.match_score+t.oddone_score) as tot,
if((t.puzzle_score+t.clock_score+t.match_score+t.oddone_score) < 90, 'Yes', 'No') as refer, u.ufname, u.ulname
from wpks_results t
join wpks_hasi_users u on t.userid = u.userid
where t.member_id = %d,".$mid);
$rows = $wpdb->get_results($query);
//this below works
$row= $wpdb->get_row("select subid, firstname, lastname from wpks_members where member_id = ".$mid);
$query = $wpdb->prepare();
$rows = $wpdb->get_results("select t.subid, t.testid, t.test_date, t.puzzle_score,t.clock_score,t.match_score,t.oddone_score,(t.puzzle_score+t.clock_score+t.match_score+t.oddone_score) as tot,
if((t.puzzle_score+t.clock_score+t.match_score+t.oddone_score) < 90, 'Yes', 'No') as refer, u.ufname, u.ulname
from wpks_results t
join wpks_hasi_users u on t.userid = u.userid
where t.member_id = ".$mid);
It should be:
$query = $wpdb->prepare("select subid, firstname, lastname from wpks_members where member_id = %d", $mid);

How to display subcategories product's min and max price on parent categories in wordpress

I want to display min and max price of subcategories product on parent categories pages.
function woocommerce_template_loop_subcategory_info( $category ) {
$term = get_queried_object();
global $wpdb;
# Get ALL related products prices related to a specific product category
$results = $wpdb->get_col( "
SELECT pm.meta_value
FROM {$wpdb->prefix}term_relationships as tr
INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms as t ON tr.term_taxonomy_id = t.term_id
INNER JOIN {$wpdb->prefix}postmeta as pm ON tr.object_id = pm.post_id
WHERE tt.taxonomy LIKE 'product_cat'
AND t.term_id = {$term->term_id}
AND pm.meta_key = '_price'
");
// Sorting prices numerically
sort($results, SORT_NUMERIC);
// Get the min and max prices
$min = current($results);
$max = end($results);
// Format the price range after the title
$price_range = sprintf( __( ' <small>(from %1$s to %2$s)</small>', 'woocommerce' ), wc_price( $min ), wc_price( $max ) );
return $term_title . $price_range;
}
this is how to get the related product min and max price but i want to get subcategories price.

How to get the first cell that has a value in phpexcel

I have an excel file that have first data in cell B4, this is the first of the content area. The content area range is B4:D9.
How can I get the B4?
I used this code but it is not getting the b4,
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$data = array();
$highestNum = 0;
for ($row = 1; $row <= $highestRow; $row++){
$data[] = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
"",
TRUE,
FALSE);
$columns[] = PHPExcel_Cell::stringFromColumnIndex($row-1);
$highestNum = $row;
}

How to Get all terms by post type

I need to get all items in a custom taxonomy in custom post type
here is my try:
function gat_all_terms($taxonomy){
$terms = get_terms( $taxonomy, 'orderby=count&hide_empty=0' );
$count = count($terms);
$out ='';
if ( $count > 0 ){
foreach ( $terms as $term ) {
$out .= "<li class='item'> <a href='#'>" . $term->name . "</a></li>";
}
}
return $out;
}
But i cant detect for a custom post type.
If you're trying to get the terms of a custom post type you can check out the answer here:
https://wordpress.stackexchange.com/questions/14331/get-terms-by-taxonomy-and-post-type
static public function get_terms_by_post_type( $taxonomies, $post_types ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT t.*, COUNT(*) from $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s')
GROUP BY t.term_id",
join( "', '", $post_types ),
join( "', '", $taxonomies )
);
$results = $wpdb->get_results( $query );
return $results;
}
Which you could convert to something simpler like:
function get_terms_by_post_type( $taxonomy, $post_type ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT t.*, COUNT(*) from $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s')
GROUP BY t.term_id",
$post_type,
$taxonomy
);
$results = $wpdb->get_results( $query );
return $results;
}
get_terms_by_post_type( 'taxonomy', 'type' );

Drupal Upgrade:: Need to rewrite pagerquery to version 7

I need to rewrite pagerquery, i have tried out several options adding tags, extend(PagerDefault) but nothing worked for me:
Please help.
My version 6 code is:
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '.$sort_join.' WHERE n.uid = %d AND n.type = "case" AND n.status = 1 ORDER BY '. $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.uid = %d AND n.type = "case" AND n.status = 1';
$args = array('uid' => $user->uid);
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql($sql_count);
if ($pager) {
$result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args);
dsm($result);
}
else {
$result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10));
}
$num_rows = FALSE;
while ($node = db_fetch_object($result)) {
$output .= node_view(node_load($node->nid), 1);
$num_rows = TRUE;
}
Without knowing the name of your join table this isn't complete, but should get you started:
$query = db_select('node', 'n')
->fields('n', array('nid', 'sticky', 'title', 'created'))
->condition('n.uid', $user->uid)
->condition('n.type', 'case')
->condition('n.status', 1)
->extend('PagerDefault')
->limit(variable_get('default_nodes_main', 10))
->addTag('node_access')
->orderBy('col_name');
$query->join('table_to_join', 'table_alias', 'table_alias.nid = n.nid');
foreach ($query->execute() as $row) {
// Do what you need to
}
if ($vidw == 'my_cases' or $vidw == 'my') { // including private
switch ($sort_by) {
case 'rated':
$order = 'n.sticky DESC, vc.value DESC';
$sort_join = 'LEFT OUTER JOIN {votingapi_cache} vc ON n.nid = vc.content_id AND vc.content_type = "node" AND vc.function = "average"';
break;
case 'discussed':
$order = 'n.sticky DESC, nc.comment_count DESC';
$sort_join = 'LEFT OUTER JOIN {node_comment_statistics} nc ON n.nid = nc.nid';
break;
case 'viewed':
$order = 'n.sticky DESC, nc.totalcount DESC';
$sort_join = 'LEFT OUTER JOIN {node_counter} nc ON n.nid = nc.nid';
break;
case 'recent':
default:
$order = 'n.sticky DESC, n.created DESC';
$sort_join = '';
break;
}
// from taxonomy_select_nodes
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '.$sort_join.' WHERE n.uid = %d AND n.type = "case" AND n.status = 1 ORDER BY '. $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.uid = %d AND n.type = "case" AND n.status = 1';
$args = array('uid' => $user->uid);
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql($sql_count);
if ($pager) {
$result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args);
}
else {
$result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10));
}
// $output .= taxonomy_render_nodes($result);
$num_rows = FALSE;
while ($node = db_fetch_object($result)) {
$output .= node_view(node_load($node->nid), 1);
$num_rows = TRUE;dsm($output);
}
if ($num_rows) {
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
}
else {
$output .= '<p>'. t('There are currently no visible cases in this category.') .'</p>';
}
}
In above drupal 6 code, i have updated the query which gives me result but node_view() and theme() does not work.

Resources