cakephp illegal offset string in php 5.4 - string

MyHtmlHelper
public function url($url = null, $full = false) {
if(empty($url['lang']) && isset($this->params['lang'])) {
$url['lang'] = $this->params['lang'];
}
return parent::url($url, $full);
}
AppHelper
public function url($url = null, $full = false) {
if (empty($url['lang'])) {
$url['lang'] = $this->params['lang'];
}
return parent::url($url, $full);
}
when I ran . application warnings cakephp illegal offset lang .....
how do I fix that errors

Check the values you test are actually set first.
Change this:
if(empty($url['lang']) && isset($this->params['lang'])) {
$url['lang'] = $this->params['lang'];
}
To:
if ( ( ! isset( $url['lang'] ) || empty( $url['lang'] ) ) && isset( $this->params['lang'] ) ) {
$url['lang'] = $this->params['lang'];
}
I'm not sure whether I have this the right way round for what you're trying to achieve by the way. In my example I'm checking if the value of $url['lang'] is either not set or empty. It may be that you want to check that it is set and empty.
Use the same technique on the AppHelper as well to resolve your issue.

You need to add is_array($url) to your validation.
Or you might get “Illegal string offset ‘language'”
function url($url = null, $full = false) {
if(!isset($url['language']) && is_array($url) && isset($this->params['language']))
{
$url['language'] = $this->params['language'];
}
return parent::url($url, $full);
}

Related

Null and empty check in one go in groovy

