I would like to run an update query for every row with a specific ID:
e.g.
$ids = array(111, 112, 113);
$query = "UPDATE mytable SET columnName = 'Y' WHERE id = :id1 or id = :id2 or id = :id3";
$stmt->bindParam(':id1', $ids[0], PDO::PARAM_INT);
$stmt->bindParam(':id2', $ids[1], PDO::PARAM_INT);
$stmt->bindParam(':id3', $ids[2], PDO::PARAM_INT);
This works fine if I know there are 3 ids to update, but how would I do this if the number of id fields is variable?
Something like this would do the trick.
$ids = array(111, 112, 113);
$valueString = "";
foreach($ids as $key => $val) {
$valueString .= ":id" . $key . " = " . $val;
if (end($ids) != $val) {
$valueString .= ", ";
}
}
$query = "UPDATE mytable SET columnName = 'Y' WHERE ". $valueString;
foreach($ids as $key => $val) {
$stmt->bindParam(':id' . $key, $val, PDO::PARAM_INT);
}
Related
I want to create a database query in a view helper, this works with the following code:
$uid = 11;
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_test_domain_model_author');
$query = $queryBuilder
->select('*')
->addSelectLiteral(
$queryBuilder->expr()->count('tx_test_domain_model_author.author', 'counter')
)
->from('tx_test_domain_model_author')
->join(
'tx_test_domain_model_author',
'tx_test_publication_author_mm',
'tx_test_publication_author_mm',
$queryBuilder->expr()->eq(
'tx_test_domain_model_author.uid',
$queryBuilder->quoteIdentifier('tx_test_publication_author_mm.uid_foreign')
)
)
->where(
$queryBuilder->expr()->eq(
'tx_test_publication_author_mm.uid_local',
$queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
)
)
->orderBy('tx_test_domain_model_author.uid', 'ASC');
$result = $query->execute();
$res = [];
while ($row = $result->fetch()) {
$res[] = $row;
}
print_r($res);
However, I only get one record, although the counter tells me it would be 3.
What am I doing wrong?
If the counter says its three items, then try change this:
$result = $query->execute();
$res = [];
while ($row = $result->fetch()) {
$res[] = $row;
}
into this:
$result = $query->execute()->fetchAll();
That fetches all rows into an array that you walk throug with:
foreach($result as $row){
...
}
It seems the QueryBuilder works differently, this gives me one result namely the first entry in the table:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_test_publication_author_mm');
$query = $queryBuilder
->select('*')
->from('tx_test_publication_author_mm');
$result = $query->execute()->fetchAll();
foreach($result as $row){
echo $row['field'];;
}
This gives me all results:
$db = mysqli_connect($dbHost, $dbUser, $dbPassword, $dbName) or die (mysql_error ());
$sql = "SELECT * FROM tx_test_publication_author_mm";
foreach ($db->query($sql) as $row) {
echo $row['field'];
}
I'm trying to use the extension RelatedContentByTags, but the entry on bolt_taxonomy column it's being recognized as column.
I have this error when I put {{ relatedcontentbytags(record) }} on index.
'Twig_Error_Runtime thrown with message
"An exception has been thrown during the rendering of a template
("An exception occurred while executing ' SELECT bolt_entries.id FROM bolt_entries LEFT JOIN
bolt_taxonomy ON bolt_entries.id = bolt_taxonomy.content_id WHERE
bolt_entries.status = "published"AND bolt_entries.id != 3 AND
bolt_taxonomy.contenttype = "entries" AND (bolt_taxonomy.taxonomytype = "tags" AND
(bolt_taxonomy.slug = "teste2"))':
SQLSTATE[42703]: Undefined column: 7 ERROR: column "published" does not exist
LINE 1: ...t_taxonomy.content_id WHERE bolt_entries.status = "published...^") in "entry.twig" at line 65."
And the extension.php is that:
$results = array();
foreach ($tables as $name) {
$table = sprintf('%s%s', $tablePrefix, $name);
$querySelect = '';
$querySelect .= sprintf(' SELECT %s.id FROM %s', $table, $table);
$querySelect .= sprintf(' LEFT JOIN %s', $taxonomyTable);
$querySelect .= sprintf(' ON %s.id = %s.content_id', $table, $taxonomyTable);
$querySelect .= sprintf(' WHERE %s.status = "published"', $table);
if ($name == $record->contenttype['slug']) {
$querySelect .= sprintf('AND %s.id != '. $record->id, $table);
}
$querySelect .= sprintf(' AND %s.contenttype = "%s"', $taxonomyTable, $name);
$querySelect .= sprintf(' AND (%s)', $queryWhere);
$queryResults = $app['db']->fetchAll( $querySelect );
if (!empty($queryResults)) {
$ids = implode(' || ', \utilphp\util::array_pluck($queryResults, 'id'));
$contents = $app['storage']->getContent($name, array('id' => $ids, 'returnsingle' => false));
$results = array_merge( $results, $contents );
}
}
Depending on what database you are using (I'm guessing Postgresql based on the error code) text enclosed in double-quotes "" can be used to denote a table name; single-quotes '' are normally used to denote character literals, so try changing "published" to 'published' (the same goes for entries, tags, and teste2 too of course).
-
I have been researching on how to create a date archive for a custom post which has a taxonomy and various terms...
I have had great success with this Stackoverflow answer from walltea however I need some help with modifying it to also take into account taxonomies and its terms...
Is this possible?
--EDIT--
Ok I managed to do this after LOADS of time on it, If you know if a better solution to this then please do share as the way I found is a little bit cumbersome: (again original code curtosy of walltea's Stackoverflow answer)
1.
In your functions.php place this code: (changing the custom posts types that you have for '$cptPart')
/**
* Custom post type date archives
*/
/**
* Custom post type specific rewrite rules
* #return wp_rewrite Rewrite rules handled by Wordpress
*/
add_action('generate_rewrite_rules', 'cpt_rewrite_rules');
/**
* Generate date archive rewrite rules for a given custom post type
* #param string $cpt slug of the custom post type
* #return rules returns a set of rewrite rules for Wordpress to handle
*/
function cpt_rewrite_rules($wp_rewrite) {
$rules = array();
$cptPart = "(news_events|live_reports|book_review|readers_digest|publications|jobs)";
$taxonomyPart = "([a-zA-Z_-]+)";
$termPart = "([a-zA-Z_-]+)";
$patterns = array(
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/$taxonomyPart/$termPart",
'vars' => array('post_type', 'year', 'monthnum', 'day', 'taxonomy', 'term')
),
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})/$taxonomyPart/$termPart",
'vars' => array('post_type', 'year', 'monthnum', 'taxonomy', 'term')
),
array(
'rule' => "$cptPart/([0-9]{4})/$taxonomyPart/$termPart",
'vars' => array('post_type', 'year', 'taxonomy', 'term')
),
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/$taxonomyPart",
'vars' => array('post_type', 'year', 'monthnum', 'day', 'taxonomy')
),
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})/$taxonomyPart",
'vars' => array('post_type', 'year', 'monthnum', 'taxonomy')
),
array(
'rule' => "$cptPart/([0-9]{4})/$taxonomyPart",
'vars' => array('post_type', 'year', 'taxonomy')
),
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})",
'vars' => array('post_type', 'year', 'monthnum', 'day')
),
array(
'rule' => "$cptPart/([0-9]{4})/([0-9]{1,2})",
'vars' => array('post_type', 'year', 'monthnum')
),
array(
'rule' => "$cptPart/([0-9]{4})",
'vars' => array('post_type', 'year')
),
);
foreach ($patterns as $pattern) {
$i = 1;
$params = array();
foreach ($pattern['vars'] as $var) {
$params[] = $var . '=' . $wp_rewrite->preg_index($i);
$i++;
}
$query = 'index.php?' . implode('&', $params);
$rules[$pattern['rule']."/?$"] = $query;
$rules[$pattern['rule']."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
$rules[$pattern['rule']."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
$rules[$pattern['rule']."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i);
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite;
}
/**
* Get a montlhy archive list for a custom post type
* #param string $cpt Slug of the custom post type
* #param boolean $echo Whether to echo the output
* #return array Return the output as an array to be parsed on the template level
*/
function get_cpt_archives($cpt, $echo = false, $taxomony = null, $term = null)
{
global $wpdb;
$conditions = "";
if ($taxomony) {
$conditions .= " AND wtt.taxonomy LIKE %s";
if ($term) {
$conditions .= " AND wt.slug LIKE %s";
}
}
$sql = $wpdb->prepare("
SELECT *
FROM wp_posts p
LEFT JOIN wp_term_relationships wtr ON wtr.object_id = p.ID
LEFT JOIN wp_term_taxonomy wtt ON wtr.term_taxonomy_id = wtt.term_taxonomy_id
LEFT JOIN wp_terms wt ON wtt.term_id = wt.term_id
WHERE post_type = %s
AND post_status = 'publish'
$conditions
GROUP BY YEAR(p.post_date),
MONTH(p.post_date)
ORDER BY p.post_date DESC
", $cpt, $taxomony, $term);
$results = $wpdb->get_results($sql);
if ( $results )
{
$archive = array();
foreach ($results as $r)
{
$year = date('Y', strtotime( $r->post_date ) );
$month = date('F', strtotime( $r->post_date ) );
$month_num = date('m', strtotime( $r->post_date ) );
$link = get_bloginfo('siteurl') . '/' . $cpt . '/' . $year . '/' . $month_num;
if ($taxomony) {
$link .= "/" . $r->taxonomy;
if ($term) {
$link .= "/" . $r->slug;
}
}
$this_archive = array( 'month' => $month, 'year' => $year, 'link' => $link );
array_push( $archive, $this_archive );
}
if( !$echo )
return $archive;
foreach( $archive as $a )
{
echo '<li>' . $a['month'] . ' ' . $a['year'] . '</li>';
}
}
return false;
}
-
2. In your custom archive file (for example one of mine was archive-publications.php) place this code above the loop:
<?php
function lr_posts_join($join) {
global $taxonomy_global_join;
global $term_global_join;
if ($taxonomy_global_join) $join .= " $taxonomy_global_join";
if ($term_global_join) $join .= " $term_global_join";
return $join;
}
function lr_posts_where($where) {
global $taxonomy_global_where;
global $term_global_where;
if ($taxonomy_global_where) $where .= " $taxonomy_global_where";
if ($term_global_where) $where .= " $term_global_where";
return $where;
}
add_filter('posts_join', 'lr_posts_join');
add_filter('posts_where', 'lr_posts_where');
$extraArgs = "";
if (get_query_var('taxonomy')) {
$taxonomy_global_join = "
LEFT JOIN $wpdb->term_relationships wtr ON wtr.object_id = $wpdb->posts.ID
LEFT JOIN $wpdb->term_taxonomy wtt ON wtr.term_taxonomy_id = wtt.term_taxonomy_id ";
$taxonomy_global_where = " AND wtt.taxonomy LIKE '" . esc_sql(like_escape(get_query_var('taxonomy'))) . "'";
$extraArgs .= "&taxonomy=" . get_query_var('taxonomy');
if (get_query_var('term')) {
$term_global_join = "
LEFT JOIN $wpdb->terms wt ON wtt.term_id = wt.term_id ";
$term_global_where = " AND wt.slug LIKE '" . esc_sql(like_escape(get_query_var('term'))) . "'";
$extraArgs .= "&term=" . get_query_var('term');
}
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts = query_posts("post_type=publications&paged=$paged$extraArgs");
$taxonomy_global_join = $taxonomy_global_where = '';
$term_global_join = $term_global_where = '';
while (have_posts()) : the_post(); ?>
-- loop code here --
<?php endwhile; ?>
3. The above code lets our archive page understand our filtering for taxonomies and terms. Save your files and re-save your permalinks in the admin area. Next lets use this puppy! I used this in my custom sidebar (sidebar-publications.php):
<ul>
<?php echo get_cpt_archives( 'publications', true, 'publication_category', 'articles' ) ?>
</ul>
Our function gets the custom post (Publications) and 'true' is set so we want the normal list layout. Next it calls the taxonomy we want... I just have one for this custom post ( publication_category ) and finally the term I want in that taxonomy ( articles ). This should spit out a link like this: www.website/custom_post/year/month/taxonomy/term
I hope this code serves you well! My client is very happy ;)
Very Best,
Chris
Ok, I would like to add one more sort variable on category pages, that is to sort by Manufacturer.
Running on 1.5.3.1 and have tried this:
1. catalog/controller/product/category.php
added the following:
$this->data['sorts'][] = array(
'text' => $this->language->get('text_manufacturer_asc'),
'value' => 'manufacturer-ASC',
'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=manufacturer&order=ASC' . $url)
);
$this->data['sorts'][] = array(
'text' => $this->language->get('text_manufacturer_desc'),
'value' => 'manufacturer-DESC',
'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=manufacturer&order=DESC' . $url)
);
catalog/language/*/product/category.php
added:
$_['text_manufacturer_asc'] = '▲ -Proizvođaču- ▲';
$_['text_manufacturer_desc'] = '▼ -Proizvođaču- ▼';
catalog/model/catalog/product.php
before: 'p.date_added' I inserted a line:
'manufacturer'
and few lines below I changed the block with the following one:
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} elseif ($data['sort'] == 'p.price') {
$sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
} elseif ($data['sort'] == 'manufacturer') {
$sql .= " SELECT * FROM". DB_PREFIX . "product p ORDER BY manufacturer";
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY p.sort_order";
}
But it is not working.
Error says:
PHP Notice: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROMproduct p ORDER BY manufacturer ASC,...
and another one I have found:
PHP Notice: Error: Unknown column 'special' in 'order clause'<br />Error No: 1054<br />SELECT p.product_id, (SELECT AVG(rating) AS total FROM review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id)...
Can anybody help?
You are almost there but You will have to modify it a little bit more.
First, You really do not want to order by manufacturer ID stored in product.manufacturer_id but by manufacturer name. In this case we need to modify the query in catalog\model\catalog\product.php model in getProducts method - find this line:
$sql .= " LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
(should be line 89 in OC 1.5.5.1) and before it add:
$sql .= " LEFT JOIN " . DB_PREFIX . "manufacturer m ON p.manufacturer_id = m.manufacturer_id";
Also You'll need to allow manufacturer as a sorting option:
$sort_data = array(
'pd.name',
'p.model',
'p.quantity',
'p.price',
'rating',
'p.sort_order',
'p.date_added',
'manufacturer', // <== this is added
);
Now in the sorting section add sorting by manufacturer name:
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} elseif ($data['sort'] == 'p.price') {
$sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
} elseif ($data['sort'] == 'manufacturer') { // <== this is added
$sql .= " ORDER BY m.name"; // <== this is added
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY p.sort_order";
}
And that's it.
I want to skip the foreach loop;
How can I get the sum by using a yii query?
$sql = 'select size_kb from comp_arch_stats where company_id = ' . ($model->company_id) . ' and arch_month = ' . $month . ' and arch_year = ' . $year . ';';
$val = Yii::app()->db->createCommand($sql)->queryAll();
$sum = 0;
foreach ($val AS $result) {
$sum += $result['size_kb'];
}
"'select sum(size_kb) as size_kb from comp_arch_stats where company_id = ' . ($model->company_id)"
Try this
$criteria=new CDbCriteria;
$criteria->select = 'sum(size_kb) AS KbCount';
$criteria->condition ="company_id = :company_id AND arch_month =:arch_month AND arch_year=:arch_year "
$criteria->params = array (
':company_id '=> $model->company_id,
':arch_month' => $month,
':arch_year' => $year,
);
comp_arch_stats::model()->findAll($criteria);