Smarty Object Syntax - php

I registered an object an I'm trying to get the following below:
stdClass Object (
[test] => test
[users] => stdClass Object (
[createSave_email_subject] => - New
User Account
[createSave_email_pass] => The user
was created, and an email was sent to
them!
) )
Smarty Code:
Works:
{language->test}
Doesn't Work:
{language->users->createSave_email_subject}
{language[users]->createSave_email_subject}
{language.users->createSave_email_subject}
{language->users.createSave_email_subject}
{language->users[createSave_email_subject]}

I built a test case for this using this code:
$test = json_decode('{"test":"test","users":{"createSave_email_subject":"new user account","createSave_email_pass":"The user was created, and an email was sent to them!"}}');
$smarty->assign('testing',$test);
$test is this when var_dumped
object(stdClass)#8 (2) {
["test"]=>
string(4) "test"
["users"]=>
object(stdClass)#7 (2) {
["createSave_email_subject"]=>
string(16) "new user account"
["createSave_email_pass"]=>
string(52) "The user was created, and an email was sent to them!"
}
}
In the tpl I placed
{$testing->users->createSave_email_subject}
And it worked without issue. This was using Smarty 2.6.23

You're missing the dollar signs, you should be using
{$language->test}
Not sure if that's the problem though, as I didn't think smarty would output anything with the syntax you gave. Your test case isn't very reliable either, it's safer to use something like
array('test' => 'worked');
where the key and value are different. With your test, smarty could be printing the key and you wouldn't know the difference.
Use debug_print_var to help identify your problem. e.g.
$language: {$language|#debug_print_var}
users: {$language->users|#debug_print_var}
cse_subject: {$language->users->createSave_email_subject|#debug_print_var}

Related

How Do I retrieve a string value JSON in PHP?

I have a returned string result from an API and it looks like this.. for the life of me, I cannot retried the WorkID value!
The returned string is a json string:
{"notes":"","RecordsStatus":"{\"0\":{\"WorkID\":\"0090210\",\"Message\":\"Record Created\"}}"}
It has two parts:
“notes” and “RecordStatus”.
If message is empty, means the batch is imported without error.
In RecordStatus, there are two parts too.
First is the index number of the record, and it has second part that the key for the record created(in my case it’s the WorkID) and a message tells the record is created or updated(in my case, it’s created).
Array
(
[notes] =>
[RecordsStatus] => {"0":{"WorkID":"0090210","Message":"Record Created"}}
)
Do a Var_dump() of decoded_json results in this:
array(2) {
["notes"]=>
string(0) ""
["RecordsStatus"]=>
string(52) "{"0":{"WorkID":"0090210","Message":"Record Created"}}"
}
I tried
foreach($decoded_json as $item) {
$uses = $item['RecordsStatus'][0]['WorkID']; //etc
}
but does not work

Does PHP allow to have duplicate properties inside an stdObject?

I'm stuck with a very weird bug. I have an object called $row that looks like this:
stdClass Object
(
[title] => Some Title
[body] => My body
[topic] => Topic
[dataType] => Survey
[csvrownum] => 1
)
I'm just trying to print out the title property in the following way:
print_r($row->title);
However for some reason that doesn't output anything.
Then I've tried to manually set the title property and print it right after, something like this:
$row->title = 'My Title';
print_r($row->title);
Surprisingly it worked but why? To make this more strange I decided to var_dump the object after set the title variable by hand:
$row->title = 'My Title';
var_dump($row);
And this is what I've got:
class stdClass#391 (6) {
public $title =>
string(3) "Some title"
public $body =>
string(7) "My body"
public $topic =>
string(6) "Topic"
public $dataType =>
string(17) "Survey"
public $csvrownum =>
int(1)
public $title =>
string(8) "My title"
}
Notice the title key is duplicated with different values. Is there any condition under this could happen?
No, PHP does not allow an object to have duplicate property names, because objects in PHP are implemented just like arrays. They are both implemented as ordered hashmaps. In a hashmap, two things that have the same hash, overwrite each other.
You likely just have unprintible characters in your object property name. You can see this more clearly by doing something like the following for debug purposes...
foreach($row as $key => $value) {
var_dump($key);
}
If we had an object like this, for example, you'd see it gets overwritten.
$row = new stdClass;
$row->title = "First";
$row->title = "Second";
But something like this might be more deceptive...
$row = new stdClass;
$row->{"title\0"} = "First";
$row->title = "Second";
Output from the foreach using var_dump on the key, would reveal this...
string(6) "title"
string(5) "title"
Notice one is string of length 6 and the other is a string of length 5.
Grain of salt
It's always better to use var_dump when attempting to debug variables than using something like print_r, as var_dump was specifically designed for debug purposes, whereas print_r is just a recursive print (hence the name). Printing values like null, false, or empty strings, gives you no useful information for debug purposes, but var_dump does.

Creating json array for Jira multi-checkbox with PHP

I'm receiving data in the following format from a multi-checkbox
["Ethnicity"]=>
array(3) {
["Maori"]=>
string(5) "Maori"
["Pacific Peoples"]=>
string(15) "Pacific Peoples"
["Other European"]=>
string(14) "Other European"
}
I'm trying to get this into a multi-checkbox in Jira via an API call using the following segment
'customfield_11337' => [
"value" => $data["Ethnicity"]
],
But this returns an error string(21) "data was not an array"
So I've tried to massage the data into a single array using
$ethnicityArray = array();
foreach ($data["Ethnicity"] as $eth => $value) {
array_push($ethnicityArray, $value);
}
But this returns the same error. I should note that I've got no problem populating radio buttons, text fields etc in Jira via the same method. It just seems to be checkboxes I can't get right.
How do I go about solving this using PHP?
The correct solution was to get the data into a format that looks like:
'customfield_11333' => [["value" => "Asian"], ["value" => "Other"]]

Laravel Raw Query Not Returning a Result

Hard-coded query
$stock = DB::select("SELECT * FROM reports_finra WHERE symbol = ? ", array('CWCO')); // works and returns valid result set
Array (
[0] => Array ( [query] => SELECT * FROM symbols [bindings] => Array ( ) [time] => 37.4 )
[1] => Array ( [query] => SELECT * FROM reports_finra WHERE symbol = ? [bindings] => Array ( [0] => CWCO ) [time] => 27.07 )
)
# result set when var_dump($stock) ( [1]'s query )
array(11) {
[0]=> object(stdClass)#5939 (7) { ["date"]=> int(20140116) ["symbol"]=> string(4) "CWCO" ["id"]=> int(1498) }
[1]=> object(stdClass)#5940 (7) { ["date"]=> int(20140117) ["symbol"]=> string(4) "CWCO" ["id"]=> int(8515) }
[2]=> object(stdClass)#5941 (7) { ["date"]=> int(20140121) ["symbol"]=> string(4) "CWCO" ["id"]=> int(15526) }
...
}
Dynamically binded variable being passed to query
$symbol = $symbol->symbol;
echo $symbol;
echo '<br />';
$stock = DB::select("SELECT * FROM reports_finra WHERE symbol = ? ", array($symbol));
$query = DB::getQueryLog();
print_r($query);
echo '<br />';
var_dump($stock); exit;
Results
CWCO // the result from echoing $symbol
Array ( // getQueryLog()
[0] => Array ( [query] => SELECT * FROM symbols [bindings] => Array ( ) [time] => 36.06 )
[1] => Array ( [query] => SELECT * FROM reports_finra WHERE symbol = ? [bindings] => Array ( [0] => CWCO ) [time] => 27.55 ) )
array(0) { } // var_dump($stock);
As you can see, when I pass the value of symbol into the query as a hard-coded value, I get the desired result set. However, when I pass the value in dynamically, the query returns an empty result set.
I have been going back and forth with this for quite a while now and am out of ideas on how to further troubleshoot this. Even more perplexing is the fact that these exact queries work in my dev environment but not when I pushed to prod.
Through troubleshooting, prior to realizing that when I pass in the raw, hard-coded value, that I was getting a valid result set, I ensured that there were only minor PHP / MySQL version differences between dev and prod ENVs. I triple checked that the user had the proper DB permissions. And, I tailed out my log files. None of these actually help shed any light onto what is actually going wrong here though.
As I just recently started to get back into development and have only been playing with L4 for a week or two, I'm hoping someone with more experience will see what it is that is causing this (I have an odd feeling that it is going to be an ID10T error..).
I look forward to any advice and insight you may have to offer.
-- Edit --
As the query is returning an (empty) result back (and not throwing an error), the query appears to be "working" as far as MySQL is concerned. However, it is not returning the result set for whatever reason. I am not sure if this has to do with PDO, Eloquent, or something else entirely. It's a basic SELECT * query on a non-related table; it shouldn't be this much hassle.
And, I'm quite perplexed why the other select statements work without an issue, but this one is not. Granted, I am querying a table that is not related to the model (there is no DB/L4 relationships defined; this is a once-off Select statement in order to do some essential math).
Unfortunately, I can't proceed until I get this worked out...
I stated in the question that I thought this might be an ID10T error, and it was.
After doing a var_dump($symbols) on it again, and paying a little closer attention, I realized that there was a trailing space behind each of the $symbols which didn't match anything in the table. Aha! So, the query was executing successfully and returning the correct results all along; go figure. ;)
Interesting thing to note, I should have done a trim on the $symbols before inserting them into the DB table using Laravel's http://laravel.com/docs/eloquent#accessors-and-mutators as pointed out by the awesome AndreasLutro in #laravel.

Why can I not echo the value of this multimensional array in PHP?

This is so incredibly basic that I am totally baffled as to why it doesn't work. I have an array called $elements, and I want to just echo out one of the values.
I use NetBeans as an IDE, and if I use that to examine the contents of the multidimensional array in question, it looks like this:
So far as I can tell, everything looks normal. It is a multidimensional array, where the first level is numbered "0", and the second level has four named entries.
I just want to echo the value of "parameters", which is a string.
However, this code outputs nothing:
echo "This is the value of 'parameters': " . $elements[0]['parameters'];
Have I got this most basic code wrong in some way?
This is what I get if I do var_dump($elements):
array(1) { [0]=> object(Element)#3 (4) { ["type":"Element":private]=>
string(4) "Text" ["resource":"Element":private]=> string(1) "0"
["parameters":"Element":private]=> string(209) "IP1 111.111.111.111
IP2 222.222.222.222 IP3 333.333.333.333 IP4 444.444.444.444 IP5
555.555.555.555 IP6 666.666.666.666 IP7 777.777.777.777 IP8 888.888.888.888 IP9 999.999.999.999 IP10 111.111.111.112" ["parent":"Element":private]=> NULL } }
... and this is the output from print_r($elements):
Array ( [0] => Element Object ( [type:Element:private] => Text [resource:Element:private] => 0 [parameters:Element:private] => IP1 111.111.111.111 IP2 222.222.222.222 IP3 333.333.333.333 IP4 444.444.444.444 IP5 555.555.555.555 IP6 666.666.666.666 IP7 777.777.777.777 IP8 888.888.888.888 IP9 999.999.999.999 IP10 111.111.111.112 [parent:Element:private] => ) )
Your var dump is saying that element 0 is an object, so you will need to access it like so:
echo $elements[0]->parameters;
The problem is that from your dump, the parameters element is marked as private, so you will not be able to access it.
Solutions are:
Change parameters to public
Write a getter (getParameters()) and use that method to get your parameters.
Entry 0 at $elements is not just an array of attributes it's a class Element instance so in order to access its properties do something like:
echo( $elements[ 0 ]->parameters );
Although the parameters field seems private so you'd better add an accessor method to the object like getParameters() which would be public and return the value of parameters.

Categories