I am trying to json encode an array,it does encode but i get lots of errors:
$products = array( array( Title => "rose",
Price => "1.25,1.31,1.54,1.39",
Type => "dropdown"
),
array( Title => "daisy",
Price => "0.75",
Type => "text_field",
),
array( Title => "orchid",
Price => "1.15",
Type => "text_field"
)
);
echo json_encode($products);
I get the following errors.
Notice: Use of undefined constant Title - assumed 'Title' in C:\wamp\www\serializer.php on line 2
Notice: Use of undefined constant Price - assumed 'Price' in C:\wamp\www\serializer.php on line 3
Notice: Use of undefined constant Type - assumed 'Type' in C:\wamp\www\serializer.php on line 4
Notice: Use of undefined constant Title - assumed 'Title' in C:\wamp\www\serializer.php on line 6
Notice: Use of undefined constant Price - assumed 'Price' in C:\wamp\www\serializer.php on line 7
Notice: Use of undefined constant Type - assumed 'Type' in C:\wamp\www\serializer.php on line 8
Notice: Use of undefined constant Title - assumed 'Title' in C:\wamp\www\serializer.php on line 10
Notice: Use of undefined constant Price - assumed 'Price' in C:\wamp\www\serializer.php on line 11
Notice: Use of undefined constant Type - assumed 'Type' in C:\wamp\www\serializer.php on line 12
You need to quote the keys. Without quotes, they're constants. The interpreter is guessing what you mean, but you should change it to avoid the notice.
$products = array( array( "Title" => "rose",
"Price" => "1.25,1.31,1.54,1.39",
"Type" => "dropdown"
),
you must use quotation mark for STRING keys in arrays. Your code with changes is shown below:
<?php $products = array( array( 'Title' => "rose",
'Price' => "1.25,1.31,1.54,1.39",
'Type' => "dropdown"
),
array( 'Title' => "daisy",
'Price' => "0.75",
'Type' => "text_field",
),
array( 'Title' => "orchid",
'Price' => "1.15",
'Type' => "text_field"
)
); echo json_encode($products);
Additional information about arrays in php you will find here PHP: Arrays
put quotes around the array key names
$products = array( array( 'Title' => "rose",
'Price' => "1.25,1.31,1.54,1.39",
'Type' => "dropdown"
),
array( 'Title' => "daisy",
'Price' => "0.75",
'Type' => "text_field",
),
array( 'Title' => "orchid",
'Price' => "1.15",
'Type' => "text_field"
)
);
echo json_encode($products);
array( array( 'Title' => "rose",
'Price' => "1.25,1.31,1.54,1.39",
'Type' => "dropdown"
),
array( 'Title' => "daisy",
'Price' => "0.75",
'Type' => "text_field",
),
array( 'Title' => "orchid",
'Price' => "1.15",
'Type' => "text_field"
)
);
You might be confusing javascripts object notation syntax with PHP here, like the other answers have suggested, wrapping the array keys in quotes (so that they're passed in as strings) will sort out your problem.
It might be worth reading up on PHP Constants to better understand the error message you've been given: http://php.net/manual/en/language.constants.php
I came across the exact same problem and after staring at the PHP5 manual page for arrays it eventually clicked. Here is what I found out:
This line
If ($showallresult[Composer] == "") $showallresult[Composer] = "?";
will cause that notice to show.
I also use this line in my code
print ("<TD ALIGN=CENTER VALIGN=TOP>$showallresult[Composer]</TD>\n");
When I wrap the array key in single quotes in each line as such
If ($showallresult['Composer'] == "") $showallresult['Composer'] = "?";
print ("<TD ALIGN=CENTER VALIGN=TOP>$showallresult['Composer']</TD>\n");
I get a parse error on the second line, but the first line appears to be fine. Looking at http://us2.php.net/manual/en/function.array.php and there Example #4 the answer is right there. When accessing array values within a double quoted string you have to enclose the array value construct in curly braces (mustaches). As it turns out, this is how it is correct:
If ($showallresult['Composer'] == "") $showallresult['Composer'] = "?";
print ("<TD ALIGN=CENTER VALIGN=TOP>{$showallresult['Composer']}</TD>\n");
Oddly, this works as well without throwing parse errors or notices...welcome to the logic of PHP:
If ($showallresult['Composer'] == "") $showallresult['Composer'] = "?";
print ("<TD ALIGN=CENTER VALIGN=TOP>$showallresult[Composer]</TD>\n");
Rather bizarre that both lines work fine and generate the expected result. While the no-single-quote-no-curly-braces notation works I do suggest to go with how it seems to be correct and use curly braces and single quotes within a string.
In any case, reading the docs and mulling over it some time fixed it for me. And yes, sadly, it's all there right in the manual!
Related
I've got the following problem, I'm assigning values to an array, and one of the values I'm trying to assign is dependent on a value from the same array.
Is there a way of doing it somehow LIKE this:
# this code throws a notice
$myArray = [
'keyPairs1' => [
'color' => 'green',
'...' => '...'
],
'keyPairs2' => [
'value' => $myArray['keyPairs1']['color'] === 'green' ? 'yes' : 'no', # this line
'...' => '...'
]
];
Here PHP throws me a Notice:
Notice: Undefined variable: myArray in .../somefile.php on line ...
Of course I could simply define the array first and assign something like an empty string to the value and after creating the array simply do
$myArray = [
'keyPairs1' => [
'color' => 'green',
'...' => '...'
],
'keyPairs2' => [
'value' => '', # this line
'...' => '...'
]
];
$myArray['keyPairs2']['value'] = $myArray['keyPairs1']['color'] === 'green' ? 'yes' : 'no';
but I'd rather keep it all in the array if there is any possible way to do it. The above attempt is obviously not leading to the desired result.
$aa = Input::get('AccountOpeningDate' . $i);
$dateinfo = explode("-", $aa);
$testDay = Carbon::createFromDate($dateinfo[0], $dateinfo[1],
$dateinfo[2], 'UTC');
$actualDate = $testDay->setTimezone('+6:00');
when I run this code then I get an output.But it cause an error that like the image below.
ErrorException in MemberController.php line 532:
Undefined offset: 1
in MemberController.php line 532
at HandleExceptions->handleError('8', 'Undefined offset: 1', 'C:\xampp\htdocs\timf\app\Http\Controllers\MemberController.php', '532', array('id' => '4001-5088-0565', 'memberdata' => object(Member), 'somityDay' => object(Zone1), 'i' => '2', 'aa' => '', 'dateinfo' => array(''), 'testDay' => object(Carbon), 'actualDate' => object(Carbon), 'producttype' => '2', 'memberaccount' => object(Accountstable), 'valsa' => object(Product), 'AccNameSub' => 'MSSM', 'accnumber' => 'MSSM.4001-5088-0565', 'k' => '13', 'SavingSetup' =>
This code is written in laravel 5.1.
$aa = Input::get('AccountOpeningDate' . $i);
Here $aa has no data in case of any conditions. So the array $dateinfo remaining empty. I have fixed the problem by ensuring $aa data not empty.
now the code is running well.
There may be a comma missing in your first line of code.
After quite some trying out and web research I go crazy with this query. I want to build a query for 'Clubs' around a geo point (distance max 500 meters) in php on MongoDB.
But when I run query it ignores the distance limit and shows all clubs in database BUT sorted by distance.
Here is my dataset (2dsphere index geoLoc):
{"_id":ObjectId("547c649e30afe32c23000048"),"name":"Club Ritzz","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"}],"location":{"city":"Mannheim"},"geoLoc":{"type":"Point","coordinates":[8.473665839156,49.484065272756]}}
{"_id":ObjectId("547c649f30afe32c2300004a"),"name":"Das Zimmer Mannheim","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"}],"geoLoc":{"type":"Point","coordinates":[8.4709362941178,49.487260552592]}}
{"_id":ObjectId("547c64ab30afe32c23000063"),"name":"Nationaltheater Mannheim","category":"Arts/entertainment/nightlife","category_list":[{"id":"173883042668223","name":"Theatre"}],"geoLoc":{"type":"Point","coordinates":[8.4776534992592,49.48782606969]}}
{"_id":ObjectId("547c64a130afe32c2300004f"),"name":"SOHO Bar Club Lounge","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"},{"id":"164049010316507","name":"Gastropub"}],"geoLoc":{"type":"Point","coordinates":[8.4630844501277,49.49385193591]}}
{"_id":ObjectId("547c64a730afe32c2300005a"),"name":"Loft Club","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"},{"id":"176139629103647","name":"Dance Club"}],"geoLoc":{"type":"Point","coordinates":[8.4296300196465,49.484211928258]}}
And here my php code (updated Dec-2):
$qry = $pub->find(
array( '$and' =>
array(
array( 'geoLoc' =>
array('$nearSphere' =>
array('$geometry' =>
array('type'=>'Point',
'coordinates'=>
array(
floatval($sLon), floatval($sLat)
)
),
'maxDistance' => 500
)
)
),
array( '$or' =>
array(
array( 'name' => new MongoRegex("/.*club/i")),
array( 'name' => new MongoRegex("/.*zimm/i"))
)
),
array('$or' =>
array(
array('category_list.name' => 'Night Club'),
array('category_list.name' => 'Dance Club'),
array('category' => 'Club')
)
)
)
),
array('id' => 1, 'name' => 1, '_id' => 0)
);
Anyone know why the results are not limited to the specified maxDistance?
I found a similar issue on StackOverflow which outlines that one has to use radians for the maxDistance parameter.
See https://dba.stackexchange.com/questions/23869/nearsphere-returns-too-many-data-what-am-i-missing-am-i-wrong-is-it-a-bug-d
Also it is probably helpful if you'd test the query in mongo shell without using the PHP APIs first (just to see if the query is generally working and append '.explain()' to it to see what generally happens inside DB).
I am trying to display double variable inside an Smarty environment converting it into int.
The File configuring the variables has the following code.
The Variable PRODUCTS_Quantity_x is the viewed Variable which I added. It is giving me a comma separated value with 3 fractional digits. The {$module_data.PRODUCTS_Quantity} is already be able to get displayed in through the html.
I have already tryed things like count() etc. I mostly got an empty variable or smarty errors ...
$t_data_array = array ( 'PRODUCTS_NAME' => htmlspecialchars_wrapper($array['products_name']),
'COUNT' => $array['ID'],
'PRODUCTS_ID' => $array['products_id'],
'PRODUCTS_VPE' => $this->getVPEtext($array, $products_price['plain']),
'PRODUCTS_IMAGE' => $this->productImage($array['products_image'], $image),
'PRODUCTS_IMAGE_W' => $array['products_image_w'],
'PRODUCTS_IMAGE_H' => $array['products_image_h'],
'PRODUCTS_IMAGE_WIDTH' => PRODUCT_IMAGE_THUMBNAIL_WIDTH,
'PRODUCTS_IMAGE_PADDING' => ((PRODUCT_IMAGE_THUMBNAIL_HEIGHT + 8) - $array['products_image_h'])/2,
'PRODUCTS_IMAGE_ALT' => $array['gm_alt_text'],
'PRODUCTS_LINK' => $gm_product_link,
'PRODUCTS_PRICE' => $products_price['formated'],
'PRODUCTS_TAX_INFO' => $gm_tax_info,
'PRODUCTS_SHIPPING_LINK' => $gm_shipping_link,
'PRODUCTS_BUTTON_BUY_NOW' => $buy_now,
'GM_PRODUCTS_BUTTON_BUY_NOW_URL' => $gm_buy_now_url,
'GM_PRODUCTS_BUTTON_BUY_NOW' => $gm_buy_now,
'PRODUCTS_SHIPPING_NAME' => $shipping_status_name,
'PRODUCTS_SHIPPING_IMAGE' => $shipping_status_image,
'PRODUCTS_DESCRIPTION' => $array['products_description'],
'PRODUCTS_EXPIRES' => $array['expires_date'],
'PRODUCTS_CATEGORY_URL' => $array['cat_url'],
'PRODUCTS_SHORT_DESCRIPTION' => $array['products_short_description'],
'PRODUCTS_FSK18' => $array['products_fsk18'],
'GM_FORM_ACTION' => xtc_draw_form('gm_add_to_cart_'.$array['products_id'], xtc_href_link('index.php', 'action=buy_now&BUYproducts_id=' . $array['products_id'] . $gm_cat_search, 'NONSSL', true, true, true), 'post', 'onsubmit="return gm_quantity_check_listing(\'' . $array['products_id'] . '\')"'),
'FORM_DATA' => $t_form_array,
'QTY_DATA' => $t_qty_array,
'PRODUCTS_Quantity_x' => $array['products_quantity'],
'GM_FORM_END' => '</form>',
'GM_PRODUCTS_QTY' => $gm_qty,
'GM_PRODUCTS_STOCK' => $gm_products_stock,
'PRODUCTS_META_DESCRIPTION' => $array['products_meta_description'],
'PRODUCTS_WEIGHT' => gm_prepare_number((double)$array['products_weight'], $xtPrice->currencies[$xtPrice->actualCurr]['decimal_point']),
'SHOW_PRODUCTS_WEIGHT' => $array['gm_show_weight']);
return $t_data_array;
// EOF GM_MOD
Inside the html the following code is given
<span class="label">{$module_data.PRODUCTS_Quantity_x}<span>
I hope you can help me out here
thanks in advance
You can do this 2 way:
1) First way:
You simple use in Smarty the following code with added string_format:
<span class="label">{$module_data.PRODUCTS_Quantity_x|string_format:"%d"}<span>
2) Second way:
You do whatever you need in PHP and assign desired value in PHP.
So in PHP you can change line:
'PRODUCTS_Quantity_x'=> $array['products_quantity'],
into
'PRODUCTS_Quantity_x'=> (int) $array['products_quantity'],
and then in Smarty you use the same code as you used earlier (without string_format):
<span class="label">{$module_data.PRODUCTS_Quantity_x}<span>
I want to match course_id with $courseInfo which is fetching from $cLID.
When I change this $clID['Relationscl']['course_id'] line as 1 everything is ok, but the other way an error appears like Undefined index: Relationscl.
Here is my code:
$this->loadModel('Course');
$this->loadModel('Lecturer');
$clID = $this->Relationscl->find('all', array(
'conditions' => array(
'student_id' => $id
),
'fields' => array(
'course_id','lecturer_id'
)
));
$this->set('clIDs', $clID);
$courseInfo = $this->Course->find('first',array(
'conditions' => array(
'course_id' => $clID['Relationscl']['course_id']
),
'fields' => array(
'course_name','course_code','course_credit'
)
));
$this->set('cInfos', $courseInfo);
Correct me if I'm wrong, but the find('all') function returns and indexed array, so it should be something like
$clID = array( 0 => array('Relationscl'=>array('course_id'=>1 /*and more fields*/)),
1 => array('Relationscl'=>array('course_id'=>2 /*and more fields*/))
/*etc*/);
So clearly $clID['Relationscl'] is undefined. Try with $clID[0]['Relationscl']. Though even that seems weird, why would you do a find('all') if you only plan on using the one record, isn't find('first') better? Or set that $courseInfo definition inside a loop?