Drupal Tableselects - mysql

I can (finally!!!) set a query to return results for a checkbox(es) set within a collapsible fieldset in Drupal 7- however when I try to put it into a tableselect, I get no results. Would someone be able to check out this code and see if you can tell me why? I also have a screen shot of both results- but since I am new here it won't allow me to post it.
$form = array();
$secnum = 1;
$result = db_query('SELECT s.secser_id, s.ser_name FROM {secser} s WHERE s.sec_num = :secnum', array(':secnum' => $secnum));
$options = array();
foreach ($result as $record) {
$options[$record->secser_id] = $record->ser_name;
}
$form['secser']['1'] = array(
'#title' => t('Basic Sanitation'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$form['secser']['1']['secser'] = array(
'#title' => t('Choices'),
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#options' => $options,
'#description' => t('choose!'),
);
$form['secser']['2'] = array(
'#title' => t('Community Systems'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$secnum = 2;
$result = db_query('SELECT s.secser_id, s.ser_name FROM {secser} s WHERE s.sec_num = :secnum', array(':secnum' => $secnum));
$opt2 = array();
foreach ($result as $record) {
$opt2[$record->secser_id] = $record->ser_name;
}
$header = array(
'ser_name' => t('Choose Service(s)'),
);
$form['secser']['2']['secser'] = array(
'#type' => 'tableselect',
'#title' => t('Community Systems'),
'#header' => $header,
'#options' => array($opt2),
'#multiple' => TRUE,
);

I haven't tried running your code, but it looks like your options are an array which contains another array.
#options' => array($opt2),
because $opt2 is an array, this should be:
#options' => $opt2,
the same way you have the options set up in your checkboxes

Related

WP query retrieve the src of the attached image

I'm encoding in JSON a bunch of data from a WP Query:
$args = array(
'posts_per_page' => 20,
'post_type' => 'post',
'category' => 6,
'meta_key' => 'custom_total_hits',
'tag' => 'indie-pop',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
'after' => date('Y-m-d', strtotime('-40 days'))
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
foreach( $posts as $post ) {
$output[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'count' => $post->custom_total_hits,
'soundcloud_url' => $post->soundcloud_song,
'soundcloud_id' => $post->soundcloud_ids,
'link' => get_permalink($post),
);
}
echo json_encode($output);
I would like to display in my JSON a key corrisponding to the src of the medium size of the attached image. If I use 'images' => get_attached_media('image', $post->ID) it retrives an array of multiple data which I can not access since I don't know the ID of the attached image when I process the data of my JSON. How can I do to retrieve a first level key - value where the value is the src of the attached image?
get_post_thumbnail_id : Get post thumbnail ID
wp_get_attachment_url : Get attachment URL by attachment id
'images' => parse_url( wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ) );
Would you please try above code?
Try this solution:
$images = array();
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
foreach (get_intermediate_image_sizes() as $size) {
$images[$size] = wp_get_attachment_image_src($post_thumbnail_id, $size);
}
//end
'images' => $images // type_of_size => image_url

Dynamic value graph creation in yii2 using GoogleChart?

Here I wanna draw line chart graph by dynamic value.but in my case for every value of array created different different graph...Please help me I am first time do this task.Thanks in Advances
<?php
$modelEmployee=Employee::find()->select(['id','sales','expenses'])->all();
$arr = array('id'=>array(),
'sales'=>array(),
'expenses'=>array());
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
$arr['id'] = $modEm[$i]['id'];
$arr['sales'] = $modEm[$i]['sales'];
$arr['expenses'] = $modEm[$i]['expenses'];
print_r($arr);
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => array(
array('Year', 'Sales', 'Expenses'),
array($arr['id'],$arr['sales'],$arr['expenses']),
),
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));
?>
first of all the multiple graphs are because you are doing echo inside for loop so it will take only one value and create graph from that.
you have to create an array of values and pass it to the graph widget as following
$graph_data = [];
$graph_data[] = array('Year', 'Sales', 'Expenses');
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
$arr['id'] = $modEm[$i]['id'];
$arr['sales'] = $modEm[$i]['sales'];
$arr['expenses'] = $modEm[$i]['expenses'];
$graph_data[] = array($arr['id'],$arr['sales'],$arr['expenses']); //add the values you require as set in the order of Year, Sales , Expenses
} //loop ends here
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => $graph_data,
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));
Try this
action
$model=Employee::find()->select(['id','sales','expenses'])->all();
$data[]=["id","sales","expenses"];
foreach ($model as $item) {
$data[]=[(string) $item['id'],(int) $item['sales'],(int) $item['expenses']];
}
return $this->render('test',['data'=>$data]);
view
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => $data,
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));

Drupal 7 form insert if already exists show message like already existed filed

