How To Create Dynamic Array These Value Use Foreach? - multidimensional-array

$data_array = array(
array(
'ID',
'Name',
'Designation ',
'Message'
)
);
$sql = mysql_query("SELECT * FROM tbl_name");
while($data = mysql_fetch_object($sql))
{
array_push($data_array, array(
$data->id,
$data->name,
$data->designation,
$data->message
));
}
How can i create dynamic array for any sql table fields or data like in data array take fields value dynamically ?

Related

get the user id from user meta in WordPress using the custom meta key

I need to get the subscribers user details based on the selection in the filter for this i have tried the below code
$status = join('","',$status);
$distribusjon = join('","',$distribusjon);
$qrys = '
AND (
(utrop_usermeta.meta_key = "status" AND utrop_usermeta.meta_value IN ("'.$status.'")) OR
(utrop_usermeta.meta_key = "mepr_distribusjon" AND utrop_usermeta.meta_value IN ("'.$distribusjon.'"))
)
';
$users = $wpdb->get_results('SELECT DISTINCT(ID) FROM '.$wpdb->users.' u LEFT JOIN '.$wpdb->usermeta.' m ON m.user_id = u.ID WHERE 1 AND ID IN ( SELECT user_id FROM '.$wpdb->usermeta.' WHERE 1 '.$qrys.') AND meta_key = "utrop_capabilities" AND meta_value REGEXP "[[:<:]]s:10:\"subscriber\";"');
You can try this method.
List all users:
$users = get_users();
Filter for role (ex. subscriber):
$users = get_users('role=subscriber');
Exclude for id user:
$users = get_users('exclude=1');
And You can filter by WP_User_Query
$q = new WP_User_Query(
array(
'role' => 'Your_role',
'meta_query' => array(
array(
'key' => 'Your_custom_meta_key',
'value' => 'Your_meta_value',
'compare' => 'LIKE'
)
)
)
);
Thanks

Get users in WordPress with an array of exceptions

If I wanted to get a list of users in WordPress with an exception of users, how would I do this? So to get a complete list of users I could write a query like:
$dont_include = array(1,5,9); // IDs not to include
$query = "SELECT * FROM wp_users WHERE ID > 0 AND ID != %d";
$data = array(
$dont_include
);
$query = $wpdb->prepare($query, $data);
$results = $wpdb->get_results($query);
You can do it easily with the WordPress function get_users and the exclude parameter:
$dont_include = array(1,5,9); // IDs not to include
$args = array(
'exclude' => $dont_include,
'orderby' => 'login',
'order' => 'ASC',
'fields' => 'all',
);
$users = get_users($args);
exclude - An array of IDs. Users matching these IDs will not be returned, regardless of the other arguments. It will be ignored if the include argument is set.
More details get_users

Drupal EntityFieldQuery group results by taxonomy

