I'm looking to grab admin user emails to use within a contact form. I have 2 admins that I've assigned. I'd like to grab them from the database. Where is/what is the designation point that I can use when I make the sql statment?
You can do the following
global $wpdb;
//comma separated list
$admins=$wpdb->get_var('select group_concat(`user_email`) as `admin_emails` from `' . $wpdb->prefix . 'users` as `users` inner join `' . $wpdb->prefix . 'usermeta` as `usermeta` on `usermeta`.`user_id`=`users`.`ID` where `meta_key`=\'wp_user_level\' and `meta_value` in (8,9,10);');
//array of associated arrays
$admins=$wpdb->get_results('select `user_email` from `' . $wpdb->prefix . 'users` as `users` inner join `' . $wpdb->prefix . 'usermeta` as `usermeta` on `usermeta`.`user_id`=`users`.`ID` where `meta_key`=\'wp_user_level\' and `meta_value` in (8,9,10);', ARRAY_A);
<?php $user_info = get_userdata(1);
echo 'Username: ' . $user_info->user_login . "\n";
echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
echo 'User ID: ' . $user_info->ID . "\n";
?>
You can pass the User id herer.....
The first thing to do is to create the function. To do so, paste the following code in your functions.php file:
function getUsersByRole($role) {
$wp_user_search = new WP_User_Search($usersearch, $userspage, $role);
return $wp_user_search->get_results();
}
Once done, you can call the function this way:
$editors = getUsersByRole('Administrator');
foreach($editors as $editor){
//$editor now holds the user ID of an editor
}
Related
The PHP uses the Coinbase API to get the current balance and list what currencies the account has money in. It has been used for a few weeks now, but since swapping one currency into ETC (Ethereum Classic), the balance ignores this currency. It works for all other currencies I have tried, so what is special about ETC that Coinbase is not returing anything via the API. The coinbase website portfolio does report the ETC balance correctly, so it's definitely a API issue.
<?php
include 'cfg.php'; // Contains API key
require_once ('vendor/autoload.php'); // Loads the API libraries
use Coinbase\Wallet\Client as Client;
use Coinbase\Wallet\Configuration as Configuration;
use Coinbase\Wallet\Enum\Param;
use Coinbase\Wallet\Resource\Transaction;
$configuration = Configuration::apiKey($cbase_API_Key, $cbase_API_Secret);
$client = Client::create($configuration);
$stime = $client->getTime();
echo '<h2> Server Time ' . $stime['iso'] . '</h2>';
$balance = 0.00;
$accounts = $client->getAccounts(); // Get the account(s)
echo '<table>';
foreach ( $accounts as $acct )
{
$amt = $acct->getBalance()->getAmount() ;
echo '<tr>';
echo '<td>' . $acct->getCurrency() . '</td>';
echo '<td>' . $acct->getName() . '</td>';
echo '<td>' . $acct->getBalance()->getAmount() . '</td>';
echo '<td>' . $acct->getNativeBalance()->getAmount() . '</td>';
echo '</tr>';
$balance = $balance + $acct->getNativeBalance()->getAmount();
}
echo '<table>';
echo '<h2> Total Balance: ' . $balance . '</h2>';
?>
The issue came down to pagination:
The fix therefore was to set the pagination limit to 100 (max allowed).
The default is 24, hence why the returned list was incomplete.
$accounts = $client->getAccounts(['limit' => 100]);
Forgive me - pretty much a Joomla/SQL rookie and trying to navigate through some tasks I trying to help my local home owners association with.
I'm looking for a way to build my own user list containing multiple custom user fields. My current query gives me multiple records for each user because each user is linked to multiple fields in the "__fields_values" table. I need only ONE record for each user and each custom field as a seperate value in this row.
A simple group clause doesn't seem to cut it.
Nevermind the actual table html, that's just for testing right now.
Hope someone can help me see the light :)
Now I get:
username1|field1|<blank>|email
username1|field2|<blank>|email
My table should look like:
username1|field1|field2|email
My current query and output:
$query = $db->getQuery(true);
$query
->select('*')
->from($db->quoteName('#__users', 'u'))
->join('INNER', $db->quoteName('#__user_usergroup_map', 'm') . ' ON (' . $db->quoteName('u.id') . ' = ' . $db->quoteName('m.user_id') . ')')
->join('INNER', $db->quoteName('#__fields_values', 'f') . ' ON (' . $db->quoteName('u.id') . ' = ' . $db->quoteName('f.item_id') . ')')
->where($db->quoteName('group_id') . ' = ' . $group_id)
->order($db->quoteName('u.username') . ' ASC')
$db->setQuery($query);
$users = $db->loadObjectList();
And then output to this table:
<table style="width:100%">
<tr style="border-bottom:1pt solid black;text-align: left;">
<th>Name</th>
<th>CustomField1</th>
<th>CustomField2</th>
<th>Email</th>
</tr>
<?php
foreach($users AS $user)
{
?>
<tr>
<td><?php;
echo $user->name;
?></td>
</td>
<td><?php;
echo $user->value;
?></td>
<td><?php;
echo $user->NEED ANOTHER VALUE HERE;
?></td>
<td><?php;
echo $user->email;
}?>
</table>
Probably You can do something like this to concatenate the result. If you are not getting the exact result you can just play with the code for what result you want. Your fields will be in the field array.
I have used CONCAT and GROUP_CONCAT. And you can have a look at it here https://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php
$query = $db->getQuery(true)
->select(
array(
'*',
"CONCAT('[',GROUP_CONCAT(".$db->quoteName('f.value')."),']') field"
)
)
->from($db->quoteName('#__users', 'u'))
->leftJoin($db->quoteName('#__user_usergroup_map', 'm') . ' ON (' . $db->quoteName('u.id') . ' = ' . $db->quoteName('m.user_id'). ')')
->leftJoin($db->quoteName('#__fields_values', 'f') . ' ON (' . $db->quoteName('u.id') . ' = ' . $db->quoteName('f.item_id') . ')')
->where($db->quoteName('group_id') . ' = ' . $group_id);
$db->setQuery($query);
$users = $db->loadObjectList();
This will result in a singly array which you can do a var_dump() and check for yourself.
I'm combining about 20 excel sheets (user uploaded) using PHPExcel into a report. This is done by using this bit of code:
$filename = APP . 'docs' . DS . 'reports' . DS . $Workbooks[0]['Report']['company'] . '.' . $Workbooks[0]['Report']['report_name'] . '.' . $Workbooks[0]['Report']['extension'];
$blacklist = array('Company Organogram');
$MasterFile = $this->PhpExcel->createMasterFile($filename,$company . '.' . $title,$colour);
foreach($Workbooks as $Workbook) :
if(!in_array($Workbook['Report']['report_name'],$blacklist)) {
$file = APP . 'docs' . DS . 'reports' . DS . $Workbook['Report']['company'] . '.' . $Workbook['Report']['report_name'] . '.' . $Workbook['Report']['extension'];
$MasterFile = $this->PhpExcel->addExternalWorksheet($MasterFile,$file,$company . '.' . $title,$colour);
#unlink($file);
}
endforeach;
$this->PhpExcel->saveToDisk(APP . 'docs' . DS . 'reports' . DS . 'Weekly Reports for Week ' . (date('W') + 1) . '.xlsx',$MasterFile);
I'm working with CakePHP, so there is a helper. saveToDisk simply saves the file to the disc after which it is attached and sent to a mailing list.
All the above works fine. But I'm having issues where the combining process draws in extra lines. For instance, this:
becomes this:
How would I fix this? Is there something I should set so it doesn't draw in all the lines?
I'm trying to make a query to a database in Joomla 2.5. I have a db named 'example', and I'm trying to get certain value named 'value' (very original) for a user whose id is 949:
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$user = 949;
$db->setQuery( 'SELECT value FROM example WHERE user_id = ' . $user );
$result = $db->loadObjectList();
echo $result;
However, I'm just getting 'Array' as result (the expected value is a decimal, e.g. 4.5).
Could someone please tell me what am I doing wrong?
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$user = 949;
$db->setQuery( "SELECT value FROM example WHERE user_id = '" . $user."'" );
$result = $db->loadObjectList();
echo $result;
try this one
$db->loadObjectList() returns an array of objects, which echo can't display. If you want to just return one value from one row, user $db->loadResult() instead.
How to create select list, radio buttons, checkboxes with field_create_field() and how to specify the options to be given in these fields
Run this code with the details for an existing field with the properties that you want to copy:
$entity_type = 'node';
$field_name = 'body';
$bundle_name = 'article';
$info_config = field_info_field($field_name);
$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);
unset($info_config['id']);
unset($info_instance['id'], $info_instance['field_id']);
include_once DRUPAL_ROOT . '/includes/utility.inc';
$output = "field_create_field(" . drupal_var_export($info_config) . ");\n";
$output .= "field_create_instance(" . drupal_var_export($info_instance) . ");";
drupal_set_message("<textarea rows=30 style=\"width: 100%;\">". $output .'</textarea>');
That will produce the PHP code used to create the field/field instance. Then you just need to go through the code and make the changes for your new field/instance.