function countries_form($form, &$form_state,$id=0) {
if($id!=0){
$result = db_query('SELECT * FROM {countries} WHERE id = '.$id.'')->fetch();
// print_r($result);exit;
$form['country_name'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'Country name',
'#size' => 30,
'#maxlength' => 30,
'#default_value' => $result->country_name,
'#required' => TRUE, //make this field required
);
$form['description'] = array(
'#type' => 'textarea', //you can find a list of available types in the form api
'#title' => 'Description',
'#default_value' => $result->description,
'#required' => TRUE, //make this field required
);
$form['status'] = array(
'#type' => 'radios',
'#title' => t('Status'),
'#default_value' => $result->status,
'#options' => array(
'1' => t('Active'),
'0' => t('Inactive'),
),
);
}else{
$form['country_name'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'Country name',
'#size' => 30,
'#maxlength' => 30,
'#required' => TRUE, //make this field required
);
$form['description'] = array(
'#type' => 'textarea', //you can find a list of available types in the form api
'#title' => 'Description',
'#required' => TRUE, //make this field required
);
}
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
);
return $form;
}
function countries_form_submit($form, &$form_state) {
//$result = db_query('SELECT country_name FROM {countries}')->fetch();
//print_r($result);
if(arg(2)!=0){
$query = db_update('countries')->fields(array('country_name'=>$form_state['values']['country_name'],'description'=>$form_state['values']['description'],'status'=>$form_state['values']['status']))->condition('id',arg(2));
$query->execute();
// print_r($query);
drupal_set_message(t('Country %name has been updated.', array('%name' => $form_state['values']['country_name'])));
//print_r($description);
}else{
//$query = db_update('countries')->fields(array('country_name'=>$form_state['values']['country_name'],'description'=>$form_state['values']['description'],'status'=>$form_state['values']['status']))->condition('country_name','%s');
// print_r($query);
$query = db_insert('countries')->fields(array('country_name'=>$form_state['values']['country_name'],'description'=>$form_state['values']['description']));
//print_r($query);
$query->execute();
drupal_set_message(t('Country %name has been saved.', array('%name' => $form_state['values']['country_name'])));
}
//exit;
$form_state['redirect'] = 'mypages/table';
}
bu using druapl 7 form I am inserting and displaying date and editing and deleting as well all working fine..
now i want ..when inserting form we need to show message if already filed exists country name already taken like that..
there is not update query required only query for while inserting value check weather it is existed or not...
in that the below code is used or edit by passing arguments..
if(arg(2)!=0){
$query = db_update('countries')->fields(array('country_name'=>$form_state['values']['country_name'],'description'=>$form_state['values']['description'],'status'=>$form_state['values']['status']))->condition('id',arg(2));
$query->execute();
// print_r($query);
drupal_set_message(t('Country %name has been updated.', array('%name' => $form_state['values']['country_name'])));
//print_r($description);
}else{
in the else code inserting here we need to check weather existed or not

Save an object in wordpress database

it is possibe to save an object in prefix_options in wordpress database like this one:
$arr_params = array( 'cat' => $display_category, 'product' => $single_post_ID );
Thanks
edit:
after make some changes, the code can't add an new array in exsisting array in the database:
$item= array(
'name' => $name ,
'prename' => $prename
);
print_r($item);
$options = get_option( 'options' );
if (empty($options['items'])) {
$options['items']=array();
add_option( 'options', $options );
$options = get_option( 'options' );
$options['items'] = array_push($options['items'], "$item");
update_option( 'options', $options );
}
else{
$options = get_option( 'options' );
$options['items'] = array_push($options['items'], "$item");
update_option( 'options', $options );
}
yes you can,
$arr_params = array( 'cat' => $display_category, 'product' => $single_post_ID );
if( get_option("_arr_params") === false ) {
add_option("_arr_params", $arr_params);
}
else {
// holds : array( 'cat' => $display_category, 'product' => $single_post_ID );
$my_param = get_option("_arr_params");
}
According to Edit Part: array_push() on works to add one or more elements not the array, you can use array_merge() in place of it, or second option is i already used in below codes.
$options['wphyper_orders'][] = $order_detail;
helpful link : get_option()

Drupal Html array. How to reference a changing radio selection within a fieldset

I cannot make this Drupal html array fire the visible property on change of radio option. I have moved everything inside of the fieldset, not sure if this makes any difference.
Does anyone know why it isn't firing?
function services_formation_founders($form, &$form_state) {
$form = array();
$form['#tree'] = TRUE;
$form['description'] = array(
'#type' => 'item',
'#title' => t('Founders form'),
);
$form['founder']['add_officer'] = array(
'#type' => 'fieldset',
'#title' => t('Add Founder'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(TRUE,
),
),
);
$form['founder']['add_officer']['founder_type'] = array(
'#type' => 'radios',
'#options' => array(
'individual' => t('Individual'),
'corporate' => t('Corporation'),
),
'#default_value'=>'individual',
'#title' => t('What type of Founder?')
);
if (empty($form_state['num_names'])) {
$form_state['num_names'] = 1;
}
$form['founder']['add_officer']['individual'] = array(
'#type' => 'textfield',
'#title' => t('Individual'),
'#states' => array(
'visible' => array(
':input[name="founder_type"' => array('value' => "individual"),
),
),
);
$form['founder']['add_officer']['corporation'] = array(
'#type' => 'textfield',
'#title' => t('Corporation'),
'#states' => array(
'visible' => array(
':input[name="founder_type"' => array('value' => "corporate"),
),
),
);
return $form;
}
Place $ sign at line no 2
$form = array();