Can someone please clarify below issue.
Below validation throw NULL pointer error when pass null in myVar. It is because of !myVar.isEmpty()
if (myVar!= null || !myVar.isEmpty() ) {
some code///
}
Below works though as expected,
if (myVar!= null) {
if (!myVar.isEmpty()) {
some code///
}
Any other way of having both steps together.
If .isEmpty() is used on a string, then you can also just use Groovy
"truth" directly, as null and also empty strings are "false".
[null, "", "ok"].each{
if (it) {
println it
}
}
// -> ok
if ( myVar!= null && !myVar.isEmpty() ) {
//some code
}
the same as
if ( !( myVar== null || myVar.isEmpty() ) ) {
//some code
}
and to make it shorter - it's better to add method like hasValues
then check could be like this:
if( myVar?.hasValues() ){
//code
}
and finally to make it groovier - create a method boolean asBoolean()
class MyClass{
String s=""
boolean isEmpty(){
return s==null || s.length()==0
}
boolean asBoolean(){
return !isEmpty()
}
}
def myVar = new MyClass(s:"abc")
//in this case your check could be veeery short
//the following means myVar!=null && myVar.asBoolean()==true
if(myVar) {
//code
}

Create a search bar with multiple columns in codeigniter

Controller:
public function search_products_auto(){
$this->load->helper(array('form', 'url','common'));
$this->load->library(array('session','pagination'));
$this->load->model('homemodel','',TRUE);
$this->homemodel->search_products_auto();
}
public function search($subcategory=''){
$this->load->helper(array('form', 'url','common'));
$this->load->library(array('session','pagination'));
$this->load->model('homemodel','',TRUE);
$search_name=$this->input->post("title"); // first get search character
$data['records']=$this->homemodel->search($search_name,$_GET); // SearchModel is the model class name
$data['category']=$this->homemodel->getAllCategories();
$data['subcategory']=$subcategory;
$data['mens']=$this->homemodel->getAllMen();
$data['womens']=$this->homemodel->getAllWomen();
$data['scarves']=$this->homemodel->getAllScarve();
$data['collections']=$this->homemodel->getAllCollection();
$data['material']=['Cotton'=>'Cotton','Polyster'=>'Polyster'];
$data['color']=['Red'=>'Red','Black'=>'Black','Blue'=>'Blue','Green'=>'Green','Orange'=>'Orange','Black'=>'Black'];
$data['size'] =['S'=>'S','M'=>'M','L'=>'L','XL'=>'XL','XXL'=>'XXL','XXL'=>'XXL'];
$data['subcat']=$this->homemodel->getAllSubCat($search_name,$_GET);
$data['price']=$this->homemodel->getPrice($search_name,$_GET);
$this->load->view('products',$data);
}
Model:
function search($search_name,$search=array(),$cat_array=array()){
$search=array_filter($search);
$this->db->select('product.*');
$this->db->select('product_images.features');
$this->db->select('product_images.image');
$this->db->from('product');
$this->db->join('product_images',"product.id=product_images.product_id and product_images.features='yes'");
$this->db->join('product_material',"product.id=product_material.product_id",'left');
$this->db->join('product_color',"product.id=product_color.product_id",'left');
$this->db->join('product_size',"product.id=product_size.product_id",'left');
if(isset($cat_array->sub_cat_id) && $cat_array->sub_cat_id!=''){
$this->db->where('product.product_subcategory',$cat_array->sub_cat_id);
}
if(isset($search['material']) && $search['material']!=''){
$this->db->where_in('product_material.material',explode(",",$search['material']));
}
if(isset($search['color']) && $search['color']!=''){
$this->db->where_in('product_color.color',explode(",",$search['color']));
}
if(isset($search['size']) && $search['size']!=''){
$this->db->where_in('product_size.size',explode(",",$search['size']));
}
if(isset($search['price']) && $search['price']!=''){
$this->db->where_in('product.product_price',explode(",",$search['price']));
}
if(isset($search['search']) && $search['search']!=''){
$this->db->or_like(array('product.product_name'=>$search['search'],'product.sku_number'=>$search['search']));
}
$this->db->or_like(array('product.product_name'=>$search_name,'product.sku_number'=>$search_name));
$this->db->where("product.status='active'");
$this->db->group_by(['product_color.product_id','product_material.product_id','product_size.product_id']);
$query=$this->db->get();
$result=$query->result();
return $result;
}
function search_products_auto(){
$search_item = $this->input->post('query');
$this->db->select(['product.id','product.product_name','product.sku_number']);
$this->db->from('product');
if($search_item!=''){
$this->db->or_like(array('product.product_name'=>$search_item,'product.sku_number'=>$search_item));
}
$this->db->where("product.status='active'");
$query=$this->db->get();
$result=$query->result();
$dataSearch=[];
if(count($result)>0){
$i=0;
foreach($result as $results)
{
$dataSearch[$i]['id']=isset($results->id)?$results->id:"";
$dataSearch[$i]['name']=isset($results->product_name)?$results->product_name:"";
$dataSearch[$i]['name']=isset($results->sku_number)?$results->sku_number:"";
$i++;
}
}
echo json_encode($dataSearch);
}
Now, I am getting only sku number values in search bar...not the other value. How can I get all three in one search bar. I am unable to understand the concept of integrating three into one array.Rest of the things are working properly, except this search bar and also the search results are also not fine.
Please try these code it may help you, I have change only in Model:
function search($search_name, $search=array(), $cat_array=array()){
$search = array_filter($search);
$this->db->select('product.*');
$this->db->select('product_images.features');
$this->db->select('product_images.image');
$this->db->from('product');
$this->db->join('product_images',"product.id=product_images.product_id and product_images.features='yes'");
if(isset($cat_array->sub_cat_id) && $cat_array->sub_cat_id!=''){
$this->db->where('product.product_subcategory',$cat_array->sub_cat_id);
}
if(isset($search['price']) && $search['price']!=''){
$this->db->where_in('product.product_price',explode(",", $search['price']));
}
if(isset($search['search']) && $search['search']!=''){
$this->db->or_like(array('product.product_name' => $search['search'],'product.sku_number' => $search['search']));
}
if(isset($search['material']) && $search['material']!=''){
$this->db->join('product_material',"product.id=product_material.product_id",'left');
$this->db->where_in('product_material.material',explode(",",$search['material']));
}
if(isset($search['color']) && $search['color']!=''){
$this->db->join('product_color',"product.id=product_color.product_id",'left');
$this->db->where_in('product_color.color',explode(",",$search['color']));
}
if(isset($search['size']) && $search['size']!=''){
$this->db->join('product_size',"product.id=product_size.product_id",'left');
$this->db->where_in('product_size.size',explode(",",$search['size']));
}
$this->db->or_like( array('product.product_name' => $search_name, 'product.sku_number' => $search_name));
$this->db->where("product.status='active'");
$this->db->group_by(['product_color.product_id','product_material.product_id','product_size.product_id']);
$query = $this->db->get();
$result_array = $query->result_array();
/* $result=$query->result(); */
return $result_array;
}
function search_products_auto(){
$search_item = $this->input->post('query');
$this->db->select(['product.id','product.product_name','product.sku_number']);
$this->db->from('product');
if($search_item != ''){
$this->db->or_like(array('product.product_name' => $search_item, 'product.sku_number' => $search_item));
}
$this->db->where("product.status","active");
$query = $this->db->get();
/* $result=$query->result();
$dataSearch=[];
if(count($result)>0){
$i=0;
foreach($result as $results)
{
$dataSearch[$i]['id']=isset($results->id)?$results->id:"";
$dataSearch[$i]['name']=isset($results->product_name)?$results->product_name:"";
$dataSearch[$i]['sku_number']=isset($results->sku_number)?$results->sku_number:"";
$i++;
}
}
echo json_encode($dataSearch); */
echo json_encode($query->result_array());
}

How to solve type error when I add new fields to object in nodejs

I created an object. But when I try to set some values in it an error is thrown.
See code below:
function CheckStatus(oldJob, newJob) {
var obj = {};
if(newJob && newJob.Status) {
if (oldJob.Status.total !== newJob.Status.total) {
obj.Status.total = newJob.Status.total;
}
if (oldJob.Status.charge_description && oldJob.Status.charge_description !== newJob.Status.charge_description) {
obj.Status.charge_description = newJob.Status.charge_description;
}
}
}
TypeError: Cannot set property 'total' of undefined
Well your definition of obj is an empty object. So before setting obj.Status.total you need to define obj.Status, either in the original declaration like so:
var obj = {
Status: {}
}
or later like so:
obj.Status = {}
Your obj variable has nos property status ! you must create his real structure before !
use lodash _.set :
const _ = require('lodash');
function CheckStatus(oldJob, newJob) {
let obj = {};
if (newJob && newJob.Status) {
if (oldJob.Status.total !== newJob.Status.total) {
_.set(obj, 'Status.total', newJob.Status.total);
}
if (oldJob.Status.charge_description && (oldJob.Status.charge_description !== newJob.Status.charge_description)) {
_.set(obj, 'Status.charge_description', newJob.Status.charge_description);
}
}
}
FYI it return a mutated object not a new object ! ;)

