Strict standards issue with Drupal 7 custom module - drupal-7

The following error message is displayed when I visit the homepage:
Strict Standards: Only variables should be passed by reference in C:\wamp\www\vb-15-05-2018-2\sites\all\modules\vb_ymlp\vb_ymlp.module on line 40
This is coming from a custom module and the line within the if statement is the issue. Does this point to a bigger issue or is there a simple solution?
function vb_ymlp_block_view($delta = ""){
module_load_include('inc', 'vb_ymlp', 'vb_ymlp.form');
$block = array();
if($delta == 'vb_ymlp_block'){
// the line below is causing this error
$block['content'] = drupal_render(drupal_get_form('vb_ymlp_form'));
}
return $block;
}

You are not able to set value as function as you set drupal_render()
You have to get it first and then set it to the content variable
Like you have to do
function vb_ymlp_block_view($delta = ""){
module_load_include('inc', 'vb_ymlp', 'vb_ymlp.form');
$block = array();
if($delta == 'vb_ymlp_block'){
// the line below is causing this error
$content = drupal_get_form('vb_ymlp_form');
$content = drupal_render($content);
$block['content'] = $content;
}
return $block;
}
HOPE THIS SOLVE YOUR PROBLEM
THANKS

Related

CodeIgniter : Undefined index in model variable

I have the below code in of of my models. Im loading the offer details page and it gave me the error
Severity: Notice
Message: Undefined index: viewc
Filename: statistics/Statistic_model.php
Line Number: 551
Statistic_model:
public function getTotalDetailsViewOfActiveOffer($comid) {
$this->db->select('count(statistic_offer_view_count.id) as viewc');
$this->db->from('statistic_offer_view_count');
$this->db->join('offers', 'offers.id = statistic_offer_view_count.offer_id');
$this->db->where('offers.com_id',$comid);
$this->db->where('offers.approved_status',"2");
$this->db->where('offers.enddate >=',date('Y-m-d'));
$this->db->group_by("offers.com_id");
$query = $this->db->get();
$row = $query->result_array();
$count=$row['viewc']; //this is line 551
}
Any idea how to fix this error?
That error means that the index viewc is not in the array $query->result_array() most likely because your query failed or returned null. You should get into the habit of doing:
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row()->viewc;
}
return false; // or in this case 0 or whatever
In Your Code You Selecting data from database Multiple array's.And assigning at to a variable like a single array.
Your Code....
$row = $query->result_array();
$count=$row['viewc'];
correct way is in your conduction to assign a variable is....
$data = array();
foreah($row as $key => $value){
$data[$key] = $value['viewc'];
}

Find all in CakePHP is dumping a semicolon in my view

I don't know what exactly is happening with my CakePHP app. It worked last week and I have literally changed nothing with that particular file.
When I use find 'all' in my controller it dumps a semi-colon in my view even if there is nothing in the view file.
Here is my code
$evts = $this->Event->find('all');
There is nothing in my view file. I don't know if it makes a difference but I'm using a json view.
As requested here is the complete code for the action
public function search(){
$this->Event->recursive = 2;
$conditions = array();
if(!empty($this->request->query['name'])){
$conditions = array('Event.name LIKE ' => "%" . str_replace(" ","%", $this->request->query['name']) . "%");
}
if(!empty($this->request->query['home'])){
$conditions = array('Event.home_team_id' => $this->request->query['home']);
}
if(!empty($this->request->query['away'])){
$conditions = array('Event.away_team_id' => $this->request->query['away']);
}
$limit = 25;
if(!empty($this->request->query['limit'])){
$limit = $this->request->query['limit'];
}
//$evts = $this->Event->find('all',array('conditions'=>array($conditions),'order' => array('Event.start_time'),'limit'=>$limit));
$evts = $this->Event->find('all');
$this->set('events',$evts);
}
Everything in the view has been commented out ... but here is the code anyway
$results = array();
$i = 0;
foreach ($events as $event) {
$results[$i]['id'] = $event['Event']['id'];
$results[$i]['label'] = $event['Event']['name'] . "(" . date_format(date_create($event['Event']['start_time']), 'D, jS M Y') . ")";
$results[$i]['value'] = $event['Event']['name'];
$results[$i]['home_team_name'] = $event['HomeTeam']['name'];
$results[$i]['away_team_name'] = $event['AwayTeam']['name'];
$results[$i]['sport_name'] = $event['Sport']['name'];
$results[$i]['tournament_name'] = $event['Tournament']['name'];
$results[$i]['start_time'] = $event['Event']['start_time'];
$results[$i]['img'] = $event['Event']['img_path'];
$results[$i]['listener_count'] = 0; //TODO Get the follower count
$i++;
}
echo json_encode($results);
Display
If you changed nothing with that particular file then perhaps you have installed callbacks (either in the controller or in the model) that echo that ";". Look for 'afterFind' calls...
Search your model and your controller folders for "echo statement". In fact you should search your entire app folder except for the Views folder.