I have an entity bundle called Map Points that has a taxonomy term reference field of countries. I'm using EntityFieldQuery in a custom module to list out all the map point entities but I need to group them by the country reference field.
Here is the code I have right now which filters the query by tid 1 and I know I can repeat the query for each country to group the results but I'm looking for a more refined way to group the results by the country term id's without having to write a new query for each country.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'givepower')
->entityCondition('bundle', 'map_points')
->propertyCondition('type', 'map_points', '=')
->fieldCondition('field_map_point_country', 'tid', '1');
$result = $query->execute();
// set empty map point id array
$map_point_ids = array();
// loop through all givepower entities
foreach($result['givepower'] as $record) {
$map_point_ids[] = $record->id;
}
// load entities
$map_point = entity_load('givepower', $map_point_ids);
// set entity view
$entities = entity_view('givepower', $map_point);
return $entities;
I believe I figured this out. Here's the finished code:
//load recent_work_countries taxonomy
$vocabulary = taxonomy_vocabulary_machine_name_load('recent_work_countries');
// load all terms in the recent_work_countries taxonomy
$terms = entity_load('taxonomy_term', FALSE, array('vid' => $vocabulary->vid));
// set empty countries array
$countries = array();
// loop through each recent_work_countries term
foreach($terms as $tid => $value) {
// set country term name and term id(tid) vars
$country_name = $terms[$tid]->name;
$country_tid = $terms[$tid]->tid;
// add each country to the counties array
$countries[$country_name] = array();
// create new query to grab all map point entities with the current recent_work_countries tid
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'givepower')
->entityCondition('bundle', 'map_points')
->propertyCondition('type', 'map_points', '=')
->fieldCondition('field_map_point_country', 'tid', $country_tid);
$result = $query->execute();
// set empty tab id array
$map_point_ids = array();
// loop through all givepower entities that have the map_point_ids
foreach($result['givepower'] as $record) {
$map_point_ids[] = $record->id;
}
// load entities
$map_point = entity_load('givepower', $map_point_ids);
// set entity view
$entities = entity_view('givepower', $map_point);
// loop through each entities results
foreach($entities['givepower'] as $eid => $e_val) {
// add entity result data to countries array
$countries[$country_name][] = array(
'title' => $e_val['field_map_point_title']['#items'][0]['safe_value'],
'description' => $e_val['field_map_point_description']['#items'][0]['safe_value'],
'latitude' => $e_val['field_map_point_latitude']['#items'][0]['safe_value'],
'longitude' => $e_val['field_map_point_longitute']['#items'][0]['safe_value'],
);
}
}
This will create an array that looks like this:
Array (
[Kenya] => Array (
[0] => Array (
[title] => Test Map Point 1
[description] => lorum ipsum
[latitude] => 1.765404
[longitude] => 40.079880 )
[1] => Array (
[title] => Test Map Point 1
[description] => Lorum ipsum
[latitude] => 0.633657
[longitude] => 37.350050 ) )
[Haiti] => Array (
[0] => Array (
[title] => GA University
[description] => lorum ipsum
[latitude] => 18.574420
[longitude] => -72.310981 ) )
[Nepal] => Array ( )
[Ghana] => Array ( )
)

Content filter does not search drupal7

I have added a drop down of my courses title at the filter second of the drupal - admin - content area.
http://www.XYZ.com/demo/admin/content
But when I select any of the title and hit Filter nothing gets appear. The data which showed previously it shows again, nothing happens really.
My code for the add filter drop down:
function products_form_node_admin_content_alter(&$form, &$form_state){
$results = db_query("SELECT r.nid, r.title FROM {node} AS n
LEFT JOIN {node_revision} AS r ON r.nid = n.nid
WHERE type = 'product'")->fetchAll();
$optionsF = Array ( '[any]' => 'any' );
foreach($results as $key => $result) {
$options[$result->title] = $result->title;
}
$options = $optionsF + $options;
$course_titles['title'] = Array
(
'#type' => 'select',
'#options' => $options,
'#title' => 'title',
'#default_value' => 'any'
);
$form['filter']['filters']['status']['filters'] = $form['filter']['filters']['status']['filters'] + $course_titles;
$uid_column = array('uniqueid' => array(
'data' => 'UniqueID',
'field' => 'n.nid'
));
$form['admin']['nodes']['#header'] = $form['admin']['nodes']['#header']+$uid_column;
foreach ($form['admin']['nodes']['#options'] as $key => $row) {
$node = node_load(array('nid' => check_plain($key)));
$form['admin']['nodes']['#options'][$key]['uniqueid'] = $node->field_unique_code_course['und'][0]['value'];
}
}
Does any body have any idea what is lacking in my code or method?
Cheers!!!
I would recommend using Admin Views, it's built for this purpose.
https://drupal.org/project/admin_views

Drupal 6 : Not populating select list from db_query()

I have made a custom form in a module where I have used a select list & I am trying to populate that with the name of OG name.
I wrote a function for db_query() & that is giving me exact out put but I am not able to populate that in from select.
Function for db_query():-
function taskform_project_select(){
$options=array();
$project_query = "SELECT node.title FROM {node}, {og} WHERE node.nid = og.nid";
$project_details = db_query($project_query);
while($project_title = db_fetch_object($project_details)){
$options = $project_title->title;
dpm($options);
}
return $options;
}
Code in Form:-
$options = taskform_project_select();
$form['edproject'] = array(
'#type' => 'select',
'#title' => t('Project'),
'#options' => $options,
'#description' => t('Choose a project'),
'#prefix' => '<td>',
'#suffix' => '</td>',
);
Thanks :)
Correct syntax is:
$options[] = $project_title->title;
..with square brackets.
$options[$project_title->title] = $project_title->title;
provides a meaningful key value to the result. You could also retrieve the nid and use that as the key.

Resources