Pagination link not work properly?

Pagination not work properly?
I saved this code in "Forum-Com.php" file and when i open this file, pagination works properly and correctly but when I include this file in another page, its open first page correctly but when I click second or another page link it open same comments which are on first page. Please help me. ( I am using Scriptsmill comments script v1.06 and function make_pages_string from admin.php )
$COM_CONF['full_path'] = dirname(__FILE__);
function make_pages_string ($all_count, $records_per_page, $cur_page, $base_url) {
if ($all_count > $records_per_page) {
if ($cur_page > 0) { $cur_page=$cur_page-1; }
$first_record = ($cur_page) * $records_per_page;
$limit_string = "LIMIT $first_record, $records_per_page";
$pages=$all_count/$records_per_page;
if ($pages > (int) $pages) { $pages=(int)$pages+1; }
}
if ($pages>1) {
$pages_string.="Page: ";
if ($cur_page>10 && $pages>20) { $first_page=$cur_page-9; }
else { $first_page=1; }
if ($pages>20 && ($cur_page+10)<$pages) { $last_page=$first_page+19; }
else { $last_page=$pages; }
if ($cur_page+1>1) {
$prev=$cur_page;
$pages_string.="<a href='$base_url&page=$prev'>&lt</a> ";
}
for ($i=$first_page; $i<=$last_page; $i++){
if ($i != $cur_page+1) {
$pages_string.="<a href='$base_url&page=$i'>$i</a> ";
}
else {
$pages_string.="<b>$i</b> ";
}
}
if ($cur_page+1<$pages) {
$next=$cur_page+2;
$pages_string.="<a href='$base_url&page=$next'>&gt</a> ";
}
}
return array ($pages_string, $limit_string);
}
function smcom_view()
{
global $comments_db_link, $COM_CONF, $COM_LANG;
$result = mysql_query("select COUNT(id) from {$COM_CONF['dbmaintable']}", $comments_db_link);
list ($all_count) = mysql_fetch_row($result);
list ($pages_string, $limit_string) = make_pages_string ($all_count, 10, $_REQUEST['page'], "{$COM_CONF['base_url']}?action=view");
$result = mysql_query("select time, text, author, email, dont_show_email from {$COM_CONF['dbmaintable']} order by time {$COM_CONF['sort_order']} $limit_string", $comments_db_link);
$comments_count=0;
$id=$time=$text=$author=$email=$dont_show_email=$ip=array();
while (list($id[$comments_count], $time[$comments_count], $text[$comments_count], $author[$comments_count], $email[$comments_count], $dont_show_email[$comments_count], $ip[$comments_count])=mysql_fetch_array($result)) {
$comments_count++;
}
require("{$COM_CONF['full_path']}/templates/Forum-default.php");
}
The code given above has no problem at all. The problem is with the server configuration which turned off $_REQUEST global variable from direct access.

