I need to use updateall to update position and column of widget
if (!empty($column3[0])) {
$column = preg_split("/,/", $column3[0]);
$name = $column[2];
switch ($name) {
case 'My Profile':
$table = 'WidgetProfile';
break;
case 'My script':
$table = 'WidgetScript';
break;
case 'My Setting':
$table = 'WidgetSetting';
break;
case 'Upload Script':
$table = 'WidgetUpload';
break;
case 'My message':
$table = 'WidgetMessages';
break;
default :
echo "Error3 0";
}
$this->loadModel($table);
$row = $this->$table->find('count',array('conditions'=>array('user_id'=>'$id));
if($row==0){
$this->$table->set(array('column',=> 3, 'position' => 1,'user_id'=>$id));
$this->$table->save();
}else{
$this->$table->updateAll(**?????????????**);
}
how to use updateAll for it
It's not very clear from the question what you're trying to do, but it looks like you're trying to update an existing record, or if there isn't a record already, create one. You can use Model::save() for both. If the model's id is set it will update, otherwise it'll insert a new row.
$row = $this->$table->find(
'first',
array(
'conditions' => array( 'user_id'=> $id ),
'fields' => "$table.id",
'recursive' => -1
)
);
if( !empty( $row ) ) {
$this->$table->id = $row[ $table ][ 'id' ];
}
$this->$table->save( array('column' => 3, 'position' => 1, 'user_id'=> $id ) );
Related
I'm trying to update multiple rows once, I use the array to store the data and I use for loop to try to update the data but it's not working because the data in the database is still the same. What am I doing wrongly?
global $wpdb;
$table_name = $wpdb -> prefix . 'test';
$check_first = 1;
$check_second = 2;
$update = array (
array ( 'value' => $check_first ),
array ( 'value' => $check_second )
);
$condition = array (
array ( 'name' => 'enable' ),
array ( 'name' => 'picked' )
);
for ( $x = 0; $x <= 2; $x++ ) {
$wpdb -> update ( $table_name, $update, $condition );
}
You only need to set array index in 'update' and 'condition' like this -
global $wpdb;
$table_name = $wpdb->prefix . 'test';
$update = array(
array( 'value' => 3 ),
array( 'value' => 4, 'state' => 'asdf' )
);
$condition = array(
array( 'name' => 'enable' ),
array( 'name' => 'picked' )
);
for ( $i = 0; $i < sizeof( $condition ); $i++ ) {
$wpdb->update( $table_name, $update[$i], $condition[$i] );
}
This will update line by line.
how to fetch data from database in drupal7:
fields i have name:
subject:
email:
message:
give me the code for drupal 7 i want it in table form.
my insert code is this:
function form_example_form_submit($form, &$form_state) {
echo $name = $form_state['values']['textfield'];
echo $email = $form_state['values']['mail'];
echo $subject = $form_state['values']['subject'];
echo $message = $form_state['values']['message'];
echo $ip=ip_address();
echo $cb=$name;
//echo $timestamp = REQUEST_TIME;
echo $time=time();
$nid=db_insert('form') // Table name no longer needs {}
->fields(array(
'name' => $name,
'email' => $email,
'subject' => $subject,
'message' => $message,
'ip' => $ip,
'created_by' => $cb,
//'created_at' => $time,
))
->execute();
//print_r($nid);
drupal_set_message(t('The form has been submitted.'));
}
how can i fetch the data from database in drupal 7 in the form of table ,
give me the code .i m newbie in drupal 7 so its very difficult forme
Here is the snippet to fetch all content from "form" table with table format and pager. Also you can add condition which I have added in comment if required.
<?php
// Set header
$header = array(
array('data' => t('Name'), 'field' => 'name'),
array('data' => t('Email'), 'field' => 'email'),
array('data' => t('Subject'), 'field' => 'subject'),
array('data' => t('Message'), 'field' => 'message'),
);
//query to fetch all content
$query = db_select('form', 'f');
$query->fields('f');
//$query->condition('f.name', $search_name, '=') //if needed
$table_sort = $query->extend('TableSort') // Table sort extender
->orderByHeader($header); // Order by headers
$pager = $table_sort->extend('PagerDefault')
->limit(20); // Set page limit
$arr_result = $pager->execute();
$rows = array();
foreach($arr_result as $result) {
$rows[] = array(
$result->name,
$result->email,
$result->subject,
$result->message,
);
}
// Set empty output
$output = '';
if (!empty($rows)) {
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
/*'attributes' => array(
'id' => 'sort-table' // add if want to add sorting
) */
));
$output .= theme('pager');
}
else {
$output .= t("No results found.");
}
return $output;
?>
Let me know if any query/confusion occurs for the same.
I am trying to use the count function , this is what it looks like :
$sql = "SELECT COUNT(Live) as c FROM tapplicant WHERE CompletedDate >= CURDATE() ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo $row['c'] ;
How do i conver this using the cakePHP way ? , i have tryed :
$count = $this->Article->find('count',
array('conditions' => array('Tapplicant.Live')));
Then to view the value of $count :
<?php echo $count ?>
I have tryed :
$this->Tapplicant = array(
'c' => 'COUNT(*)',
);
$options = array(
'fields' => array(
'Tapplicant.c',
),
);
$data = $this->find('all', $options);
$this->set('data', $data );
Basically im just trying to count the value of tapplicant.Live , there are 5 records in it.
You almost have it. In your condition, the value array should have 2 fields. 1st the column name, 2nd the value of the condition.
Considering you've binded correctly the Tapplicant table to your Article model :
$count = $this->Article->find('count', array('conditions' => array('Live >=' => 'CURDATE()' )));
Or just this, if you want to count all lines :
$count = $this->Article->find('count');
You need to set the conditions array like this:-
$count = $this->Article->find(
'count',
array(
'conditions' => array(
'Live >=' => 'CURDATE()'
)
)
);
I'm trying to create a module that will display some last entries from database. I'd like to send last entry object to template file (guestbook-last-entries.tpl.php), that looks like that
<p><?php render($title); ?></p>
<?php echo $message; ?>
I have a function that implements hook_theme
function guestbook_theme() {
return array(
'guestbook_last_entries' => array(
'variables' => array(
'entries' => NULL,
),
'template' => 'guestbook-last-entries'
),
);
}
one that do preprocess
function template_preprocess_guestbook_last_entries(&$variables) {
$variables = array_merge((array) $variables['entries'], $variables);
}
and functions that implements hook_block_view
function guestbook_block_view($delta = '') {
switch ($delta) {
case 'guestbook_last_entries':
$block['subject'] = t('Last entries');
$block['content'] = array();
$entries = guestbook_get_last_entries(variable_get('guestbook_m', 3));
foreach ($entries as $entry) {
$block['content'] += array(
'#theme' => 'guestbook_last_entries',
'#entries' => $entry,
);
}
break;
}
return $block;
}
function that gets data from database
function guestbook_get_last_entries($limit = 3) {
$result = db_select('guestbook', 'g')
->fields('g')
->orderBy('posted', 'DESC')
->range(0, $limit)
->execute();
return $result->fetchAllAssoc('gid');
}
But in this case I get only one entry displayed. Can anyone tell me how to resolve this, how should I build that $block['content']?
Thank you
This here wont work:
$block['content'] += array(
'#theme' => 'guestbook_last_entries',
'#entries' => $entry,
);
Maybe you want this if you need an array as the result:
// note that I replaced += with a simple = and added two brackets that will create a new element in that array $block['content']
$block['content'][] = array(
'#theme' => 'guestbook_last_entries',
'#entries' => $entry,
);
I am trying to build my own node reference custom field - I know several projects out there already do this, I am building this in order to learn... :)
My problem is the autocomplete path, it's not being triggered, I have checked the noderefcreate project and implemented my solution based on that project. But still; nothing is being triggered when I check firebug.
Here is my code:
function nodereference_field_widget_info() {
return array(
'nodereference_nodereference_form' => array(
'label' => t('Node Reference Form'),
'field types' => array('nodereference'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
'settings' => array(
'autocomplete_match' => 'contains',
'autocomplete_path' => 'nodereference/autocomplete',
),
),
);
}
function nodereference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
if ($instance['widget']['type'] == 'nodereference_nodereference_form') {
$widget = $instance['widget'];
$settings = $widget['settings'];
$element += array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['nid']) ? $items[$delta]['nid'] : NULL,
'#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'],
);
}
return array('nid' => $element);
}
You need to define method from where auto compete will take suggestions.
It can be done like this:
/**
* implements hook_menu
*/
function your_module_name_menu() {
$items['master_place/autocomplete'] = array(
'page callback' => '_your_module_name_autocomplete',
'access arguments' => array('access example autocomplete'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Auto complete method implementation
* #param $string
*/
function _your_module_name_autocomplete($string) {
$matches = array();
// Some fantasy DB table which holds cities
$query = db_select('target_db_table', 'c');
// Select rows that match the string
$return = $query
->fields('c', array('target_column'))
->condition('c.target_column', '%' . db_like($string) . '%', 'LIKE')
->range(0, 10)
->execute();
// add matches to $matches
foreach ($return as $row) {
$matches[$row->city] = check_plain($row->city);
}
// return for JS
drupal_json_output($matches);
}