I have 3 nodes of content type 'mycontenttype'. I'm trying to setup a sortable/pagable table with a limit of 10 items per page.
In this code, $nids only returns 3 nodes. I'm actually runnng node_load_multiple($nids) then looping through those nodes to build the $rows variable. Only 3 appear.
Problem: The pager is rendering with 4 pages.
Expectation: There should be no pager rendering because I do not have 10 nodes in the query or the count query.
Any insight would be greatly appreciated.
<?php
function mymodule_create_a_pager_table() {
$query = db_select('node', 'n')->extend('PagerDefault')->extend('TableSort')->element('my_custom_id');
$query->fields('n', array('nid'));
$query->condition('n.type', 'mycontenttype');
$query->condition('n.status', 1);
$count_query = clone $query;
$query->setCountQuery($count_query);
$query->limit(10);
$header = array(array('data' => 'Title', 'sort' => 'asc', 'field' => 'n.title'), 'column 2', 'column 3');
$query->orderByHeader($header);
$nids = $query->execute()->fetchCol();
// ... building $rows array for display only here
$output = theme('table', array('header'=> $header, 'rows' => $rows));
$output .= theme('pager', array('element' => 'my_custom_id', 'quantity' => 10));
return $output;
}
?>
Output
Node 1 Title | col2 val | col3 val
Node 2 Title | col2 val | col3 val
Node 3 Title | col2 val | col3 val
1 2 3 4 next › last »
I think element should be an integer.
Try to leave both the ->element() call and any arguments to theme('pager') away.
Also, your count query is wrong. Just don't define it either, that will be done automatically for you. This is probably the actual reason for your problem, not the element thing.
The count query get's executed and the first returned value (fetchField()) is assumed to be the number of elements. Your query probably returns a nid and that is mistaken as the number. So just leave that away, and Drupal will build a correct count query automatically for you.
Related
I am writing a batch process to import some orders from a mysql table called tbl_bankimport:
$bankrecs = $wpdb->get_results("Select * from tbl_bankimport");
foreach ($bankrecs as $bankrec) {
$order = wc_create_order(array(['customer_id' => $bankrec->user_id]));
echo "Order " . $order->id . "created </br>";
}
In my test file I have 47 record and when I run this I get 47 order # echoed on the screen. But I end up with 94 records in woocommerce!
If I clear down the orders replace this:
$order = wc_create_order(array(['customer_id' => $bankrec->user_id]));
with
$order = wc_create_order(array(['customer_id' => 7072]));
and then re-run I get 47 records entered.
Bemused and mystified!
Excluding the customer_id from the data array and adding it like this:
update_post_meta($order->id, '_customer_user', bankrecs->user_id );
seems to be a solution.
I have an array like
Array (
[1] => 85590762,22412382,97998072
[3] => 22412382
)
Where key is the item_id and value is the value of a column which I need to update against an item. I can use db_update in a loop but i want to avoid this strategy due to performance. I want to update all the rows in a single db call. Also using db_query I think will not be a good idea. So is there any way using db_update to update these rows?
According to above data, standard mysql queries will be like
update items set sold= 1, users = '85590762,22412382,97998072' Where item_id = 1;
update items set sold = 1, users = '22412382' Where item_id = 3;
Unfortunately you can't do that. Currently and probably in the feature, there won't be support for updating (multiple) values on different rows with different values with one db_update.
If your data is this:
$for_update = array(
1 => "85590762,22412382,97998072",
3 => "22412382",
);
You can do these:
CASE 1: Create a loop and call every time the update function:
foreach ($for_update as $key => $value) {
$fields = array('sold' => 1, 'users' => $value);
db_update('items')->fields($fields)->condition('item_id', $key, '=')->execute();
}
Pros: other modules can hook inside your query, can support multiple DB drivers
Cons: Object initialization is slow, lots of DB connection/traffic
CASE 2: It's faster if you use db_query()...
... because in that case there's no object instantiation/operation, which is a bit costly and other conversion. (i.e.: https://drupal.stackexchange.com/questions/129669/whats-faster-db-query-db-select-or-entityfieldquery)
foreach ($for_update as $key => $value) {
db_query("UPDATE items SET sold = :sold, users = :users WHERE item_id = :item_id",
array(':sold' => 1, ':users' => $value, ':item_id' => $key)
);
}
Pros: no object initialization and operation < so it's faster
Cons: other modules can't hook inside your query, your code can break under different DB drivers, lots of DB connection/traffic
CASE 3: Also you can use transaction
This can boost a bit your performance at some case.
$transaction = db_transaction();
try {
foreach ($for_update as $key => $value) {
$fields = array('sold' => 1, 'users' => $value);
db_update('items')->fields($fields)->condition('item_id', $key, '=')->execute();
}
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('my_type', $e);
}
Note: transaction mostly used, when you want everything or nothing. But with some DB drivers, you can optimize out the COMMIT commands.
In case 1 you get something like this:
UPDATE items SET sold = 1, users = '85590762,22412382,97998072' WHERE item_id = 1;
COMMIT;
UPDATE items SET sold = 1, users = '22412382' WHERE item_id = 3;
COMMIT;
With transaction you do something like this:
UPDATE items SET sold = 1, users = '85590762,22412382,97998072' WHERE item_id = 1;
UPDATE items SET sold = 1, users = '22412382' WHERE item_id = 3;
COMMIT;
With COMMIT you write out the data to the final place (into long-term storage, until this, it's only somewhere in the memory or a temporary place). When you use rollback, you drop these changes. At least this is how I understand this. (i.e. I get performance improvement with this when I used sqlite.)
Same as case 1 or 2 +
Pros: you can rollback your data if you need to be consistent, you write out data only once to the drive < so sql only "once" do IO, lots of DB connection/traffic
CASE 4: Or you can build one sql statement for this:
$ids = array();
$when_then = "";
foreach ($for_update as $key => $value) {
$ids[] = $key;
$when_then .= "WHEN $key THEN '$value' ";
}
db_query("UPDATE items
SET sold = 1, users = CASE item_id
$when_then
ELSE users
END
WHERE item_id IN(:item_ids)", array(':item_ids' => $ids));
Probably this is the fastest way from all from here.
NOTE: $when_then variable doesn't contain the best solution, it's better if you can use the params or you escape unsafe data.
Pros: between your server and sql there will be only one request and less redundant data, one bigger sql queries faster then a lot of small ones
Cons: other modules can't hook inside your query, your code can break under different DB drivers
Also please note, after (I think) PHP 5.3 you can't run more than one statement in db_query or in PDO by default, because of it can be an SQL Injection, so it blocks. However you can set this, but not recommended. (PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND))
13-05-2019 edit:
Just a side note, I did a few month ago some performance measurement on medium dataset to query down some data via db_select and db_query. The magnitude of the performance of these are: under you run one db_select, you can run two db_query. This also applies to db_update and db_query. The test queried down some columns with a simple SELECT, no JOINS, no fancy dandy stuff, just two condition. Of course, the measurement also contained/included the time which was required to build up these queries (at both case). However please note, the Drupal Coding Standard requires to use db_update, db_delete, db_merge on modification normally, only 'allows' db_query instead of db_select operation.
Using BULK UPDATE you can update multiple values on diferent rows or in the same row with the dinamic query db_update .
Note: By bulk updating only one query can be sent to the server instead of one query for each row to update.
Syntax:
UPDATE yourTableName
SET yourUpdateColumnName =
(CASE yourConditionColumnName WHEN Value1 THEN 'UpdatedValue'
WHEN Value2 THEN 'UpdatedValue'
.
.
N
END)
WHERE yourConditionColumnName IN(Value1,Value2,.....N);
More info about expressions on UpdateQuery on:
https://api.drupal.org/api/drupal/includes%21database%21query.inc/function/UpdateQuery%3A%3Aexpression/7.x
With your data will be:
//Create array
$for_update = [
1 => "85590762,22412382,97998072",
3 => "22412382",
];
$item_ids_to_filter = [];
//Setup when-then conditions
$when_then = "CASE item_id ";
foreach ($for_update as $item_id => $users_id) {
$when_then .= "WHEN {$item_id} THEN '{$users_id}' ";
$item_ids_to_filter[] = $item_id;
}
$when_then .= "END";
//Execute query and update the data with db_update
db_update('items')
->expression("users",$when_then)
->condition('item_id', $item_ids_to_filter)
->execute();
As result the next query is generated:
UPDATE items
SET users =
(CASE item_id = WHEN 1 THEN '85590762,22412382,97998072'
WHEN 3 THEN '22412382'
END)
WHERE (item_id IN ('1', '3'))
I think you could do something like
$query = db_udpate('table')->fields(array('field_1','field_2','field_3'));
foreach ($fields as $record) {
$query->values($record);
}
return $query->execute();
Where $fields = array ('field_1' => VALUE, 'field_2' => VALUE)
I use JetPack stats to follow the stats for my blog. I would like to extract the top 10 most-viewed posts for a given period (e.g. last month).
I used the WordPress stats plugin API before which worked nicely, but after upgrade to JetPack this doesn't work anymore.
Is there an API which allows me to query the number of view counts for a post?
Inside the Database
This option is recorded in the database and is used by the Dashboard widget:
get_option( 'stats_cache' );
It returns an array like this:
array(
['7375996b7a989f95a6ed03ca7c899b1f'] => array(
[1353440532] => array(
[0] => array(
['post_id'] => 0
['post_title'] => 'Home page'
['post_permalink'] => 'http://www.example.com/'
['views'] => 1132
)
[1] => array(
['post_id'] => 4784
['post_title'] => 'Hello World!'
['post_permalink'] =>
['views'] => 493
)
/* till item [9] */
Querying WordPress.com
It is possible to call the following Jetpack function:
if( function_exists( 'stats_get_csv' ) ) {
$top_posts = stats_get_csv( 'postviews', 'period=month&limit=30' );
}
Which returns:
array(
[0] => array(
['post_id'] => 0
['post_title'] => 'Home page'
['post_permalink'] => 'http://www.example.com/'
['views'] => 6806
)
[1] => array(
['post_id'] => 8005
['post_title'] => 'Hello World!'
['post_permalink'] =>
['views'] => 1845
)
/* till item [29] */
Function get_stats_csv
/plugins/jetpack/modules/stats.php
The function get_stats_csv calls http://stats.wordpress.com/csv.php. If we visit this address, we get this response:
Error: api_key is a required parameter.
Required parameters: api_key, blog_id or blog_uri.
Optional parameters: table, post_id, end, days, limit, summarize.
Parameters:
api_key String A secret unique to your WordPress.com user account.
blog_id Integer The number that identifies your blog.
Find it in other stats URLs.
blog_uri String The full URL to the root directory of your blog.
Including the full path.
table String One of views, postviews, referrers, referrers_grouped,
searchterms, clicks, videoplays.
post_id Integer For use with postviews table.
end String The last day of the desired time frame.
Format is 'Y-m-d' (e.g. 2007-05-01)
and default is UTC date.
days Integer The length of the desired time frame.
Default is 30. "-1" means unlimited.
period String For use with views table and the 'days' parameter.
The desired time period grouping. 'week' or 'month'
Use 'days' as the number of results to return
(e.g. '&period=week&days=12' to return 12 weeks)
limit Integer The maximum number of records to return.
Default is 100. "-1" means unlimited.
If days is -1, limit is capped at 500.
summarize Flag If present, summarizes all matching records.
format String The format the data is returned in,
'csv', 'xml' or 'json'.
Default is 'csv'.
Non-working query example:
?api_key=123456789abc&blog_id=155&table=referrers&days=30&limit=-1&summarize
Result format is csv with one row per line and column names in first row.
Strings containing double quotes, commas, or "\n" are enclosed in double-quotes.
Double-qoutes in strings are escaped by inserting another double-quote.
Example: "pet food" recipe
Becomes: """pet food"" recipe"
Developers, please cache the results for at least 180 seconds.
In Drupal, I can execute a SQL as follows:
$query_object = db_query("SELECT * FROM {nodes}");
If I know the query returns only a single result (so only 1 row and 1 column), I can directly fetch it with:
$result = db_result($query_object);
If I got multiple results, I need to loop through them with something like:
$rows[] = array();
while (($row = db_fetch_object($query_object) != FALSE) {
$rows[] = $row;
}
I'm wondering if there is an easier way to do that? Is there a way that I can transfer all results into an array with a single statement? Or isn't that working, because db_result returns a cursor-like object, where you can only fetch a single row each time?
Not in Drupal 6.
In Drupal 7, there are fetch methods that can help to avoid loops like that. From http://drupal.org/node/310072:
<?php
// Retrieve all records into an indexed array of stdClass objects.
$result->fetchAll();
// Retrieve all records into an associative array keyed by the field in the result specified.
$result->fetchAllAssoc($field);
// Retrieve a 2-column result set as an associative array of field 1 => field 2.
$result->fetchAllKeyed();
// You can also specify which two fields to use by specifying the column numbers for each field
$result->fetchAllKeyed(0,2); // would be field 0 => field 2
$result->fetchAllKeyed(1,0); // would be field 1 => field 0
// Retrieve a 1-column result set as one single array.
$result->fetchCol();
// Column number can be specified otherwise defaults to first column
$result->fetchCol($column_index);
?>
In Drupal 7, you can also use:
db_query('QUERY')->fetchAll(PDO::FETCH_ASSOC);
I do always something like this ( just a simple exemple) :
$query = db_query("SELECT nid
FROM {from}
WHERE blallala
",
$tab_arg
);
if ($query->rowCount() == 0) {
$output=t('no result')
} else
{
foreach($query as $result)
{
$tab_res[]=$result;
}
foreach($tab_res as $res)
{
$output.=$res->nid;
}
}
One can also use db_fetch_array($result), where $result =db_query($queryString). As explained from the Drupal documentation:
[It returns] ...an associative array representing the next row of the result, or
FALSE. The keys of this object are the names of the table fields
selected by the query, and the values are the field values for this
result row.
I have a content types with 3 file fields, all of them can have an unlimited number of images. I need to make a view that returns the title of the content and the images name inside an array (I'm using amfphp with services). The problem is that when I add the relationship to the content field_pics fid I get as many duplicate nodes as the number of the images in the field:
EG:
[10] => stdClass Object
(
[nid] => 56
[node_title] => asd asd asd
[node_language] =>
[node_nid] => 56
)
[11] => stdClass Object
(
[nid] => 56
[node_title] => asd asd asd
[node_language] =>
[node_nid] => 56
)
This is the query:
SELECT node.nid AS nid, node.title AS node_title, node.language AS node_language, node.nid AS node_nid
FROM node node
LEFT JOIN content_field_colori node_data_field_colori ON node.vid = node_data_field_colori.vid
LEFT JOIN files files_node_data_field_colori ON node_data_field_colori.field_colori_fid = files_node_data_field_colori.fid
WHERE (node.status <> 0 OR (node.uid = ***CURRENT_USER*** AND ***CURRENT_USER*** <> 0) OR ***ADMINISTER_NODES*** = 1) AND (node.type in ('prodotto'))
ORDER BY node_nid ASC
I don't know how to fix this.
ANy help is appreciated.
Thanks
I think I understand what you're trying to do now. Unfortunately, Services's views support isn't all that great when it comes to CCK. There are a lot of different issues (e.g. one, two, three) with a lot of different patches and comments, but based on my understanding, to capture what you want is to not use relationships and to use the Node row style. If you use relationships, you get the output you're seeing, and if you use the Fields row style, the ImageField fields never load.