Change currency position according to currency in wordpress

Googled and searched but I couldn't find what I was looking for.
I am building a woocommerce website which has 2 currencies: KRW and USD.
I have two buttons which switch between these currencies.
When KRW is selected, price display 10,000W and when USD is selected, it displays 100$.
What I want is to display $100 when USD is selected.
I tried in my functions.php:
add_filter('woocommerce_price_format','change_currency_pos');
function change_currency_pos( $currency_pos, $currency ) {
switch( $currency ) {
case 'USD': $currency_pos = '%1$s%2$s'; break;
case 'KRW': $currency_pos = '%2$s%1$s'; break;
}
return $currency_pos;
}
also tried:
function change_currency_pos() {
$currency_pos = get_option('woocommerece_currency');
switch($currency_pos) {
case 'USD': $format = '%1$s%2$s'; break;
case 'KRW': $format = '%2$s%1$s'; break;
}
return $currency_pos;
}
add_filter('woocommerce_price_format','change_currency_pos');
Both didn't work. :(
Can somebody help please. Thank you.
This is just a guess since I don't know what plugin you are using or how it works. I am going to assume that it adds a $_GET variable named currency to the end of your URL. www.example.com&currency=KRW But idea is that you set a value for the woocommerce_currency_pos option based on some data provided by the currency plugin.
add_filter( 'pre_option_woocommerce_currency_pos', 'change_currency_position' );
function change_currency_position(){
if( ! isset( $_GET['currency'] ) {
return false;
}
if ( 'USD' == $_GET['currency'] ){
return 'left';
} elseif ( 'KRW' == $_GET['currency'] ){
return 'right';
}
}
Alternatively, I could assume that "right" is the default currency position. And that you only need to filter the option in the instance where the site is displayed in USD-mode. In which case you would only need the following
add_filter( 'pre_option_woocommerce_currency_pos', 'change_currency_position' );
function change_currency_position(){
if( isset( $_GET['currency'] && 'USD' == $_GET['currency'] ){
return 'left';
}
}
For woocommerce_price_format to work you need to set the format like this
add_filter('woocommerce_price_format', 'woo_custom_format_position', 999, 2);
function woo_custom_format_position($format, $currency_pos)
{
/*'left':$format = '%1$s%2$s';
'right':$format = '%2$s%1$s';
'left_space':$format = '%1$s %2$s';
'right_space':$format = '%2$s %1$s';
*/
$format = '%1$s%2$s';//Change your position
return $format;
}
For some strange reason the 'position' setting wasn't working for EUR currency. I had to add this to my functions.php in order to fix it:
add_filter('woocommerce_price_format', 'woo_custom_format_position', 999, 2);
function woo_custom_format_position($format, $currency_pos)
{
/*'left':$format = '%1$s%2$s';
'right':$format = '%2$s%1$s';
'left_space':$format = '%1$s %2$s';
'right_space':$format = '%2$s %1$s';
*/
switch ($currency_pos) {
case 'left':
return '%1$s%2$s';
case 'right':
return '%2$s%1$s';
case 'left_space':
return '%1$s %2$s';
case 'right_space':
return '%2$s %1$s';
};
}
Now it actually uses the position chosen in the settings.
Hope it helps somebody!

Resources