using same array in different functions

Here's the situation: I have 2 different functions and one view. I need to send data from function 1 to the view (2 arrays), and from that view i need to send data to function 2 (1 array).
Sending data from function 1 to the view is an easy job, but i don't know how to do it with function 2 because the information i would like to send to it has a function 1's array plus other new data the users entry.
I know there's no chance to send an array through URL, but i'm out of ideas.
Which is the best option for passing the data?
i send to the view this data:
admin: (part of the function 1)
//last part of the code
$this->data['conditions'] = $conditions;
$this->data['notselectedconditions'] = $notselectedconditions;
$this->parser->parse('admin/tools/showReport.tpl',$this->data);
In the view i use the information on those arrays, and user can put some new entry.
view:
<dt>Tipo de Usuario<dt>
<dd>Pre Registrado</dd>
reloadConditions is function 2, the one that needs what function 1 provides plus the selection of the user in order to keep filtering the results.
The information i need to have available on function 2 is: array 'conditions' and users new entry
After a long discussion and some clarifications in chat, here is the solution I came up with:
<?php
function addQueryParameters($url, $params) {
$paramsStr = http_build_query($params);
$fragment = parse_url($url, PHP_URL_FRAGMENT);
$query = parse_url($url, PHP_URL_QUERY);
$interUrl = '';
if ($query === NULL) {
$interUrl = '?';
}
if ($fragment !== NULL) {
$interUrl = '&';
}
$interUrl .= $paramsStr;
if ($fragment !== NULL) {
$pos = strpos($url, '#' . $fragment);
$url = substr($url, 0, $pos) . $interUrl . substr($url, $pos);
}
return $url;
}
function linkifyFilterConditions($url, $conditions) {
return addQueryParameters($url, array('conditions' => $conditions));
}
// Tests
// TODO: replace by unit tests
var_dump(linkifyFilterConditions('http://example.com/blub', [1,2,3]));
var_dump(linkifyFilterConditions('http://example.com/blub#a', [1,2,3]));
var_dump(linkifyFilterConditions('http://example.com/blub?a=b#a', [1,2,3]));
Usage (in OP's views):
$href = 'yourscript?conditions[]=a';
$href = linkifyFilterConditions($href, $conditions);
echo '<a href="' . $href . '">Test</>';
How about puting you arrays in $_SESSION?
see here http://www.phpriot.com/articles/intro-php-sessions/7

How do I use fixed fields in CakeDC's csvUpload behavior in Util plugin

I am using the csvUpload behavior of the Utils plugin by CakeDC, on a CakePHP 2.2.1 install.
I have it working great it's processing a rather large csv successfully. However there are two fields in my table / Model that would be considered fixed, as they are based on ID's from from associated models that are not consistent. So I need to get these fixed values via variables which is easy enough.
So my question is, how do I use the fixed fields aspect of csvUpload? I have tried that following and many little variation, which obviously didn't work.
public function upload_csv($Id = null) {
$unique_add = 69;
if ( $this->request->is('POST') ) {
$records_count = $this->Model->find( 'count' );
try {
$fixed = array('Model' => array('random_id' => $Id, 'unique_add' => $unique_add));
$this->Model->importCSV($this->request->data['Model']['CsvFile']['tmp_name'], $fixed);
} catch (Exception $e) {
$import_errors = $this->Model->getImportErrors();
$this->set( 'import_errors', $import_errors );
$this->Session->setFlash( __('Error Importing') . ' ' . $this->request->data['Model']['CsvFile']['name'] . ', ' . __('column name mismatch.') );
$this->redirect( array('action'=>'import') );
}
$new_records_count = $this->Model->find( 'count' ) - $records_count;
$this->Session->setFlash(__('Successfully imported') . ' ' . $new_records_count . ' records from ' . $this->request->data['Model']['CsvFile']['name'] );
$this->redirect(array('plugin'=>'usermgmt', 'controller'=>'users', 'action'=>'dashboard'));
}
}
Any help would be greatly appreciated as I have only found 1 post concerning this behavior when I searching...
I made my custom method to achieve the same task. Define the following method in app\Plugin\Utils\Model\Behavior
public function getCSVData(Model &$Model, $file, $fixed = array())
{
$settings = array(
'delimiter' => ',',
'enclosure' => '"',
'hasHeader' => true
);
$this->setup($Model, $settings);
$handle = new SplFileObject($file, 'rb');
$header = $this->_getHeader($Model, $handle);
$db = $Model->getDataSource();
$db->begin($Model);
$saved = array();
$data = array();
$i = 0;
while (($row = $this->_getCSVLine($Model, $handle)) !== false)
{
foreach ($header as $k => $col)
{
// get the data field from Model.field
$col = str_replace('.', '-', trim($col));
if (strpos($col, '.') !== false)
{
list($model,$field) = explode('.', $col);
$data[$i][$model][$field] = (isset($row[$k])) ? $row[$k] : '';
}
else
{
$col = str_replace(' ','_', $col);
$data[$i][$Model->alias][$col] = (isset($row[$k])) ? $row[$k] : '';
}
}
$is_valid_row = false;
foreach($data[$i][$Model->alias] as $col => $value )
{
if(!empty($data[$i][$Model->alias][$col]))
{
$is_valid_row = true;
}
}
if($is_valid_row == true)
{
$i++;
$data = Set::merge($data, $fixed);
}
else
{
unset($data[$i]);
}
}
return $data;
}
And you can use it using:
$csv_data = $this->Model->getCSVData($this->request->data['Model']['CsvFile']['tmp_name'], $fixed);
Here $csv_data will contain an array of all of those records from the csv file which are not empty and with the fixed field in each record index.
So as I was telling Arun, I answered my own question and figured it out. I was looking to broad instead of really examining what was in front of me. I started running some debugging and figured it out.
First of all, $unique_add = 69 is seen as an int, duh. In order for it to be added to the csv it need to viewed as a string. So it simply becomes, $unique_add = '69'.
I couldn't enter the value of $Id directly into the fixed array. So I just had to perform a simple find to get the value I needed.
$needed_id = $this->Model->find('first', array(
'condition'=>array('Model.id'=>$Id)
)
);
$random_id = $needed_id['Model']['id'];
Hopefully this won't be needed to help anyone because hopefully no one else will make this silly mistake. But one plus... Now there's actually more than one post on the internet documenting the use of fixed fields in the CakeDC Utils plugin.

codeigniter multiple table joins

function in codeigniter model is not working. I'm getting http error 500 because of below code in codeigniter model function. if I remove this code.. code is working fine.
function get_ad_by_user($a,$b)
{
$this->db->select('title,ci_categories.category,ci_subcategories.subcategory,state,city,locality,ad_website,description,phone_number,address,image_token1,image_token2');
$this->db->from('ci_pre_classifieds');
$this->db->join('ci_categories', 'ci_categories.category_id = ci_pre_classifieds.category');
$this->db->join('ci_subcategories', 'ci_subcategories.subcategory_id = ci_pre_classifieds.sub_category');
$this->db->where('ci_pre_classifieds.id',$a);
$this->db->where('ci_pre_classifieds.ad_string_token',$b);
$q=$this->db->get();
$this->db->last_query();
log_message('debug','******query'.$this->db->last_query().'********');
if($q->num_rows()==1)
{
foreach($q->result_array() as $row)
{
$data['title]=$row['title'];
$data['category']=$row['ci_categories.category'];
$data['sub_category']=$row['ci_subcategories.subcategory'];
$data['state']=$row['state'];
$data['locality']=$row['locality'];
$data['ad_website']=$row['ad_website'];
$data['description']=$row['description'];
$data['phone_number']=$row['phone_number'];
$data['address']=$row['address'];
}
}
else
{
$data['message']="sorry either classified expired or deleted";
}
return $data;
}
I'm getting error in for each block..if I remove everything working...How to copy $row array in $data array
$data['title]=$row['title']; has a ' missing.
This could be the reason but without that error message, it's hard to tell.
Also, you may want to reduce the function to the following:
function get_ad_by_user($a,$b)
{
$data = array();
$this->db->select('title,ci_categories.category,ci_subcategories.subcategory,state,city,locality,ad_website,description,phone_number,address,image_token1,image_token2');
$this->db->from('ci_pre_classifieds');
$this->db->join('ci_categories', 'ci_categories.category_id = ci_pre_classifieds.category');
$this->db->join('ci_subcategories', 'ci_subcategories.subcategory_id = ci_pre_classifieds.sub_category');
$this->db->where('ci_pre_classifieds.id',$a);
$this->db->where('ci_pre_classifieds.ad_string_token',$b);
$q=$this->db->get();
log_message('debug','******query'.$this->db->last_query().'********');
if($q->num_rows()==1)
{
$data[] = $q->row_array();
}
else
{
$data['message']="sorry either classified expired or deleted";
}
return $data;
}
If you are only returning one row in your query, you can simply use $q->row_array() or $q->row() to return the single row.
Just in case, I would also declare $data = array() at the start of your get_ad_by_user function, otherwise it "could" complain that $data does not exist.

Resources