I am trying to created a block that saves a value from a select box. However, after reload, variable_get does not return the saved value... so variable_set seams not to be working.
What am I doing wrong here? (Drupal 6)
function get_courses(){
global $user;
$my_items_sql = 'SELECT course_node.uid, ';
//get the course nid
$my_items_sql .='course_node.nid as course_nid, ';
//get the course title
$my_items_sql .='course_node.title as course_title ';
$my_items_sql .=' from node as course_node where course_node.type="course" and course_node.uid = "'.$user->uid.'"';
$my_items_sql .=' order by course_node.nid; ';
$my_items_data= db_query($my_items_sql);
$my_courses = array();
while ($row = db_fetch_array($my_items_data)) {
//$course_node = node_load($row["course_nid"]);
$my_courses[$row["course_nid"]]=$row["course_title"];
}
//$variables['courses']=$my_courses;
//drupal_add_js(array('courses'=>$my_courses), "setting");
return $my_courses;
}
function front_page_block($op='list',$delta = 0, $edit = array()) {
$course_options = array();
$courses = get_courses();
$tmp = $courses;
$first_course = $tmp;
reset($first_course );
$first_course_nid = key($first_course);
$first_course_nid = key($first_course);
switch($op){
case 'list':
$blocks[0]['info']= t('Course Data Loader');
$blocks[0]['cache']= BLOCK_NO_CACHE;
return $blocks;
case 'configure':
$form['course_to_display'] = array(
'#type' => 'select',
'#description' => t('Display flashcards from which deck on the front page?'),
'#options' => $courses,
'#default_value'=>variable_get('front_page_deck_to_load', $first_course_nid)
);
return $form;
case 'save':
variable_set('front_page_deck_to_load',
$edit['front_page_deck_to_load']);
break;
}
At first check, what value is set for $edit['front_page_deck_to_load'], before call the
variable_set('front_page_deck_to_load', $edit['front_page_deck_to_load']); function.
If the value of $edit['front_page_deck_to_load'] is set properly, then variable_set is working.
Now the problem is, the '#default_value' of $form['course_to_display'] is not set properly. Please modify the get_courses() function as follows and try again:
function get_courses(){
global $user;
$my_items_sql = 'SELECT course_node.uid, ';
//get the course nid
$my_items_sql .='course_node.nid as course_nid, ';
//get the course title
$my_items_sql .='course_node.title as course_title ';
$my_items_sql .=' from node as course_node where course_node.type="course" and course_node.uid = "'.$user->uid.'"';
$my_items_sql .=' order by course_node.nid; ';
$my_items_data= db_query($my_items_sql);
$my_courses = array();
while ($row = db_fetch_array($my_items_data)) {
//$course_node = node_load($row["course_nid"]);
$my_courses[$row["course_title"]]=$row["course_title"];
}
//$variables['courses']=$my_courses;
//drupal_add_js(array('courses'=>$my_courses), "setting");
return $my_courses;
}
FYI: Please follow the Comments #1 at http://drupal.org/node/240783
Related
I am loading Laravel view and exporting as Excel using Maatwebsite/Laravel-Excel but my data showing as text but i need to make it as decimal, number. how can i do this. I have already read the documentation but there i cant find solution.
$fileName = 'Receipt Register : From '.date('d-m-Y', strtotime($date_from)).' To '.date('d-m-Y', strtotime($date_to)).($itemDetails ? ' For Item '.$itemDetails->item_code : "");
Excel::create($fileName, function( $excel) use($date_from, $date_to, $request) {
$excel->sheet('Receipt-Register', function($sheet) use($date_from, $date_to, $request) {
$itemDetails = [];
$itemFilterData = [];
$result = Ledger::where('receive_quantity', '!=', NULL)
->where('receive_quantity', ">", 0)
->orderBy('date', 'ASC')
->orderBy('mrn_number',"ASC")
->whereNotNull('mrn_number')
->whereNotNull('mrn_id')
->orderBy('id', "ASC")
->with('department', 'item', 'itemGroup', 'mrn')
->has('mrn', ">", 0);
if($request->date_from) {
$date_from = date('Y-m-d', strtotime($request->date_from));
$result = $result->whereDate('date', '>=', $date_from);
}
if($request->date_to) {
$date_to = date('Y-m-d', strtotime($request->date_to));
$result = $result->whereDate('date', '<=', $date_to);
}
if($request->item_id){
$result = $result->where('item_id', '=', $request->item_id);
$itemDetails = Item::find($request->item_id);
}
$results = $result->get();
$sheet->loadView('export_view',[
'results' => $results,
'date_from' => $date_from,
'date_to' => $date_to,
'itemFilterData' => $itemFilterData
]);
});
})->download('xlsx');
I am new in CakePHP. Now, I am using cakephp (version 2.8.5).
I try to create custom query pagination because I need to join multiple tables. But, its not work and I got Unsupported operand types error.
In my User model, I try to create paginate() and paginateCount() function as shown in below.
public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$recursive = -1;
// // Mandatory to have
// $this->useTable = false;
$sql = '';
$sql .= "SELECT u.id,u.name, u.password, r.display_name, GROUP_CONCAT(p.display_name SEPARATOR ', ') as per_name ";
$sql .= "FROM `users` u ";
$sql .= "LEFT JOIN roles r ON (u.role_id = r.id) ";
$sql .= "LEFT JOIN user_permission up ON (u.id = up.user_id) ";
$sql .= "LEFT JOIN permissions p ON (up.permission = p.id) ";
$sql .= "WHERE 1 GROUP BY u.name ORDER BY u.id ";
// Adding LIMIT Clause
$sql .= "LIMIT ".(($page - 1) * $limit) . ', ' . $limit;
$results = $this->query($sql);
return $results;
}
public function paginateCount($conditions = null, $recursive = 0,
$extra = array()) {
$sql = '';
$sql .= "SELECT COUNT(*) as count FROM
(SELECT u.id,u.name, u.password, r.display_name, GROUP_CONCAT(p.display_name SEPARATOR ', ') as per_name
FROM `users` u
LEFT JOIN roles r ON (u.role_id = r.id) LEFT JOIN user_permission up ON (u.id = up.user_id)
LEFT JOIN permissions p ON (up.permission = p.id)
WHERE 1
GROUP BY u.name
ORDER BY u.id ) AS Temp";
$this->recursive = $recursive;
$results = $this->query($sql);
return $results;
}
In my UsersController,
public function index() {
$users = $this->User->getAllUser();
//for pagination
$this->paginate = array(
'limit' => 4
);
$page = $this->paginate();
$count = $this->paginateCount();
$this->set('page',$page);
$this->set('rowCount',$count);
$this->set('users',$users);
$this->render('index');
}
But its not work and I got Unsupported operand types fatal error. I think may be I was wrong in here($this->paginate()). But actually not sure what is wrong in my code.
I already searching a lot of place on web and can't solve. I'm very appreciate for any suggestion.
Ok, now I understand what's wrong in my code.
Problem is not in controller. I was wrong when I return $results in paginateCount() function. The correct way to returning the result must be like return $results[0][0]['count']; in the paginateCount() function.
Because the query result in paginateCount() will return like this:
array(1) { [0]=> array(1) { [0]=> array(1) { ["count"]=> string(1) "5"
} } }
Complete code for paginateCount() is as shown in below.
public function paginateCount($conditions = null, $recursive = 0,
$extra = array()) {
$sql = '';
$sql = "SELECT COUNT(*) as count FROM
(SELECT User.id,User.name, User.password, r.display_name, GROUP_CONCAT(p.display_name SEPARATOR ', ') as per_name
FROM `users` User
LEFT JOIN roles r ON (User.role_id = r.id) LEFT JOIN user_permission up ON (User.id = up.user_id)
LEFT JOIN permissions p ON (up.permission = p.id)
WHERE 1
GROUP BY User.name
ORDER BY User.id ) AS Temp";
$this->recursive = $recursive;
$results = $this->query($sql);
return $results[0][0]['count'];
}
When writing queries or running through result sets, I'm constantly having to refer to fields as "field_id_X". I want to believe there is a saner way to go about this than defining a CONST for every field_id/name pair.
define(NAME_FIELD ,'field_id_3');
define(HEIGHT_FIELD, 'field_id_4');
foreach( $result as $row ){
$name = $row[NAME_FIELD]; // :(
}
Get an Array for field id's and names...
function getFieldReferences() {
$sql = "SELECT field_id, field_name
FROM exp_channel_fields
WHERE site_id = ".$this->EE->config->item('site_id');
$result = $this->EE->db->query($sql);
if ($result->num_rows() > 0) {
$result = $result->result_array();
$finalResult = array();
foreach ($result as $row)
$finalResult[$row["field_id"]] = $row["field_name"];
return $finalResult;
} else {
return false;
}
}
Example conversion of a specific entry details $entry_id...
$sql = "SELECT exp_channel_data.*, exp_channel_titles.*, exp_channels.channel_name
FROM exp_channel_data, exp_channel_titles, exp_channels
WHERE exp_channel_data.entry_id = $entry_id
AND exp_cart_products.entry_id = $entry_id
AND exp_channel_titles.entry_id = $entry_id
LIMIT = 1";
$result = $this->EE->db->query($sql);
if ($result->num_rows() > 0) {
$result = $result->result_array();
$result = $result[0];
//### Get Field Titles ###
$fieldReferences = getFieldReferences();
//### Replace Field ID reference with name ###
foreach ($result as $key => $value) {
if (substr($key,0,9) == "field_id_") {
$result[$fieldReferences[substr($key,9)]] = $value;
unset($result[$key]);
}
if (substr($key,0,9) == "field_ft_")
unset($result[$key]);
}//### End of foreach ###
}
Convert Member fields to names based on specified member $id...
$sql = "SELECT m_field_id, m_field_name
FROM exp_member_fields";
$result = $this->EE->db->query($sql);
if ($result->num_rows() > 0) {
$memberFields = $result->result_array();
$sql = "SELECT exp_member_data.*, exp_members.email
FROM exp_member_data, exp_members
WHERE exp_member_data.member_id = $id
AND exp_members.member_id = $id
LIMIT 1";
$result = $this->EE->db->query($sql);
if ($result->num_rows() > 0) {
$result = $result->result_array();
$rawMemberDetails = $result[0];
//### Loop through each Member field assigning it the correct name ###
foreach($memberFields as $row)
$memberDetails[ $row['m_field_name'] ] = $rawMemberDetails['m_field_id_'.$row['m_field_id']];
}
You could look up exp_channel_fields.field_name (or field_label) by the field_id you have from exp_channel_data.
Can you provide an example of batch_mutate() function in phpcassa?
Cant understand how to work with this function and didnt found any enough information.
Also i want to know how to use it with counters
Thanks in advance.
"delete data from multiple keys":
function batch_remove($key=null, $columns=null, $super_column=null, $write_consistency_level=null) {
$timestamp = CassandraUtil::get_time();
$deletion = new cassandra_Deletion();
$deletion->timestamp = $timestamp;
if ($super_column !== null) $deletion->super_column = $this->pack_name($super_column, true);
else $deletion->super_column = null;
if ($columns !== null) {
$predicate = $this->create_slice_predicate($columns, '', '', false, self::DEFAULT_COLUMN_COUNT);
$deletion->predicate = $predicate;
}
$mutation = new cassandra_Mutation();
$mutation->deletion = $deletion;
if (is_array($key) && count($key) >= 1) {
$mut_map = array();
foreach($key as $v) {
$packed_key[$v] = $this->pack_key($v);
$mut_map[$v] = array($this->column_family => array($mutation));
}
return $this->pool->call("batch_mutate", $mut_map, $this->wcl($write_consistency_level));
} else return false;
}
//delete name1, name2, name3 from key1, key2 in a single call
$column_family->batch_remove(array(key1, key2), array(name1, name2, name3));
batch_mutate on counters is not available yet with PHPCassa: https://github.com/thobbs/phpcassa/issues/31
batch_mutate in action, as per the tutorial: http://thobbs.github.com/phpcassa/tutorial.html
"inserting data":
$row1 = array('name1' => 'val1', 'name2' => 'val2');
$row2 = array('foo' => 'bar');
$column_family->batch_insert(array('row1' => $row1, 'row2' => $row2);
I have a problem with my CakePhp Site.
My index is showing fine with pagination, but when I search the page, pagination is gone and the search results aren't showing (instead the index is showing without paging).
Any idea where my code is wrong?
class ItemsController extends AppController {
var $name = 'Items';
// load any helpers used in the views
var $helpers = array('Paginator', 'Html', 'Form', 'Javascript', 'Misc', 'Time', 'Tagcloud');
var $components = array('RequestHandler');
/**
* index()
* main index page for items
* url: /items/index
*/
function index() {
// get all options for form
$tags = $this->Item->Tag->find('list', array(
'fields'=>'id, name',
'order'=>'Tag.name',
'conditions'=> array(
'Tag.status'=>'1'
)
));
// add name to option
$tags = array(''=>'Tags') + $tags;
//pr($tags);
// if form submitted
if (!empty($this->data)) {
// if reset button pressed redirect to index page
if(isset($this->data['reset'])) {
$this->redirect(array('action'=>'index'));
}
// init
$url = '';
// remove search key if not set
if($this->data['search'] == '') {
unset($this->data['search']);
}
// loop through filters
foreach($this->data as $key=>$filter) {
// ignore submit button
if($key != 'filter') {
// init
$selected = '';
switch($key) {
case 'tag':
$selected = $tags[$filter];
break;
case 'search':
$selected = $filter;
break;
}
// if filter value is not empty
if(!empty($filter)) {
$selected = $this->slug($selected);
$url .= "/$key/$selected";
}
}
}
// redirect
$this->redirect('/items/index/'.$url);
} else {
// set form options
$this->data['tag'] = '';
$this->data['search'] = '';
}
// if any parameters have been passed
if(!empty($this->params['pass'])) {
// only select active items
$conditions = array('Item.status'=>1);
// get params
$params = $this->params['pass'];
// loop
foreach($params as $key=>$param) {
// get the filter value
if(isset($params[$key+1])) {
$value = $params[$key+1];
}
// switch on param
switch($param)
{
case 'tag':
// get tag
$tag = $this->Item->Tag->find('first', array(
'recursive' => 0,
'conditions' => array(
'Tag.slug'=>$value
)
));
// save value for form
$this->data['tag'] = $tag['Tag']['id'];
break;
case 'search':
// setup like clause
$conditions['Item.name LIKE'] = "%{$value}%";
// save search string for form
$this->data['search'] = str_replace('_', ' ', $value);
break;
}
}
//pr($conditions);
// get all items with param conditions
$items = $this->Item->find('all', array(
'order' => 'Item.name',
'conditions' => $conditions
));
// if tag filter has been set
if(isset($tag)) {
// loop through items
foreach($items as $key=>$item) {
// init
$found = FALSE;
// loop through tags
foreach($item['Tag'] as $k=>$g) {
// if the tag id matches the filter tag no need to continue
if($g['id'] == $tag['Tag']['id']) {
$found = TRUE;
break;
}
}
// if the tag was not found in items
if(!$found) {
// remove from list
unset($items[$key]);
}
}
}
} else {
// get all items from database where status = 1, order by name
$items = $this->Item->find('all', array(
'order' => 'Item.name',
'conditions' => array(
'Item.status'=>1
)
));
}
$this->paginate = array(
'limit' => 10
);
$data = $this->paginate('Item');
// set page title
$this->pageTitle = 'Index Page';
// set layout file
$this->layout = 'index';
// save the items in a variable for the view
$this->set(compact('data', 'tags', 'items'));
}
You'll want to pass your search conditions to the paginate functionality. You do this through the controller's paginate property.
function index() {
...
switch($param) {
...
case 'search':
$this->paginate['conditions']['Item.name LIKE'] = "%{$value}%";
More information on setting up pagination can be found here.